Skip to content

Commit f922d0e

Browse files
sp_QuickieStore: use EXISTS pattern in final plan OUTER APPLY
The JOIN pattern in the OUTER APPLY wasn't getting a good plan. EXISTS with the semi-join against #hi_id_staging_plans lets the optimizer pick a better strategy for the correlated subquery. Tested on SQL2022. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 20a017f commit f922d0e

1 file changed

Lines changed: 9 additions & 4 deletions

File tree

sp_QuickieStore/sp_QuickieStore.sql

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5606,10 +5606,15 @@ OUTER APPLY
56065606
qsp.last_execution_time DESC
56075607
),
56085608
qsp.query_plan
5609-
FROM #hi_id_staging_plans AS sp
5610-
JOIN ' + @database_name_quoted + N'.sys.query_store_plan AS qsp
5611-
ON qsp.plan_id = sp.plan_id
5612-
WHERE sp.query_hash = o.query_hash
5609+
FROM ' + @database_name_quoted + N'.sys.query_store_plan AS qsp
5610+
WHERE EXISTS
5611+
(
5612+
SELECT
5613+
1/0
5614+
FROM #hi_id_staging_plans AS sp
5615+
WHERE qsp.plan_id = sp.plan_id
5616+
AND sp.query_hash = o.query_hash
5617+
)
56135618
) AS qp0
56145619
WHERE qp0.n = 1
56155620
) AS qp

0 commit comments

Comments
 (0)