sp_IndexCleanup: fix #721 unique constraint subset + replace ISNULL schema join#722
Merged
erikdarlingdata merged 1 commit intodevfrom Mar 25, 2026
Merged
Conversation
…ULL schema join Issue #721: Unique constraints were excluded from both sides of the Key Subset rule, so a NC index that was a prefix of a UC's keys was never flagged. Removed the NOT EXISTS check on the wider (target) side so UCs can serve as the superset target. The narrower-side check stays to prevent disabling UCs themselves. Schema join: Replaced LEFT JOIN sys.tables + LEFT JOIN sys.views + ISNULL(t.schema_id, v.schema_id) with a single JOIN sys.objects in all 4 query locations. Eliminates the non-SARGable ISNULL predicate that caused late filtering in the optimizer. Also scoped #usage_stats staging to #filtered_objects. Tested on SQL2022 (repro from #721 confirmed fixed) and SQL2016. Closes #721 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
Bug fix (#721): Unique constraints can now serve as the superset target in Key Subset detection. Previously both sides were excluded from the rule, so a NC index with keys that were a prefix of a UC's keys was never flagged.
Schema join: Replaced
LEFT JOIN sys.tables + LEFT JOIN sys.views + ISNULL(t.schema_id, v.schema_id)withJOIN sys.objectsin all 4 query locations. Eliminates non-SARGable predicate.Test plan
IX_tbl_owntesttable_OrgunitID_PersIDcorrectly flagged as Key Subset ofUI_tbl_owntesttableCloses #721
🤖 Generated with Claude Code