← SQL: better questions for your data
05 / 8 · 22 MIN

Indexes and execution plans

Connect data access, selectivity, and maintenance cost.

Understand the concept

An index can reduce search work but consumes space and needs maintenance during writes. The planner chooses a plan using estimates; a sequential scan can be appropriate when much of the table is needed. An index existing does not prove a query uses it or should use it.

Apply and decide

Observe filters, volume, ordering, and statistics. For composite indexes, column order and predicates influence options; exact behavior depends on engine and version. EXPLAIN describes the plan; EXPLAIN ANALYZE executes the query. For writes or expensive queries, this difference matters for effects and load.

Guided workplace application

Analyze a query using parameters and distribution representative of real work. A plan estimating 100 rows and observing 20000 with loops=1 warrants investigation of the estimate; it does not by itself prove disk failure. The name Seq Scan also does not establish poor performance when a report reads almost the whole table. Compare results, rows, duration, and resources under comparable conditions. EXPLAIN without ANALYZE shows the estimated plan; ANALYZE executes the statement and can write if the statement writes. Use an appropriate environment and permissions. A rolled-back transaction is not authorization to create load or external effects. Before accepting an index, compare read gains with space and maintenance during the write batch, including its operational deadline.

IN PRACTICE

A daily query filters by account_id and time range. Evaluate an index matching that pattern and measure effects, including write cost.

Common pitfalls

Confusing estimates with measurements; running ANALYZE without considering effects; optimizing reads alone.

Related topics: Concurrency and safe updates · Query with intent

Take this idea with you

Indexes are workload-based choices, not universal speed guarantees.

Create account

Reference: PostgreSQL 18: Using EXPLAIN · PostgreSQL 18 reference semantics; DR SQL 2026.2