Relational databases: collection tree

Relational databases

Query execution and performance

Deep diveAbout 16 minutes AI-generated

A query specifies the answer a database must produce. An execution plan describes the work it chooses to produce it: where it finds rows, how it matches them, and where it adds or sorts their values. Two plans can return the same two rows while doing very different amounts of work.

The shop wants October’s paid-order line value by customer, largest first. Mina’s paid order 101 contributes 39.50; Tariq’s paid order 102 contributes 40.00. Pending order 103 contributes nothing, and Edda has no result row. These are the agreed values of order lines on orders marked paid, not a sum of payments received.

We first derive the report’s work from the small shop, then investigate an actual execution on a generated history of 120,000 additional orders. The small examples explain mechanisms. The measured plans come from a Linux PostgreSQL 18.6 instance; their counts and timings describe that fixture and environment.

From SQL to an execution plan

The report joins each eligible order to its lines, adds the line values within each customer and requests a final order:

SELECT o.customer_id,
       SUM(i.quantity * i.unit_price) AS line_value
FROM orders AS o
JOIN order_items AS i ON i.order_id = o.order_id
WHERE o.status = 'paid'
  AND o.placed_at >= timestamptz '2026-10-01 00:00+00'
  AND o.placed_at <  timestamptz '2026-11-01 00:00+00'
GROUP BY o.customer_id
ORDER BY line_value DESC, o.customer_id;

The result’s grain is one row per customer who has a contributing line. Joins and the meaning of a result row establishes why each matching line must be counted. Here the question is how the engine obtains those rows.

One possible route scans and filters the three orders, keeping paid orders 101 and 102. Joining their lines produces three contributing rows. Aggregation turns those into the two customer totals, and a final sort puts Tariq’s 40.00 before Mina’s 39.50. One result row therefore means something different before and after aggregation.

Three orders become two customer totals
Figure 1. One illustrative execution. Every contributing line reaches its customer total before the final sort can return the largest.
Figure as text

Orders 101 and 102 are paid; pending order 103 is excluded. Order 101 supplies two lines, 4.50 and 35.00, for Mina. Order 102 supplies one 40.00 line for Tariq. Grouping makes Mina 39.50 and Tariq 40.00. Sorting returns Tariq first, then Mina.

Each operation that consumes rows and supplies rows to another operation is an operator. A plan connects these operators into a tree. Its leaves obtain rows; other operators join, calculate, group or sort them. A filter can be part of a scan rather than a separate operator.

SQL’s written order does not prescribe this route. A planner can choose another join method, change a legal join order or combine operations while preserving the required answer. PostgreSQL’s executor asks its child operators for rows as it needs them. Other engines also work in batches or pass rows onwards; the useful question remains what each operation consumes and produces.

A scan can supply a matching row as it finds it. A full sort must first obtain its input. That makes time to the first row different from the time to finish the whole query. In this report the largest total cannot be known until all contributing line values have been accounted for.

Scans, filters and access paths

A full table scan examines the table’s stored data and tests the rows it finds. It can return two orders after examining 120,003. The two are the size of the answer at that step, not the size of the search.

Stored rows occupy blocks called pages, and one page holds several rows. Reading a page brings its contents into memory; later visits may find it there. Consequently rows examined, rows emitted, page visits and storage reads are different counts. A cached scan still tests rows, and repeated visits to one cached page are still work. Storage and durability follows the pages and their cached copies.

An index supplies another access path. A useful date index can locate a narrow October range, then lead to the matching orders. But finding a large fraction of the table through an index can mean many row fetches, spread over most of its pages, as well as reading the index itself. Scanning those pages once can be cheaper. An index’s existence does not establish that using it saves work.

The same choice applies inside a join. The primary key on order_items (order_id, line_no) puts one order’s lines together in the index. Looking up the lines for two orders can be cheap. Repeating the lookup for many thousands of orders may cost more than reading the line table once. Indexes and access paths develops the keys, ordering and write costs behind that choice.

Skipping whole pieces of a table

Suppose a much larger order history is split into monthly pieces by placed_at. The report’s October bounds can exclude January through September and November through December before scanning any rows in those pieces. This is partition pruning. Within October, the engine still needs a scan or index access and the paid-status test.

A full-year report excludes none of those monthly pieces. Partitioning has not made its required rows disappear. Nor does dividing a table into partitions imply that they live on different servers. Table partitioning and data lifecycle explains how to choose the boundary and what it changes for identity and retention.

Join methods

