Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content
In this guide

Guide · The plan, node by node

Nested Loop

2 min read

Checked on 0.6.1

A Nested Loop takes each row from its outer input and runs the inner input once for it. The inner side is usually an Index Scan that looks up the matching rows. In the sample, the outer is the Seq Scan on orders, 206,880 rows, so the inner Index Scan using order_items_order_idx runs 206,880 times. The loop above takes the 620,640 items and looks up customers 620,640 times.

Why it matters

It is the best join when the outer side is small and the inner lookup is indexed: ten orders, ten lookups. It is the worst when the outer side is big, because the cost is outer rows times one lookup. The planner chose it here because it expected 612 web orders. 206,880 was 338× more, and 620,640 lookups on customers at a correlation of 0.01 is 620,640 random pages. Check the outer row count against its estimate. When that is right, the join choice usually is too.

What to do

  1. Fix the estimate first. ANALYZE commerce.orders, and the planner switches to Hash Joins.
  2. If the estimate is right and the loop still slow, make each lookup cheaper: an index covering the join column and the filter, so a loop reads one page.
  3. SET enable_nestloop = off for one run shows whether the alternative is faster. Use it to learn, not as the fix.

In the Plan tab

In the row flow, the band out of the inner node carries ↻ ×207K ┅ ×612, the loops against the planner's, drawn red when the two are 10× or more apart. The inspector's Loops ruler reads 338× more. The time strip shows both index scans as their own segments, so you can see that the lookups, not the join, took the time.

The Plan tab for a join on one customer's orders: Index Scans on orders and order_items feeding a Nested Loop, the inner one marked as 37 loops, with the Nested Loop selected.

One customer's orders and their items: 37 orders, so order_items is scanned 37 times. The outer side is small and each lookup is indexed, which is this join at its best, and there are no findings.

Last checked against Seqora 0.6.1, PostgreSQL 17 on .