BE3: Fast queries

Lesson 1 of 5 ยท reading 15โ€“25 minutes; practice and project time is on the module page

How the database runs a query

๐ŸŽฏYou'll be able to

Read an EXPLAIN ANALYZE plan well enough to see where the time goes.

๐Ÿ The everyday picture

To find one student's file in a school office, the clerk can either open every cabinet drawer (slow, but works for a tiny office) or look up the index card that says "cabinet 3, drawer 2". Which one they choose depends on how big the office is and whether the card exists.

๐Ÿ“˜What it really is

๐Ÿ“A worked example

In the kit, one of the starter menu's per-dish queries:

EXPLAIN ANALYZE SELECT coalesce(sum(oi.qty), 0) FROM order_items oi JOIN orders o ON o.id = oi.order_id
WHERE oi.menu_item_id = 181 AND o.kitchen_id = 7 AND o.placed_at >= now() - interval '30 days';

What to look for in the output: a Seq Scan on order_items with Rows Removed by Filter in the hundreds of thousands, because there is no index on menu_item_id. The query is only a few milliseconds, but the starter runs it (and a ratings query) once per dish, 30 dishes per kitchen. That pattern is Lesson 3.

๐ŸŒWhere it's used

๐Ÿ“Case scenario

A developer tests a new report on their laptop with 200 orders: 3 ms. In production, with 4 million orders, it takes 9 seconds, because the plan switches from a quick scan of a tiny table to a scan of a huge one. Running EXPLAIN ANALYZE on a copy of production-sized (synthetic) data before release would have shown it.

๐Ÿ–ฑ๏ธTry it

Run the query above with EXPLAIN ANALYZE in psql. Note the scan type on each table, the actual time and the rows removed by filter. Success: you can point to the line that does the most work.

โš ๏ธCommon mistakes

๐Ÿ› ๏ธFor your project

Project 2 starts by recording EXPLAIN ANALYZE for the menu queries before you change anything.

โœ…Quick check

What does "Rows Removed by Filter: 239,000" tell you?

Show the answer

The database read many rows only to throw them away, a sign that an index or a better query could help.

Why run ANALYZE after loading lots of data?

Show the answer

It updates the statistics the planner uses to estimate rows and choose a plan.

โ† BackBE3
ProLearners Assist