The report matches orders to lines by order ID. The join’s meaning stays fixed while the physical method changes. Use the same two eligible orders and five stored lines to compare three ways of obtaining the required three matches.

Three methods, the same matching lines
Figure 2. The nested loop assumes an indexed lookup. Hashing needs a build first. The merge assumes inputs already ordered by order ID. These are different preparations, not measured relative speeds.
Figure as text

Nested loop: order 101 looks up two lines, then order 102 looks up one. Hash join: build the two eligible order keys, then probe all five lines; the two lines for order 103 do not match. Merge join: advance through ordered 101 and 102 groups, preserving every matching occurrence; when orders end, remaining 103 lines need not be consumed. Each method emits the same three lines.

Nested-loop joins

A nested loop takes one row from an outer input and searches an inner input for its matches. Those names describe their jobs in the plan, not the order in which the tables were written. Each eligible order can supply the key for an indexed line lookup.

The multiplication to watch is outer rows × inner work per repetition. Two lookups are easy to follow. Twenty thousand lookups may touch the same upper index pages twenty thousand times. If the inner input has no suitable lookup, repeatedly scanning it can be much worse. An engine may cache repeated-key results or retain a reusable inner input; check whether it actually does so before multiplying a full scan by every outer row.

Hash joins

A hash join builds a lookup table in memory from one input, then probes it with rows from the other. Here the build can contain eligible orders 101 and 102, while each stored line probes by its order ID. A matching bucket supplies candidate rows whose keys must still be checked. Hashing does not remove equal-key occurrences.

The build costs time and memory before it can answer probes. In return, it can replace many separate lookups with a pass over a larger input. A large build can exceed its memory allowance, adding temporary disk work. The size reaching the build matters more than the original table’s name or size.

Merge joins

A merge join advances through two inputs ordered by the join key. It matches the 101 groups, then the 102 groups. When both groups contain several rows, it produces every required pair, rather than silently keeping one.

Ordering may arrive from an index or require a sort. A merge can be attractive when both inputs already have useful order, but an expensive sort can outweigh the saving. The join’s key order is also different from this report’s descending total: keep the final ORDER BY whichever method is chosen.

Join order and intermediate rows

Filtering the three orders first leaves two eligible orders and three matching lines. Joining all three orders to their lines first makes five joined rows, then rejects the two for pending order 103. Both routes produce the same answer here; the first carries fewer rows onwards.

On a large history that difference can change the size of a hash table, the number of lookups and the amount later sorted. Ask where rows can be reduced without changing the question. Moving a condition across an outer join can change which unmatched rows survive, so a plausible faster route is not enough: the rewrite must preserve the result’s meaning.

Grouping, sorting and the first result

The report’s three joined rows become two totals. A hash aggregate can keep state keyed by customer ID. Customer 1 starts at 4.50, then 35.00 is added to reach 39.50, and customer 2 stays at 40.00. The working state follows the number of groups and what each calculation retains, not just the number of output rows.

An ordered aggregate instead consumes rows together by customer. Once it passes customer 1, it can finish that group. It needs the input order, supplied by a suitable path or an earlier sort. Aggregates and window functions explains the calculations and their grain; these are two ways to perform the work.

Neither method promises descending line value. A separate sort can order the completed groups. If the application adds LIMIT 1, a top-one operation can retain only the best completed candidate seen so far. It still has to consider all customer totals: the last might be the largest. A small limit reduces retained state without necessarily reducing the input examined.

Compare an order list already read newest first from a matching index. After finding ten eligible rows, that query may stop. Our report cannot use an ordinary date index to stop after ten lines and still know the ten largest customer totals. Early stopping depends on what the ordered input proves about the answer.

When working data spills to disk

A sort or hash operation has a memory allowance. If its working data outgrows that allowance, it can write temporary data to disk and read it back: a spill. A sort may create sorted runs and merge them; a hash operation may divide its input into batches. The plan’s temporary-work or spill fields distinguish this from an in-memory operation.

First check what feeds the operation. Removing unnecessary rows or wide columns can reduce its working set. Correcting an estimate can lead to a different route. More memory may help a demonstrated spill, but the allowance is not a budget for the whole server: several operations, workers and concurrent queries may each use memory. A setting that helps one isolated report can make a busy service run out.

How the planner estimates the work

Cardinality is the number of rows at a plan step. Before running the report, the planner estimates how many orders will pass the date and status tests, how many lines they will match and how many customer groups remain. These estimates help it compare access paths, join orders and working sets.

