Skip to content
Abdimalik
All notes

Read the query plan before adding an index

Start with the database's execution strategy, then test one focused change.

Databases· 1 min read

Draft for review · General engineering guidance; not a project case study.

Conceptual workflow · Across the application

Start with EXPLAIN

PostgreSQL's EXPLAIN describes the plan chosen for a query. Look at how rows are scanned, filtered and joined. A sequential scan is not automatically a problem; the appropriate strategy depends on the work involved.

Read estimates as estimates

Estimated costs are not elapsed milliseconds. Row estimates and the shape of the plan help explain the planner's decision. Check whether your development data represents the distribution and scale you expect.

Measure in a safe environment

EXPLAIN ANALYZE actually executes the statement. That distinction matters especially for writes. Use a controlled environment and understand the statement's effects before requesting execution measurements.

Change one thing

For a hypothetical slow orders lookup, write down the question before changing the schema. A focused review might ask:

  • Which operation accounts for the work?
  • Are estimates close to observed row counts?
  • Would an index support this filter or ordering?
  • What is the write and storage cost of that index?

Reference

PostgreSQL · Using EXPLAIN

Abdimalik Osman Hassan

Full-stack · AI engineering · Data analysis