Backend development

Stage 2 · Lesson 6 of 11

Reading query plans with EXPLAIN

Relational databases and SQL

shows the query plan, which is how intends to run your query. EXPLAIN ANALYZE actually runs it and reports what happened. Read the plan from the innermost step outwards.

A Seq Scan reads the whole table. An Scan or Index Only Scan uses an index. appear as Nested Loop, Join or Merge Join.

Compare the estimated rows with the actual rows. A large gap means the planner's statistics about your data are out of date, and ANALYZE refreshes them. This is how you prove an index is used, instead of hoping.

Common traptesting on a table of 50 rows: the planner rightly prefers reading the whole table there, so load realistic data before you judge a plan.

A question to check yourself, an AI practice session and 2 cheat sheet entries come with the full roadmap. Unlock

Resources for this stage

Start with· follow one of these

Practise

Keep handy

  • Query macros, pools, transactions, migrations and offline mode, with PostgreSQL examples

Also worth your time

Mark this stage as not done?