Statistics are evidence about the data, not a copy of it. They can include approximate row counts, common values, distinct-value counts and value distributions. Customer 1001, who owns most orders in our generated fixture, is a different input from a customer with a handful. A single average can misdescribe both.

Related predicates create another trap. The following invented distribution isolates the problem: most October orders are paid, while most orders in other months are pending.

A separate arithmetic model · 10,000 orders, not the measured fixture
PeriodPaidPendingAll orders
October9001001,000
Other months9008,1009,000
Total1,8008,20010,000

October matches 10% of all orders, and paid matches 18%. Treating the conditions as independent predicts 10,000 × 0.10 × 0.18 = 180 paid October orders. There are actually 900. Both individual fractions were right; their combination was wrong.

Refreshing statistics can repair an obsolete view after a large data change. Better statistics may describe skew or relationships that a basic model misses. Neither guarantees that every combination or join will be estimated correctly. Look for an error that changes the chosen work: an outer input five times larger than expected can make repeated lookups or a hash build much more costly. A large estimate error in a tiny, cheap corner need not be the first repair.

An accurate estimate can also describe an expensive request accurately. If a report needs every line in five years, a well-chosen plan may still have substantial work to do.

Reading a measured plan

The three-order example establishes the result’s meaning; it is too small to judge a reporting workload. The measured fixture keeps those rows and adds 120,000 generated orders with three lines each. It has 1,000 additional customers, with 70% of generated orders assigned to customer 1001. Dates span 2026; date and paid status are deliberately related. It models reporting data, not the shop’s stock history.

Consider a version of the report which calculates one order total at a time, then adds those totals by customer. LATERAL lets the inner query use the current order’s ID:

SELECT o.customer_id, SUM(v.order_value) AS line_value
FROM orders AS o
CROSS JOIN LATERAL (
  SELECT SUM(i.quantity * i.unit_price) AS order_value
  FROM order_items AS i
  WHERE i.order_id = o.order_id
  HAVING COUNT(*) > 0
) AS v
WHERE o.status = 'paid'
  AND o.placed_at >= timestamptz '2026-10-01 00:00+00'
  AND o.placed_at <  timestamptz '2026-11-01 00:00+00'
GROUP BY o.customer_id
ORDER BY line_value DESC, o.customer_id;

HAVING COUNT(*) > 0 suppresses the inner aggregate’s row when an order has no lines. Without it, the aggregate would still supply a null total for that order, potentially introducing a customer row absent from the original inner-join report. It matters even though every generated order has lines.

In PostgreSQL, EXPLAIN shows the estimated plan. Adding ANALYZE executes the statement and records what happened. This is a real execution, including a write’s effects when the statement writes; choose the target and statement accordingly.

EXPLAIN (ANALYZE, BUFFERS)
SELECT o.customer_id, SUM(v.order_value) AS line_value
FROM orders AS o
CROSS JOIN LATERAL (
  SELECT SUM(i.quantity * i.unit_price) AS order_value
  FROM order_items AS i
  WHERE i.order_id = o.order_id
  HAVING COUNT(*) > 0
) AS v
WHERE o.status = 'paid'
  AND o.placed_at >= timestamptz '2026-10-01 00:00+00'
  AND o.placed_at <  timestamptz '2026-11-01 00:00+00'
GROUP BY o.customer_id
ORDER BY line_value DESC, o.customer_id;

For the full-year comparison, change the bounds to 1 January 2026 inclusive and 1 January 2027 exclusive. The fixture has 20,158 paid orders in that range. The measured per-order plan runs its inner line lookup and aggregate once for every order. It sorts those order totals by customer, then adds them into 1,002 customer groups. A final sort orders the result. The complete plans are in the downloadable evidence.

Figure 3 A small inner operation, repeated 20,158 times
Scan orders → keep paid orders in 2026120,003 examined · 99,845 rejected · 20,158 emitted
Estimated output: 19,833 · actual output: 20,158
For each eligible order: index lookup → order totalIndex Scan: actual rows = 3, loops = 20,158
Inner Aggregate: actual rows = 1, loops = 20,158
Sort the 20,158 order totals by customer → add by customer1,002 customer groups emitted
Sort the groups by line value descending, then customer ID1,002 result rows · all sorts stayed in memory

Observed Linux PostgreSQL 18.6 execution, full-year parameters. Arrows summarise row flow; intermediate plan lines and costs are omitted. The index scan's displayed 3 is a rounded average per loop, not an exact count for every order: core orders 101 and 102 have two lines and one line.

