Query rewriteIdentity-verification SaaS company, anonymized × red9CS-0296
A three-hour reorder query came back in 7,769 milliseconds
The problem. An identity-verification SaaS company had a reorder and stock-threshold query that ran for about three hours. The recorded total was 10,908,331 ms of duration, 10,866,343 ms of CPU, and 1,741,956,688 logical reads. It did not reproduce on a development copy, which is the usual reason a query like this survives for years.
What we did. We restored a copy from production to work on, then ran the statement once on production between 2am and 3am with NOLOCK to measure it at production scale. The root cause turned out to be a NOT IN test against a view with no WHERE clause. We rewrote it as NOT EXISTS and verified the output matched row for row.
Red9 · Performance Impact
CPU time
30,184x
10,866,343 ms down to 360 ms.
Duration
1,404x
10,908,331 ms down to 7,769 ms.
Logical reads
1.74B → 136,988
A 12,716-fold reduction, from one change to the predicate.
Duration
~3 hrs → 7,769 ms
1,404x shorter
CPU
10,866,343 ms → 360 ms
30,184x less
Reads
1,741,956,688 → 136,988
12,716x less
Reorder query
total duration
How the math works. Each multiple divides the before by the after: 10,908,331 over 7,769 for duration, 10,866,343 over 360 for CPU, and 1,741,956,688 over 136,988 for reads. The 44,304x in the client's report is its own combined score for the rewrite; the three ratios above are the ones you can divide yourself. No index was added to achieve it.
The result. A query that occupied three hours and 1.74 billion reads now finishes in under eight seconds using 360 ms of CPU. Nothing was bought and no index was created; the predicate was simply written in a form the optimizer could work with.
The technical detail
What the review found. The query used NOT IN against a view that had no filter of its own, so the plan worked through the whole set behind that view on every row. NOT EXISTS lets it stop at the first match instead, which changes the shape of the work completely.
What we changed (identifiers generalized for privacy):
-- Reorder query: ~3 hours, 10,866,343 ms CPU, 1.74B reads. Did not reproduce on DEV.
-- before: WHERE customerId NOT IN (SELECT customerId FROM vw_invalid_customers)
SELECT s.orderId, s.productId, s.quantity
FROM dbo.[standing_orders] AS s
WHERE NOT EXISTS (SELECT 1 FROM dbo.[vw_invalid_customers] AS v
WHERE v.customerId = s.customerId);
-- output verified identical to the original before and after the change
Measuring it honestly meant one controlled run on production in the small hours, because a development copy would not have told the truth.