@@ -3084,172 +3084,6 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30843084 ia.*
30853085 FROM #index_analysis AS ia
30863086 OPTION (RECOMPILE );
3087-
3088- /* Special debug for exact duplicates */
3089- RAISERROR (' Special debug for exact duplicates after rule 2:' , 0 , 0 ) WITH NOWAIT ;
3090- SELECT
3091- ia1 .index_name AS index1_name,
3092- ia1 .action AS index1_action,
3093- ia1 .consolidation_rule AS index1_rule,
3094- ia1 .index_priority AS index1_priority,
3095- ia1 .target_index_name AS index1_target,
3096- ia1 .filter_definition AS index1_filter,
3097- ia2 .index_name AS index2_name,
3098- ia2 .action AS index2_action,
3099- ia2 .consolidation_rule AS index2_rule,
3100- ia2 .index_priority AS index2_priority,
3101- ia2 .target_index_name AS index2_target,
3102- ia2 .filter_definition AS index2_filter
3103- FROM #index_analysis AS ia1
3104- JOIN #index_analysis AS ia2
3105- ON ia1 .scope_hash = ia2 .scope_hash /* Same database and object */
3106- AND ia1 .index_name <> ia2 .index_name
3107- AND ia1 .exact_match_hash = ia2 .exact_match_hash /* Exact match: keys + includes + filter */
3108- WHERE ia1 .consolidation_rule = N ' Exact Duplicate'
3109- OR ia2 .consolidation_rule = N ' Exact Duplicate'
3110- ORDER BY ia1 .index_name
3111- OPTION (RECOMPILE );
3112- END ;
3113-
3114- /* Rule 3: Superset/subset key columns (runs before key duplicates to prioritize subset/superset relationships) */
3115- IF @debug = 1
3116- BEGIN
3117- RAISERROR (' Rule 3 debug - Testing JOIN conditions for subset/superset:' , 0 , 0 ) WITH NOWAIT ;
3118- SELECT
3119- subset_index = ia1 .index_name ,
3120- subset_keys = ia1 .key_columns ,
3121- superset_index = ia2 .index_name ,
3122- superset_keys = ia2 .key_columns ,
3123- pattern_test = REPLACE (REPLACE (REPLACE (ia1 .key_columns , ' ~' , ' ~~' ), ' [' , ' ~[' ), ' ]' , ' ~]' ) + N ' , %' ,
3124- like_match =
3125- CASE
3126- WHEN ia2 .key_columns LIKE (REPLACE (REPLACE (REPLACE (ia1 .key_columns , ' ~' , ' ~~' ), ' [' , ' ~[' ), ' ]' , ' ~]' ) + N ' , %' ) ESCAPE ' ~'
3127- THEN ' YES'
3128- ELSE ' NO'
3129- END ,
3130- filter_match =
3131- CASE
3132- WHEN ISNULL (ia1 .filter_definition , ' ' ) = ISNULL (ia2 .filter_definition , ' ' )
3133- THEN ' YES'
3134- ELSE ' NO'
3135- END ,
3136- ia1_eligible =
3137- CASE
3138- WHEN EXISTS
3139- (
3140- SELECT 1 / 0
3141- FROM #index_details AS id1
3142- WHERE id1 .index_hash = ia1 .index_hash
3143- AND id1 .is_eligible_for_dedupe = 1
3144- )
3145- THEN ' YES'
3146- ELSE ' NO'
3147- END ,
3148- ia2_eligible =
3149- CASE
3150- WHEN EXISTS
3151- (
3152- SELECT 1 / 0
3153- FROM #index_details AS id2
3154- WHERE id2 .index_hash = ia2 .index_hash
3155- AND id2 .is_eligible_for_dedupe = 1
3156- )
3157- THEN ' YES'
3158- ELSE ' NO'
3159- END ,
3160- sort_mismatch =
3161- CASE
3162- WHEN EXISTS
3163- (
3164- SELECT 1 / 0
3165- FROM #index_details AS id1
3166- JOIN #index_details AS id2
3167- ON id2 .column_position_hash = id1 .column_position_hash
3168- WHERE id1 .index_hash = ia1 .index_hash
3169- AND id2 .index_hash = ia2 .index_hash
3170- AND id1 .is_descending_key <> id2 .is_descending_key
3171- )
3172- THEN ' YES'
3173- ELSE ' NO'
3174- END ,
3175- ia1_consolidation_rule = ISNULL (ia1 .consolidation_rule , ' NULL' ),
3176- ia2_consolidation_rule = ISNULL (ia2 .consolidation_rule , ' NULL' ),
3177- ia1_is_unique_constraint =
3178- CASE
3179- WHEN EXISTS
3180- (
3181- SELECT 1 / 0
3182- FROM #index_details AS id1_uc
3183- WHERE id1_uc .index_hash = ia1 .index_hash
3184- AND id1_uc .is_unique_constraint = 1
3185- )
3186- THEN ' YES'
3187- ELSE ' NO'
3188- END ,
3189- ia2_is_unique_constraint =
3190- CASE
3191- WHEN EXISTS
3192- (
3193- SELECT 1 / 0
3194- FROM #index_details AS id2_uc
3195- WHERE id2_uc .index_hash = ia2 .index_hash
3196- AND id2_uc .is_unique_constraint = 1
3197- )
3198- THEN ' YES'
3199- ELSE ' NO'
3200- END
3201- FROM #index_analysis AS ia1
3202- JOIN #index_analysis AS ia2
3203- ON ia1 .scope_hash = ia2 .scope_hash
3204- AND ia1 .index_name <> ia2 .index_name
3205- AND ia2 .key_columns LIKE (REPLACE (REPLACE (REPLACE (ia1 .key_columns , ' ~' , ' ~~' ), ' [' , ' ~[' ), ' ]' , ' ~]' ) + N ' , %' ) ESCAPE ' ~'
3206- AND ISNULL (ia1 .filter_definition , ' ' ) = ISNULL (ia2 .filter_definition , ' ' )
3207- AND NOT (ia1 .is_unique = 1 AND ia2 .is_unique = 0 )
3208- WHERE ia1 .consolidation_rule IS NULL
3209- AND ia2 .consolidation_rule IS NULL
3210- AND NOT EXISTS
3211- (
3212- SELECT 1 / 0
3213- FROM #index_details AS id1_uc
3214- WHERE id1_uc .index_hash = ia1 .index_hash
3215- AND id1_uc .is_unique_constraint = 1
3216- )
3217- AND NOT EXISTS
3218- (
3219- SELECT 1 / 0
3220- FROM #index_details AS id2_uc
3221- WHERE id2_uc .index_hash = ia2 .index_hash
3222- AND id2_uc .is_unique_constraint = 1
3223- )
3224- AND EXISTS
3225- (
3226- SELECT 1 / 0
3227- FROM #index_details AS id1
3228- WHERE id1 .index_hash = ia1 .index_hash
3229- AND id1 .is_eligible_for_dedupe = 1
3230- )
3231- AND EXISTS
3232- (
3233- SELECT 1 / 0
3234- FROM #index_details AS id2
3235- WHERE id2 .index_hash = ia2 .index_hash
3236- AND id2 .is_eligible_for_dedupe = 1
3237- )
3238- AND NOT EXISTS
3239- (
3240- SELECT 1 / 0
3241- FROM #index_details AS id1
3242- JOIN #index_details AS id2
3243- ON id2 .column_position_hash = id1 .column_position_hash
3244- WHERE id1 .index_hash = ia1 .index_hash
3245- AND id2 .index_hash = ia2 .index_hash
3246- AND id1 .is_descending_key <> id2 .is_descending_key
3247- )
3248- AND (ia1 .index_name LIKE N ' IX_Users_%Subset%' AND ia2 .index_name LIKE N ' IX_Users_%Superset%' )
3249- ORDER BY
3250- ia1 .index_name ,
3251- ia2 .index_name
3252- OPTION (RECOMPILE );
32533087 END ;
32543088
32553089 UPDATE
@@ -3359,27 +3193,6 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33593193 ia.*
33603194 FROM #index_analysis AS ia
33613195 OPTION (RECOMPILE );
3362-
3363- /* Special debug for subset/superset test indexes */
3364- RAISERROR (' Special debug for #index_details subset/superset eligibility:' , 0 , 0 ) WITH NOWAIT ;
3365- SELECT
3366- index_id = id .index_id ,
3367- index_name = id .index_name ,
3368- column_name = id .column_name ,
3369- key_ordinal = id .key_ordinal ,
3370- is_descending_key = id .is_descending_key ,
3371- is_included_column = id .is_included_column ,
3372- is_eligible_for_dedupe = id .is_eligible_for_dedupe ,
3373- is_unique_constraint = id .is_unique_constraint ,
3374- is_primary_key = id .is_primary_key
3375- FROM #index_details AS id
3376- WHERE id .index_name LIKE N ' IX_Users_%Subset%'
3377- OR id .index_name LIKE N ' IX_Users_%Superset%'
3378- ORDER BY
3379- id .index_id ,
3380- id .key_ordinal ,
3381- id .column_name
3382- OPTION (RECOMPILE );
33833196 END ;
33843197
33853198 /* Rule 4: Mark superset indexes for merging with includes from subset */
@@ -3416,73 +3229,6 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34163229 END ;
34173230
34183231 /* Rule 5: Key duplicates - matching key columns, different includes */
3419- IF @debug = 1
3420- BEGIN
3421- RAISERROR (' Rule 5 debug - Testing key duplicates with different includes:' , 0 , 0 ) WITH NOWAIT ;
3422- SELECT
3423- index1 = ia1 .index_name ,
3424- index1_keys = ia1 .key_columns ,
3425- index1_includes = ia1 .included_columns ,
3426- index2 = ia2 .index_name ,
3427- index2_keys = ia2 .key_columns ,
3428- index2_includes = ia2 .included_columns ,
3429- key_filter_match =
3430- CASE
3431- WHEN ia1 .key_filter_hash = ia2 .key_filter_hash
3432- THEN ' YES'
3433- ELSE ' NO'
3434- END ,
3435- includes_different =
3436- CASE
3437- WHEN ISNULL (ia1 .included_columns , ' ' ) <> ISNULL (ia2 .included_columns , ' ' )
3438- THEN ' YES'
3439- ELSE ' NO'
3440- END ,
3441- ia1_consolidation_rule = ISNULL (ia1 .consolidation_rule , ' NULL' ),
3442- ia2_consolidation_rule = ISNULL (ia2 .consolidation_rule , ' NULL' )
3443- FROM #index_analysis AS ia1
3444- JOIN #index_analysis AS ia2
3445- ON ia1 .scope_hash = ia2 .scope_hash
3446- AND ia1 .index_name <> ia2 .index_name
3447- AND ia1 .key_filter_hash = ia2 .key_filter_hash
3448- AND ISNULL (ia1 .included_columns , ' ' ) <> ISNULL (ia2 .included_columns , ' ' )
3449- WHERE ia1 .consolidation_rule IS NULL
3450- AND ia2 .consolidation_rule IS NULL
3451- AND NOT EXISTS
3452- (
3453- SELECT 1 / 0
3454- FROM #index_details AS id1_uc
3455- WHERE id1_uc .index_hash = ia1 .index_hash
3456- AND id1_uc .is_unique_constraint = 1
3457- )
3458- AND NOT EXISTS
3459- (
3460- SELECT 1 / 0
3461- FROM #index_details AS id2_uc
3462- WHERE id2_uc .index_hash = ia2 .index_hash
3463- AND id2_uc .is_unique_constraint = 1
3464- )
3465- AND EXISTS
3466- (
3467- SELECT 1 / 0
3468- FROM #index_details AS id1
3469- WHERE id1 .index_hash = ia1 .index_hash
3470- AND id1 .is_eligible_for_dedupe = 1
3471- )
3472- AND EXISTS
3473- (
3474- SELECT 1 / 0
3475- FROM #index_details AS id2
3476- WHERE id2 .index_hash = ia2 .index_hash
3477- AND id2 .is_eligible_for_dedupe = 1
3478- )
3479- AND (ia1 .index_name LIKE N ' %DownVotes%' OR ia1 .index_name LIKE N ' %LastAccess%' )
3480- ORDER BY
3481- ia1 .index_name ,
3482- ia2 .index_name
3483- OPTION (RECOMPILE );
3484- END ;
3485-
34863232 UPDATE
34873233 ia1
34883234 SET
@@ -3830,7 +3576,8 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
38303576 AND NOT EXISTS
38313577 (
38323578 /* Check if there's a unique constraint with matching keys that points to this index */
3833- SELECT 1
3579+ SELECT
3580+ 1 / 0
38343581 FROM #index_analysis AS ia_uc
38353582 WHERE ia_uc .scope_hash = ia .scope_hash
38363583 AND ia_uc .key_columns = ia .key_columns
0 commit comments