TwiceToldby Develoker
ConceptsSystem designLabsCheat sheetsAbout

Develoker makes free tools for learning to build software. TwiceTold explains each idea twice, as an everyday analogy and the real thing; CloudArch lets you design cloud systems and put load on them.

About ยท Labs ยท Cheat sheets ยท Learning paths

โ† Back to Query planning and EXPLAIN

๐Ÿงญ Query planning and EXPLAIN: cheat sheet

๐ŸŽจ The analogyA sat-nav choosing a route from last week's traffic counts, and going badly wrong when those counts are stale.
๐ŸŽ“ The regular explanationHow a database turns SQL into a plan, choosing scans and joins by estimated cost from table statistics, and how EXPLAIN and EXPLAIN ANALYZE show that plan with its estimated and actual row counts.
Parse, plan, executeStep 4 of 4
๐Ÿ›ฐ๏ธ The sat-nav๐Ÿง‘You๐Ÿ“Address readernames checked๐Ÿ“Š Last week's counts๐ŸงญRoute chooser3 plans priced๐Ÿง‘โ€โœˆ๏ธDriverroute ready๐Ÿ˜๏ธ The town
  1. You type where you want to go. The sat-nav first reads it and checks that every street you named really exists.
  2. The route chooser looks up last week's counts of houses and streets, and weighs up several routes.
  3. It hands the driver the quickest-looking route, turn by turn.
  4. The driver follows the route through the town and brings back what you asked for.
Same query, different plansStep 5 of 5
chosen๐Ÿง‘You๐ŸงญSat-nav๐Ÿ“Š Last week's counts๐Ÿ›ฃ๏ธ Every streetcost 1225๐Ÿ  Straight to each housecost 1350
  1. You ask to stop at every house in the quiet village. The sat-nav checks its counts, which say only a few houses are there.
  2. It guesses the time for both ways. Driving every street in town is long; going straight to so few houses is quick.
  3. It picks going straight to each house, the smallest guess.
  4. Now you ask for every house in the busy city centre. The counts say there are thousands, so zigzagging to each one in turn would take ages.
  5. Same sat-nav, same kind of question, a different route. Driving down every street in order wins this time.
Four ways to read a tableStep 4 of 4
๐Ÿง‘โ€โœˆ๏ธDriverpins first๐Ÿ“’ Street directory๐Ÿ“ Map with pins๐Ÿ˜๏ธ The townpages in order
  1. Drive down every street and look at every door. Slow, but the best way when most doors are red anyway.
  2. Look up red doors in the street directory, then drive straight to each house. Quick for a few, a lot of zigzagging for hundreds.
  3. If the directory already lists everything you wanted to know, the driver never leaves the car park.
  4. For a few hundred red doors, first stick a pin in the map for each one. Then drive the streets in order, stopping at every pinned house once.
Three ways to joinStep 3 of 3
๐Ÿ“ฆ Pile of parcelssorted๐Ÿ˜๏ธ Housessorted๐Ÿง‘โ€โœˆ๏ธDriver pairing upzipโœ… Parcel and house
  1. Only three parcels? Take them one at a time and look up each one's house in the directory.
  2. A hundred thousand parcels? First write every house onto cards in labelled boxes, then for each parcel open just the right box. It needs room on the dashboard for the cards.
  3. Both lists already in street order? Read down both at the same time, pairing as you go, like closing a zip.
Estimates, actuals and stale statisticsStep 5 of 5
๐Ÿง‘You๐Ÿ“Š Last week's countsfresh๐ŸงญSat-navnew route๐Ÿง‘โ€โœˆ๏ธDriverreal: 48,000
  1. You press the preview button. The sat-nav shows its route and its guess, 10 stops, without moving the car.
  2. You press the other button. The driver really goes, and writes the real number beside each guess, 48,000 stops where it guessed 10.
  3. A guess this far off means the notebook is out of date. A huge new estate was built on that road since the houses were last counted.
  4. You ask for a fresh count. The notebook now matches the town.
  5. Now the guess is close to the real number, and the sat-nav picks a different, much quicker route.
From Hibernate's SQL to the planStep 4 of 4
๐Ÿง‘You๐Ÿง‘โ€๐Ÿ’ผHibernate, your helper๐Ÿ“” Helper's diary? = zoe@x.io๐Ÿ–ฅ๏ธ Preview button๐ŸงญSat-navquick: 0.1 ms
  1. You ask your helper for a trip to Zoe's house. It types the trip into the sat-nav for you, and you never see what it typed.
  2. Ask the helper to keep a diary. Now every trip it types is written down, word for word.
  3. Copy the trip from the diary into the preview button and drive it for real. The sat-nav drove every street, because the directory isn't sorted by capitals.
  4. Add a directory sorted the way the helper asks. Same trip, and now the sat-nav drives straight to the house.

๐Ÿงฉ Key questions

The same sat-nav goes straight to each house for a quiet village, but drives down every street for the busy city centre. Why?
Its counts say the centre has thousands of houses, so zigzagging to each would take longer. It adds up a guess for each way from its counts and takes the smallest, so the answer depends on how many stops it expects.
The preview button guessed 10 stops, but driving it for real showed 48,000. What is the most likely cause?
The counts are from before a big new estate was built; count again. The sat-nav can only guess from its notebook. A fresh count lets it choose a sensible route again.
You have a few hundred thousand parcels and a long list of houses, neither in street order. Which way of pairing up is usually quickest?
Write the houses onto cards in labelled boxes, then check the right box for each parcel. Looking up each of so many parcels one by one takes ages, and the zip only works when both lists are already in order.
The directory is sorted by street names as written. Which trip lets the sat-nav use it?
The street named exactly as written. Changing the name first, or asking about its ending, means the sorted order no longer helps.
Your helper types every trip for you. How do you find out why one trip is slow?
Ask the helper to keep a diary, then press preview on the trip it typed. You can't fix a route you've never seen. The diary shows what was typed and the preview shows the route it chose.