sp_HealthParser: add MAXDOP 1 to SELECT INTO queries#694
Merged
erikdarlingdata merged 1 commit intodevfrom Mar 16, 2026
Merged
Conversation
SELECT INTO with OPTION(RECOMPILE) produces parallel insert plans that cause massive LATCH_EX waits on tempdb allocation pages — even when zero rows are produced (common with @warnings_only = 1). Actual execution plans showed 253 seconds of cumulative LATCH_EX across 6 queries, with trivial CPU time (41-183ms). Each query spawned 8 parallel threads spending 7+ seconds each in latch coordination over empty result sets. Adding MAXDOP 1 eliminates the parallel insert while preserving RECOMPILE for plan optimization. Tested on SQL Server 2022: Before: 50-54 seconds (avg 52,332ms) After: 4-5 seconds (avg 4,684ms) ~10x improvement. Results consistent at idle and under TPC-H load. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
MAXDOP 1to all 23SELECT INTOqueries that useOPTION(RECOMPILE)Problem
When sp_HealthParser runs with
@warnings_only = 1(the common case for scheduled collection), most SELECT INTO queries produce zero rows. However,OPTION(RECOMPILE)causes SQL Server to generate parallel insert plans for these SELECT INTO statements.Under any level of concurrent activity, the parallel threads hit tempdb allocation page latch contention (LATCH_EX), even though there's nothing to insert. Actual execution plans from a production collector showed:
253 seconds of cumulative LATCH_EX across 6 queries, with under 1 second of actual CPU work.
Fix
OPTION(RECOMPILE)→OPTION(RECOMPILE, MAXDOP 1)on all SELECT INTO queries. This eliminates the parallel insert operator while preserving RECOMPILE for cardinality-based plan optimization.Test Results (SQL Server 2022, idle server)
~10x improvement with zero trade-off — the data volumes from system_health XML (1-hour windows) are too small for parallelism to provide any benefit.
Test plan
@warnings_only = 0to verify results are identical@skip_locks = 0, @skip_waits = 0to exercise all 23 queries🤖 Generated with Claude Code