
How to Read an Execution Plan Without Trusting the Cost Percentages
- كاتب موثّق
محتويات المقال
The common misconception
What actually happens
Cost is a prediction, in the optimizer's own units
EXPLAIN ANALYZE reports actual time in milliseconds, "so they are unlikely to match up". The cost also leaves things out; PostgreSQL's, for example, ignores the time to send rows to the client. A percentage is just one operator's share of that predicted total.
An actual plan doesn't replace the estimates
EstimatedTotalSubtreeCost and, separately, measured counters: actual rows, executions, elapsed and CPU milliseconds, logical reads. The percentages come from the first set. The scan was 95% of the estimated cost; measured, it took between 80 and 210 ms of a query that ran 2.7 to 4.7 seconds across seven runs.
Some work isn't costed at all
SET STATISTICS IO listed only the 11,972 reads of the outer scan. The root operator's QueryTimeStats told the real story: UdfElapsedTime was 94% to 98% of ElapsedTime in every run.
Wrong estimates make wrong percentages
Measure, then compare versions
CREATE OR ALTER FUNCTION dbo.CustomerTier (@customerId int)RETURNS varchar(10) WITH INLINE = OFFASBEGIN DECLARE @total decimal(18, 2) = (SELECT SUM(Total) FROM dbo.Orders WHERE CustomerId = @customerId); RETURN CASE WHEN @total > 5000 THEN 'Gold' ELSE 'Standard' END;END;GO-- Plan: scan 95% of the cost. Tested: ~95% of the time is in the UDFSELECT o.Id, o.Total, dbo.CustomerTier(o.CustomerId) AS TierFROM dbo.Orders AS oWHERE o.CreatedAt >= '2025-11-01';-- The same result as a set-based query the optimizer can see and costSELECT o.Id, o.Total, CASE WHEN t.Total > 5000 THEN 'Gold' ELSE 'Standard' END AS TierFROM dbo.Orders AS oCROSS APPLY (SELECT SUM(x.Total) AS Total FROM dbo.Orders AS x WHERE x.CustomerId = o.CustomerId) AS tWHERE o.CreatedAt >= '2025-11-01';-- Estimated cost 12.5, higher than the UDF version. Tested: 0.13-0.15 sWITH INLINE = OFF keeps the demo honest: SQL Server 2019 and later can inline many scalar functions, but not all of them.
SET STATISTICS XML ON; -- actual plan with runtime countersSET STATISTICS IO, TIME ON; -- reads per table, CPU and elapsed timeSELECT o.Id, o.Total, dbo.CustomerTier(o.CustomerId) AS TierFROM dbo.Orders AS oWHERE o.CreatedAt >= '2025-11-01';-- ANALYZE runs the query; in PostgreSQL 18 it includes BUFFERSEXPLAIN (ANALYZE)SELECT id, totalFROM ordersWHERE created_at >= '2025-11-01';How to apply it in your project
Get the actual plan and the totals Run the slow query with the actual plan on, plus SET STATISTICS IO, TIME ONin SQL Server orEXPLAIN (ANALYZE)in PostgreSQL 18, on realistic data.Start at the root Read the total elapsed and CPU time first. In SQL Server, check QueryTimeStatsfor UDF time before looking at any operator.Walk the operators by measured time and rows Look for the operators with the largest actual time and reads, and for estimated rows far from actual rows. Those gaps explain most bad plan choices. Compare versions by measurements Judge a rewrite or an index by elapsed time and reads over several runs, never by its cost percentage or its share of the batch.
Key takeaways
Cost percentages are predicted shares in the optimizer's own units, and an actual plan still shows the predicted ones. Work the optimizer doesn't cost, like a non-inlined scalar UDF, can take 95% of the time behind a 5% operator. Every cost rests on row estimates; compare estimated rows with actual rows before trusting any percentage. Find the time in actual elapsed and CPU time, executions or loops, and reads. Compare query versions by measured time and reads: in the test, the faster rewrite had the higher estimated cost.
التعليقات (0)