Home›Practice›EXPLAIN reader

EXPLAIN reader

Where does my query spend its time, and why?

Paste a PostgreSQL EXPLAIN (ANALYZE, BUFFERS), a MySQL EXPLAIN ANALYZE, or a MongoDB explain("executionStats"). You get the plan as a tree, the step where most of the time goes, and the usual causes named: full scans that throw rows away, estimates that are far off, sorts and hashes that spilled to disk, and loops that run thousands of times.

Private by design. Everything runs in your browser. Nothing you paste is sent anywhere, saved, or seen by me. Still, remove host names, user names and anything sensitive before pasting if your company requires it.

Getting a good plan

Three habits that make plans worth reading

  1. Use the real thing, with numbers. EXPLAIN alone shows guesses. ANALYZE runs the query and shows what happened. For an UPDATE or DELETE, wrap it: BEGIN; EXPLAIN ANALYZE ...; ROLLBACK;
  2. Use real data sizes and real parameter values. A plan on a test table with 1,000 rows tells you little about one with 50 million.
  3. Run it twice. The first run may read from disk; the second from memory. If only the first is slow, the problem is the cache, not the plan.

To go further: plans annotated line by line, queries that look fine and are not, and the query coach for the SQL itself.

Connected

Where this shows up

The same mechanism, indexes and plans, elsewhere on the site: in real incidents, in measurements, and in designs.

Part 2 of 4 · Practice · 9 pagesAll pages

Can I see it happen? Simulations, an index you build yourself, a real PostgreSQL in your browser, experiments run on real servers, and tools for your own database: a query coach, an EXPLAIN reader, a config checker and a health check.