Reading a query plan in SQL
The plan is the engine telling you what it is about to do. Read it before you guess.
EXPLAIN QUERY PLAN
SELECT
c.name,
COUNT(*) AS orders_count
FROM customers AS c
JOIN orders AS o
ON o.customer_id = c.id
WHERE c.country = 'GB'
GROUP BY c.name
ORDER BY orders_count DESC;
How it works
EXPLAIN QUERY PLANprints the plan without running the query.SCANreads every row;SEARCHjumped in by index or primary key.USE TEMP B-TREE FOR ORDER BYmeans the sort had no index to lean on.
Keywords and builtins used here
ASBYCOUNTDESCEXPLAINFROMGROUPJOINONORDERSELECTWHEREc
The run, in numbers
- Lines
- 10
- Characters to type
- 186
- Tokens
- 46
- Three-star pace
- 90 tpm
At the three-star pace of 90 tokens a minute, this run takes about 31 seconds.
Step 1 of 2 in Reading a plan, step 1 of 17 in Plans & performance.