The paid-order estimate is close: 19,833 against 20,158. Yet the plan still performs an index search and an aggregate once for every eligible order. Its line lookups account for 80,890 shared-buffer hits, and the whole statement reports 81,655. These are repeated accesses to cached pages, not that many different pages or storage reads. The profiled run reports zero shared reads and zero temporary blocks.

October exposes a different fact. The order scan estimates 1,614 matches and emits 9,176, about 5.7 times as many. The generated date/status relationship is one reason to question an independence estimate. It makes the repetition larger than predicted; it does not establish that refreshing statistics alone will fix the route. The full-year case shows that repeated work can be worth investigating even when its input estimate is close.

Start at the inputs and follow the rows into the region doing the work. Compare estimated output with actual output on the same basis. Check how often the operation ran: PostgreSQL’s actual rows are an average per loop, so three rows with thousands of loops means thousands of three-row results, not three rows in total.

A node’s emitted rows do not include rows it examined and rejected. Nor is a partly consumed node evidence that its full-output estimate was wrong: a parent such as LIMIT can stop asking for rows. These distinctions prevent a small displayed count from hiding either a large scan or deliberate early stopping.

Estimated cost is a planner comparison unit, not milliseconds. Measured times and page counters can include work in children; adding every line of a tree can count the same work repeatedly. Use the engine’s documented units and inclusions. Reading and repairing PostgreSQL plans follows the native fields in detail.

Change the cause and measure again

The per-order total is an intermediate calculation the report does not need to retain. Each line already belongs to exactly one order, and that order has one customer. Adding its exact numeric line values directly within each customer gives the same sum as adding first within each order and then within the customer.

The first SQL listing expresses that direct join. The proposed repair is to use it in place of the per-order lateral aggregate. The prediction is specific: remove the inner aggregate repeated for each eligible order and let the planner choose a route over the joined lines. It may still choose indexed nested loops for a small input; the rewrite does not demand a hash join.

With the full-year bounds, PostgreSQL chose an ordinary hash join within a parallel plan for the direct form. It scanned all 360,005 line rows across the leader and one worker, rather than starting 20,158 line lookups. Each process separately scanned the orders and built a hash table of the 20,158 eligible keys. The new route therefore added a second order scan and examined more lines; it removed repeated index searches and per-order aggregates.

The join emitted 60,471 matching lines in total. Local aggregates reduced them to 1,942 partial customer totals, which a gather-and-merge step fed into the final customer aggregate. That produced the same 1,002 result rows. A final sort supplied the requested order. No method was forced.

Full-year report · same fixture and result, PostgreSQL 18.6
Observed workPer-order formDirect join
Repeated inner order aggregate20,158 executionsRemoved
Line access20,158 index searchesOne parallel scan of 360,005 rows
Whole-statement shared-buffer hits81,6553,831
Shared reads / temporary blocks0 / 00 / 0
Profiled execution time44.08 ms33.93 ms

The prediction was borne out: the repeated aggregate disappeared, and the new route made far fewer buffer accesses. That is evidence for this full-year repair, not a rule that scanning more lines is always better. Its parallel setup, duplicated build and worker coordination must earn their cost too.

Result checks come before accepting a speed comparison. Compare customer IDs, exact totals, multiplicity and requested ordering. Include an eligible order with no lines and a customer without any contributing order. Summing distinct line values would change the answer when separate lines happen to have equal values; it is not an equivalent way to remove work.

The same method applies to other causes. If an estimate sends an unexpectedly large outer input into repeated lookups, test a statistics repair. If rows are fetched only to be thrown away, test an access path that excludes them earlier. If a sort spills, test reducing its input before increasing its allowance. A node name alone is not a diagnosis: scans, nested loops and sorts all perform useful work.

Forcing another method can help test a hypothesis, but a forced plan is a comparison, not proof of a lasting repair. The choice still needs to survive other parameters, changed data and competing work. And an application that now wants a smaller time window or a retained daily summary has changed the reporting contract: decide its coverage and freshness before treating it as equivalent SQL.

Parallel work and data movement

A large report can divide work among workers. Some operations then gather rows into one process; distributed engines may exchange rows across machines, often by join or group key. The boundary matters because rows have to be copied, queued and sometimes sent over a network.

