Skip to content

sp_IndexCleanup: stage dm_db_index_usage_stats to fix 4-minute scan#719

Merged
erikdarlingdata merged 3 commits intodevfrom
fix/indexcleanup-stage-usage-stats
Mar 25, 2026
Merged

sp_IndexCleanup: stage dm_db_index_usage_stats to fix 4-minute scan#719
erikdarlingdata merged 3 commits intodevfrom
fix/indexcleanup-stage-usage-stats

Conversation

@erikdarlingdata
Copy link
Copy Markdown
Owner

Summary

Pre-stages dm_db_index_usage_stats for the current database into #usage_stats with clustered PK, replacing the direct LEFT JOIN that caused 52M-row nested loop scans (~4 minutes).

Test plan

  • SQL2022 — StackOverflow2013, clean results
  • SQL2016 — StackOverflow2010, clean results

🤖 Generated with Claude Code

erikdarlingdata and others added 3 commits March 24, 2026 15:28
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>
@erikdarlingdata erikdarlingdata merged commit 40e8d64 into dev Mar 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant