sp_IndexCleanup: stage dm_db_index_usage_stats to fix 4-minute scan#719
Merged
erikdarlingdata merged 3 commits intodevfrom Mar 25, 2026
Merged
sp_IndexCleanup: stage dm_db_index_usage_stats to fix 4-minute scan#719erikdarlingdata merged 3 commits intodevfrom
erikdarlingdata merged 3 commits intodevfrom
Conversation
sp_QuickieStore: @find_high_impact performance overhaul (60s → 10s)
…scans The LEFT JOIN to dm_db_index_usage_stats was executed via nested loops, scanning the full DMV per row (52M actual rows, ~4 minutes). Now pre-stages the DMV for the current database into #usage_stats with a clustered PK on (object_id, index_id) before the main query, then LEFT JOINs to the temp table instead. Handles the database cursor loop with TRUNCATE on re-entry. Tested on SQL2022 (StackOverflow2013) and SQL2016 (StackOverflow2010). Co-Authored-By: Claude Opus 4.6 (1M context) <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
Pre-stages
dm_db_index_usage_statsfor the current database into#usage_statswith clustered PK, replacing the direct LEFT JOIN that caused 52M-row nested loop scans (~4 minutes).Test plan
🤖 Generated with Claude Code