Question presented to candidate: "Here is the output of EXPLAIN for a slow query. Walk me through how you read it: what are the nodes, what do the numbers mean, and what would you look for first?"
What a strong answer should cover:
EXPLAINshows the plan the optimizer chose without running the query; the plan is a tree of nodes, and you read it from the most indented (leaf) nodes up, because children produce rows for their parent- Every node line:
cost=startup..totalin arbitrary planner units (based onseq_page_cost = 1),rows= estimated rows output,width= estimated bytes per row; a parent cost includes its children - The common nodes: scans (Seq, Index, Index Only, Bitmap), joins (Nested Loop, Hash Join, Merge Join), and Sort, Aggregate / HashAggregate, Limit; plus the detail lines
Filter,Index Cond,Hash Cond,Sort Key - That estimates are only estimates: to see what really happened use
EXPLAIN ANALYZE(actual rows, loops, time) and compare estimated vs actual rows - Engine differences: SQLite
EXPLAIN QUERY PLAN(SCAN / SEARCH / USE TEMP B-TREE), MySQLEXPLAINas a table (type,key,rows,Extra) andEXPLAIN ANALYZEsince 8.0.18, SQL Server graphical / showplan - What to look for first: the node with the largest cost jump, a Seq Scan on a big table with a selective filter, row estimates far off, sorts and nested loops over many rows
Code / implementation expected: Optional โ reading a real EXPLAIN tree for a join with an aggregate.
Schema used by the examples: