Ch.18: Algorithms: Why SQL Joins Suddenly Get Slow

Outline

Transcript

0:00 Imagine checkout latency climbing during a promotion even though the SQL join hasn't changed. In our local test, PostgreSQL picked the faster of two joins we measured, and the query still got slow. My first thought was that the planner blew it. Wait, the faster join got slower? What did this plan actually read? The execution plan, PostgreSQL's account of how it ran the query, pointed to the orders feeding the join. We used temporary tables, so these aren't checkout measurements. Let's follow that scan.

0:31 Let's take the orders scan you spotted as our first checkpoint. It counts rows passing the promotion filter: 10 before the import, about 100,000 after it. The SELECT is unchanged. PostgreSQL expected only a few thousand. Hold on. Did the loader actually assign that promotion ID to all those orders? The import answers that: it supplied order and customer IDs but no promotion ID. The table default supplies promotion ID 1, exactly what the unchanged filter asks for. Oh! So every new row matches the old filter.

1:04 Same SELECT, completely different input. I didn't realize the default would reach imported rows. What did the join do with the extra orders? Your import question caught it. The plan built a hash lookup of 10,000 customer IDs once, then matched orders against it. Could we force the indexed alternative in our temporary test? So we forced a nested loop in the local temporary session. I wanted to see it too. Its customer index scan shows just over 100,000 loops, one lookup per order. I saw an index and thought, cheap.

1:37 Same. The printed time per lookup rounds to 0 milliseconds. How did the whole thing take 36? PostgreSQL prints that node's average per execution. That rounded zero got repeated 100,000 times. In one paired local run, the naturally chosen hash took about 13 milliseconds; the forced loop took 36. Nasty little rounding trick, but these aren't universal timings. Now look inside that forced loop. 36 milliseconds for a line that looked free. That's rude. Its “Index Only Scan” reports 100,000 “Heap Fetches.” It's tempting to read index-only as no table visits. Why touch the customer table here?

2:16 Those customer pages were freshly inserted. PostgreSQL hadn't marked them safe to answer from the index alone, so it checked the table for each lookup. I thought that might explain the whole gap. Then let's remove the table visits but keep the repeated probes. In a separate temporary-table control, we vacuumed the customers so PostgreSQL could mark those pages safe for index-only answers. “Heap Fetches” fell to zero. Huh. Zero table visits, and the loop still lost. In two low-contention runs, it took near 33 milliseconds; the hash was near 14. We'd have been wrong to stop at Heap Fetches: the repeated index lookups remained.

2:55 Even after isolating the loop cost, the next thing that bothers me is that bad row estimate. Could fresh statistics change the plan enough to fix this? We ran “ANALYZE.” The estimate caught up; PostgreSQL still chose the hash join. And, I mean, the loaded orders still matched. We fixed the guess; we didn't remove the work. Right. Those imported IDs are still there. Who can tell us whether they belong in this promotion? That leaves checkout, not the plan, to answer eligibility. Suppose its owner says the imported batch should never have carried this promotion ID. We can test that rule in a local transaction we roll back.

3:34 So we retag those imported rows, leave the SELECT alone, and refresh statistics. Does the join change when only the original 10 match? It does. PostgreSQL chooses the indexed loop without us forcing it; the query took about 3 milliseconds in two local runs. Oh, the loop didn't magically improve. We changed how often it runs. With 10 matches it makes sense; with 100,000 it lost. But this test cannot tell us what real checkout intended. But checkout could say the opposite. The checkout owner might say: “Those imported orders are real promotion orders.

4:10 We can't drop them just to make the graph look good.” We'd have to ask whether they were meant to qualify for that original promotion. The plan cannot answer that for us. If yes, the query has real added work. If no, fix the import path. Before changing data or filters, inspect the matching IDs and confirm that rule. A fast query that drops required orders is broken checkout. Yeah. Fast is only useful if those orders still count. Thanks for listening to Learning Podcasts.