@@ -1958,7 +1958,7 @@ BEGIN;
1958
1958
1959
1959
RAISERROR (' check_id 2: Keys w/ identical leading columns.' , 0 ,1 ) WITH NOWAIT ;
1960
1960
WITH borderline_duplicate_indexes
1961
- AS ( SELECT DISTINCT [object_id], first_key_column_name, key_column_names,
1961
+ AS ( SELECT DISTINCT database_id, [object_id], first_key_column_name, key_column_names,
1962
1962
COUNT ([object_id]) OVER ( PARTITION BY [object_id], first_key_column_name ) AS number_dupes
1963
1963
FROM #IndexSanity
1964
1964
WHERE index_type IN (1 ,2 ) /* Clustered, NC only*/
@@ -1985,6 +1985,7 @@ BEGIN;
1985
1985
SELECT di.[object_id]
1986
1986
FROM borderline_duplicate_indexes AS di
1987
1987
WHERE di.[object_id] = ip.[object_id] AND
1988
+ di .database_id = ip .database_id AND
1988
1989
di .first_key_column_name = ip .first_key_column_name AND
1989
1990
di .key_column_names <> ip .key_column_names AND
1990
1991
di .number_dupes > 1
@@ -2198,12 +2199,12 @@ BEGIN;
2198
2199
2199
2200
RAISERROR (N ' check_id 24: Wide clustered indexes (> 3 columns or > 16 bytes).' , 0 ,1 ) WITH NOWAIT ;
2200
2201
WITH count_columns AS (
2201
- SELECT [object_id],
2202
+ SELECT database_id, [object_id],
2202
2203
SUM (CASE max_length WHEN - 1 THEN 0 ELSE max_length END ) AS sum_max_length
2203
2204
FROM #IndexColumns ic
2204
2205
WHERE index_id IN (1 ,0 ) /* Heap or clustered only*/
2205
2206
AND key_ordinal > 0
2206
- GROUP BY object_id
2207
+ GROUP BY database_id, object_id
2207
2208
)
2208
2209
INSERT #BlitzIndexResults ( check_id, index_sanity_id, Priority, findings_group, finding, [database_name], URL , details, index_definition,
2209
2210
secret_columns, index_usage_summary, index_size_summary )
@@ -2219,7 +2220,7 @@ BEGIN;
2219
2220
+ N ' bytes in clustered index:' + i .db_schema_object_name
2220
2221
+ N ' . ' +
2221
2222
(SELECT CAST (COUNT (* ) AS NVARCHAR (23 )) FROM #IndexSanity i2
2222
- WHERE i2.[object_id]= i.[object_id] AND i2 .index_id <> 1
2223
+ WHERE i2.[object_id]= i.[object_id] AND i2 .database_id = i . database_id AND i2 . index_id <> 1
2223
2224
AND i2 .is_disabled = 0 AND i2 .is_hypothetical = 0 )
2224
2225
+ N ' NC indexes on the table.'
2225
2226
AS details,
@@ -2229,7 +2230,8 @@ BEGIN;
2229
2230
ip .index_size_summary
2230
2231
FROM #IndexSanity i
2231
2232
JOIN #IndexSanitySize ip ON i .index_sanity_id = ip .index_sanity_id
2232
- JOIN count_columns AS cc ON i.[object_id]= cc.[object_id]
2233
+ JOIN count_columns AS cc ON i.[object_id]= cc.[object_id]
2234
+ AND i .database_id = cc .database_id
2233
2235
WHERE index_id = 1 /* clustered only */
2234
2236
AND NOT (@GetAllDatabases = 1 OR @Mode = 0 )
2235
2237
AND
@@ -2240,12 +2242,12 @@ BEGIN;
2240
2242
2241
2243
RAISERROR (N ' check_id 25: Addicted to nullable columns.' , 0 ,1 ) WITH NOWAIT ;
2242
2244
WITH count_columns AS (
2243
- SELECT [object_id],
2245
+ SELECT database_id, [object_id],
2244
2246
SUM (CASE is_nullable WHEN 1 THEN 0 ELSE 1 END ) AS non_nullable_columns,
2245
2247
COUNT (* ) AS total_columns
2246
2248
FROM #IndexColumns ic
2247
2249
WHERE index_id IN (1 ,0 ) /* Heap or clustered only*/
2248
- GROUP BY object_id
2250
+ GROUP BY database_id, object_id
2249
2251
)
2250
2252
INSERT #BlitzIndexResults ( check_id, index_sanity_id, Priority, findings_group, finding, [database_name], URL , details, index_definition,
2251
2253
secret_columns, index_usage_summary, index_size_summary )
@@ -2267,6 +2269,7 @@ BEGIN;
2267
2269
FROM #IndexSanity i
2268
2270
JOIN #IndexSanitySize ip ON i .index_sanity_id = ip .index_sanity_id
2269
2271
JOIN count_columns AS cc ON i.[object_id]= cc.[object_id]
2272
+ AND i .database_id = cc .database_id
2270
2273
WHERE i .index_id IN (1 ,0 )
2271
2274
AND NOT (@GetAllDatabases = 1 OR @Mode = 0 )
2272
2275
AND cc .non_nullable_columns < 2
@@ -2275,13 +2278,13 @@ BEGIN;
2275
2278
2276
2279
RAISERROR (N ' check_id 26: Wide tables (35+ cols or > 2000 non-LOB bytes).' , 0 ,1 ) WITH NOWAIT ;
2277
2280
WITH count_columns AS (
2278
- SELECT [object_id],
2281
+ SELECT database_id, [object_id],
2279
2282
SUM (CASE max_length WHEN - 1 THEN 1 ELSE 0 END ) AS count_lob_columns,
2280
2283
SUM (CASE max_length WHEN - 1 THEN 0 ELSE max_length END ) AS sum_max_length,
2281
2284
COUNT (* ) AS total_columns
2282
2285
FROM #IndexColumns ic
2283
2286
WHERE index_id IN (1 ,0 ) /* Heap or clustered only*/
2284
- GROUP BY object_id
2287
+ GROUP BY database_id, object_id
2285
2288
)
2286
2289
INSERT #BlitzIndexResults ( check_id, index_sanity_id, Priority, findings_group, finding, [database_name], URL , details, index_definition,
2287
2290
secret_columns, index_usage_summary, index_size_summary )
@@ -2307,6 +2310,7 @@ BEGIN;
2307
2310
FROM #IndexSanity i
2308
2311
JOIN #IndexSanitySize ip ON i .index_sanity_id = ip .index_sanity_id
2309
2312
JOIN count_columns AS cc ON i.[object_id]= cc.[object_id]
2313
+ AND i .database_id = cc .database_id
2310
2314
WHERE i .index_id IN (1 ,0 )
2311
2315
AND NOT (@GetAllDatabases = 1 OR @Mode = 0 )
2312
2316
AND
@@ -2316,12 +2320,12 @@ BEGIN;
2316
2320
2317
2321
RAISERROR (N ' check_id 27: Addicted to strings.' , 0 ,1 ) WITH NOWAIT ;
2318
2322
WITH count_columns AS (
2319
- SELECT [object_id],
2323
+ SELECT database_id, [object_id],
2320
2324
SUM (CASE WHEN system_type_name IN (' varchar' ,' nvarchar' ,' char' ) OR max_length= - 1 THEN 1 ELSE 0 END ) AS string_or_LOB_columns,
2321
2325
COUNT (* ) AS total_columns
2322
2326
FROM #IndexColumns ic
2323
2327
WHERE index_id IN (1 ,0 ) /* Heap or clustered only*/
2324
- GROUP BY object_id
2328
+ GROUP BY database_id, object_id
2325
2329
)
2326
2330
INSERT #BlitzIndexResults ( check_id, index_sanity_id, Priority, findings_group, finding, [database_name], URL , details, index_definition,
2327
2331
secret_columns, index_usage_summary, index_size_summary )
@@ -2343,6 +2347,7 @@ BEGIN;
2343
2347
FROM #IndexSanity i
2344
2348
JOIN #IndexSanitySize ip ON i .index_sanity_id = ip .index_sanity_id
2345
2349
JOIN count_columns AS cc ON i.[object_id]= cc.[object_id]
2350
+ AND i .database_id = cc .database_id
2346
2351
CROSS APPLY (SELECT cc .total_columns - string_or_LOB_columns AS non_string_or_lob_columns) AS calc1
2347
2352
WHERE i .index_id IN (1 ,0 )
2348
2353
AND NOT (@GetAllDatabases = 1 OR @Mode = 0 )
@@ -2363,7 +2368,9 @@ BEGIN;
2363
2368
N ' Uniquifiers will be required! Clustered index: ' + i .db_schema_object_name
2364
2369
+ N ' and all NC indexes. ' +
2365
2370
(SELECT CAST (COUNT (* ) AS NVARCHAR (23 )) FROM #IndexSanity i2
2366
- WHERE i2.[object_id]= i.[object_id] AND i2 .index_id <> 1
2371
+ WHERE i2.[object_id]= i.[object_id]
2372
+ AND i2 .database_id = i .database_id
2373
+ AND i2 .index_id <> 1
2367
2374
AND i2 .is_disabled = 0 AND i2 .is_hypothetical = 0 )
2368
2375
+ N ' NC indexes on the table.'
2369
2376
AS details,
@@ -2406,11 +2413,14 @@ BEGIN;
2406
2413
2407
2414
2408
2415
END
2409
- -- --------------------------------------
2416
+ -- --------------------------------------
2410
2417
-- Feature-Phobic Indexes: Check_id 30-39
2411
2418
-- --------------------------------------
2412
2419
BEGIN
2413
2420
RAISERROR (N ' check_id 30: No indexes with includes' , 0 ,1 ) WITH NOWAIT ;
2421
+ /* This does not work the way you'd expect with @GetAllDatabases = 1. For details:
2422
+ https://github.com/BrentOzarULTD/SQL-Server-First-Responder-Kit/issues/825
2423
+ */
2414
2424
2415
2425
DECLARE @number_indexes_with_includes INT ;
2416
2426
DECLARE @percent_indexes_with_includes NUMERIC (10 , 1 );
@@ -2499,6 +2509,7 @@ BEGIN;
2499
2509
sz .index_size_summary
2500
2510
FROM #IndexColumns ic
2501
2511
JOIN #IndexSanity i ON
2512
+ ic .database_id = i .database_id AND
2502
2513
ic.[object_id]= i.[object_id] AND
2503
2514
ic.[index_id]= i.[index_id] AND
2504
2515
i.[index_id] > 1 /* non-clustered index */
@@ -3116,12 +3127,12 @@ BEGIN;
3116
3127
3117
3128
RAISERROR (N ' check_id 69: Column collation does not match database collation' , 0 ,1 ) WITH NOWAIT ;
3118
3129
WITH count_columns AS (
3119
- SELECT [object_id],
3130
+ SELECT database_id, [object_id],
3120
3131
COUNT (* ) AS column_count
3121
3132
FROM #IndexColumns ic
3122
3133
WHERE index_id IN (1 ,0 ) /* Heap or clustered only*/
3123
3134
AND collation_name <> @collation
3124
- GROUP BY object_id
3135
+ GROUP BY database_id, object_id
3125
3136
)
3126
3137
INSERT #BlitzIndexResults ( check_id, index_sanity_id, Priority, findings_group, finding, [database_name], URL , details, index_definition,
3127
3138
secret_columns, index_usage_summary, index_size_summary )
@@ -3143,19 +3154,19 @@ BEGIN;
3143
3154
ISNULL (ip .index_size_summary ,' ' )
3144
3155
FROM #IndexSanity i
3145
3156
JOIN #IndexSanitySize ip ON i .index_sanity_id = ip .index_sanity_id
3146
- JOIN count_columns AS cc ON i.[object_id]= cc.[object_id]
3157
+ JOIN count_columns AS cc ON i.[object_id]= cc.[object_id] AND i . database_id = cc . database_id
3147
3158
WHERE i .index_id IN (1 ,0 )
3148
3159
AND NOT (@GetAllDatabases = 1 OR @Mode = 0 )
3149
3160
ORDER BY i .db_schema_object_name DESC OPTION ( RECOMPILE );
3150
3161
3151
3162
RAISERROR (N ' check_id 70: Replicated columns' , 0 ,1 ) WITH NOWAIT ;
3152
3163
WITH count_columns AS (
3153
- SELECT [object_id],
3164
+ SELECT database_id, [object_id],
3154
3165
COUNT (* ) AS column_count,
3155
3166
SUM (CASE is_replicated WHEN 1 THEN 1 ELSE 0 END ) AS replicated_column_count
3156
3167
FROM #IndexColumns ic
3157
3168
WHERE index_id IN (1 ,0 ) /* Heap or clustered only*/
3158
- GROUP BY object_id
3169
+ GROUP BY database_id, object_id
3159
3170
)
3160
3171
INSERT #BlitzIndexResults ( check_id, index_sanity_id, Priority, findings_group, finding, [database_name], URL , details, index_definition,
3161
3172
secret_columns, index_usage_summary, index_size_summary )
@@ -3178,7 +3189,7 @@ BEGIN;
3178
3189
ISNULL (ip .index_size_summary ,' ' )
3179
3190
FROM #IndexSanity i
3180
3191
JOIN #IndexSanitySize ip ON i .index_sanity_id = ip .index_sanity_id
3181
- JOIN count_columns AS cc ON i.[object_id]= cc.[object_id]
3192
+ JOIN count_columns AS cc ON i.[object_id]= cc.[object_id] AND i . database_id = cc . database_id
3182
3193
WHERE i .index_id IN (1 ,0 )
3183
3194
AND replicated_column_count > 0
3184
3195
AND NOT (@GetAllDatabases = 1 OR @Mode = 0 )
0 commit comments