Suppose two workers each process 1,000 line rows belonging to the same two customers. Sending every line across a gather boundary moves 2,000 rows before aggregation. Adding by customer locally produces two partial totals per worker, so only four rows cross. The receiver must still combine the two contributions for each customer into two final totals; concatenating them would leave four incomplete summaries.

Figure 4 Add locally before moving the rows
Worker A
1,000 line rows
→ 2 partial customer totals
Worker B
1,000 line rows
→ 2 partial customer totals
Gather boundary
2,000 line rows or 4 partial totals
Without local addition
Gather 2,000 lines, then add by customer.
With local addition
Gather 4 partial totals, then combine into 2 final totals.

An illustrative arrangement, not a measured parallel plan. Both workers have lines for the same two customers. Partial sums must be combined; concatenating them would return four rows instead of the required two. Bytes crossing the boundary depend on row width as well as row count.

The local addition helps because many lines become few groups. If almost every line belonged to a different customer, far fewer rows would disappear. Carrying unused wide values also increases movement, even when the row count stays the same.

More workers may reduce elapsed time while increasing total work through setup, duplicated inputs, coordination and merging. Uneven inputs can leave one worker doing most of the work while others wait. The slowest required worker and the gathering operation can limit the result. Sharding and distributed SQL carries this reasoning across machines; engine-specific articles interpret the actual exchanges and worker measurements.

Testing a repair against the workload

A repair for October is a candidate for October. Repeat it for a broad date range, a single day, the common customer and a rare customer. Those inputs can reverse the balance between scanning once and repeating a few cheap lookups. Keep the schema, data and statistics state fixed while comparing the two SQL forms.

Six ordinary executions per form, alternated after result and plan collection · median, with observed range in brackets
ParametersPer-order formDirect join
October · all customers18.60 ms [17.48–21.11]15.60 ms [15.14–16.76]
Full year · all customers37.09 ms [35.43–39.70]23.17 ms [22.01–23.94]
12 October · all customers4.27 ms [3.93–4.98]3.91 ms [3.82–4.39]
October · common customer 100113.08 ms [11.95–14.27]16.87 ms [16.43–18.11]
October · rare customer 10023.77 ms [3.54–4.10]4.07 ms [3.77–5.52]

These measurements include the local client round trip and receiving the result. They came from one isolated container with warmed caches, not a production load test. The broad report improved consistently in this small comparison. The common-customer report regressed: the direct form still used an ordinary hash join inside a parallel plan and scanned all lines to obtain one customer total. For the one-day and rare-customer cases it chose indexed nested loops instead. The smaller timing differences overlap ordinary variation.

So the evidence supports a full-year reporting change and rejects a blanket replacement for every request using these tables. It does not yet establish acceptance under realistic concurrent load. Even the October profiled execution was slower for the direct form, 23.27 ms against 20.26 ms, while its ordinary repetitions were faster. A single instrumented time is too weak a basis for the decision.

Run ordinary executions as well as collecting plans. Profiling adds overhead; sending and consuming the result adds work that a server execution time may exclude. Alternate the two versions and repeat them enough to see ordinary variation. A first read from storage and a later cached read are different experiments: a faster second run has not necessarily removed any work.

Before deployment, test the representative request mix and concurrency too. Compare request latency and throughput, processor and storage use, temporary work and memory pressure. Include the other queries and writes that share those resources. A new index adds maintenance work to writes; a larger memory allowance multiplies across simultaneous operations. An isolated report cannot establish those costs.

Retain the query parameters, data-generation rules, engine version, settings, statistics state, result comparison and both plans. They let another reader distinguish a route change from a warmer cache or a smaller input. The fixture SQL supplies the exact deterministic data model for this example; the report queries and recorded results and plans give the bounded comparison. The recorded settings include a 4 MiB base working-memory allowance and a 128 MiB shared-buffer pool; neither was tuned for the comparison.

When the plan is not the delay

An application request can spend time acquiring a connection, waiting for admission or waiting for a lock before useful execution proceeds. It can also spend time transferring and processing its result afterwards. If its observed delay is elsewhere, another operator repair may leave the request just as slow.

Measure the request boundary as well as the statement. Diagnosing a database incident separates those delays; Connections, overload and capacity explains the budgets and admission choices. The plan answers how this statement obtains its result. Use it to point to work, predict what a change removes, and check whether that work actually went away.

Sources and further reading

Checked against PostgreSQL 18 documentation on 25 October 2026. The small row calculations, correlation table and worker example are models. The measured report uses a separate deterministic fixture on Linux PostgreSQL 18.6. No production workload or cross-machine execution was measured.