Pcornel
Databases

Indexes & Query Plans

Cheat sheet · https://pcornel.dk/topic.php?topic=6088

Back to the Topic

Answer

Indexes (usually B-trees) let the database find rows without scanning the table; the query planner chooses access paths using statistics and shows them in EXPLAIN plans.

Remember

Index columns used in WHERE, JOIN and ORDER BY; column order matters in composite indexes. Too many indexes slow writes. Compare estimated and actual rows in EXPLAIN ANALYZE.

First checks

  • EXPLAIN ANALYZE <query> — first check for this topic
  • psql -c "SELECT now(), version();" — PostgreSQL reachable and version
  • mysql -e "SELECT NOW(), VERSION();" — MariaDB/MySQL reachable and version
  • sqlcmd -S <server> -Q "SELECT @@VERSION" — SQL Server reachable and version

Verify in this order

query → planner uses statistics → chooses index or scan → executes

Where it usually breaks

Typical failures are adding redundant indexes, indexing low-selectivity columns without workload evidence, reading only estimated plans, ignoring parameter-sensitive plans/statistics, or treating a scan as automatically bad when most rows are needed.

Indexes & Query Plans verification boundary matrix

BoundaryInspectHealthy interpretationDo not infer
IntentB-tree conceptsIntended object/scope is explicit and versioned where relevantIntent is already effective
Owning runtime/control planeComposite indexes / SelectivityOwner accepts and reports the expected object stateDownstream dependencies are healthy
Effective stateCovering/index-only accessIndependent read-back matches the intended stateEvery client sees the same result
Consumer outcomeEXPLAIN/query plansThe original bounded check succeeds in the same time windowProduction correctness outside this scope

Read-only verification commands unless stated otherwise. Confirm syntax for your platform and release. Full reference: https://pcornel.dk/topic.php?topic=6088

Confirmation

Confirm action

Continue?