Skip to content

Commit 9ac495a

Browse files
committed
BrentOzarULTD#711 sp_BlitzIndex adding joins on database_id
Some of the checks queries weren’t joining on database_id, so they produced unpredictable results when objects in different databases had the same object_id. Closes BrentOzarULTD#711.
1 parent ebf42f7 commit 9ac495a

File tree

1 file changed

+30
-19
lines changed

1 file changed

+30
-19
lines changed

sp_BlitzIndex.sql

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1958,7 +1958,7 @@ BEGIN;
19581958

19591959
RAISERROR('check_id 2: Keys w/ identical leading columns.', 0,1) WITH NOWAIT;
19601960
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,
19621962
COUNT([object_id]) OVER ( PARTITION BY [object_id], first_key_column_name ) AS number_dupes
19631963
FROM #IndexSanity
19641964
WHERE index_type IN (1,2) /* Clustered, NC only*/
@@ -1985,6 +1985,7 @@ BEGIN;
19851985
SELECT di.[object_id]
19861986
FROM borderline_duplicate_indexes AS di
19871987
WHERE di.[object_id] = ip.[object_id] AND
1988+
di.database_id = ip.database_id AND
19881989
di.first_key_column_name = ip.first_key_column_name AND
19891990
di.key_column_names <> ip.key_column_names AND
19901991
di.number_dupes > 1
@@ -2198,12 +2199,12 @@ BEGIN;
21982199

21992200
RAISERROR(N'check_id 24: Wide clustered indexes (> 3 columns or > 16 bytes).', 0,1) WITH NOWAIT;
22002201
WITH count_columns AS (
2201-
SELECT [object_id],
2202+
SELECT database_id, [object_id],
22022203
SUM(CASE max_length WHEN -1 THEN 0 ELSE max_length END) AS sum_max_length
22032204
FROM #IndexColumns ic
22042205
WHERE index_id IN (1,0) /*Heap or clustered only*/
22052206
AND key_ordinal > 0
2206-
GROUP BY object_id
2207+
GROUP BY database_id, object_id
22072208
)
22082209
INSERT #BlitzIndexResults ( check_id, index_sanity_id, Priority, findings_group, finding, [database_name], URL, details, index_definition,
22092210
secret_columns, index_usage_summary, index_size_summary )
@@ -2219,7 +2220,7 @@ BEGIN;
22192220
+ N' bytes in clustered index:' + i.db_schema_object_name
22202221
+ N'. ' +
22212222
(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
22232224
AND i2.is_disabled=0 AND i2.is_hypothetical=0)
22242225
+ N' NC indexes on the table.'
22252226
AS details,
@@ -2229,7 +2230,8 @@ BEGIN;
22292230
ip.index_size_summary
22302231
FROM #IndexSanity i
22312232
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
22332235
WHERE index_id = 1 /* clustered only */
22342236
AND NOT (@GetAllDatabases = 1 OR @Mode = 0)
22352237
AND
@@ -2240,12 +2242,12 @@ BEGIN;
22402242

22412243
RAISERROR(N'check_id 25: Addicted to nullable columns.', 0,1) WITH NOWAIT;
22422244
WITH count_columns AS (
2243-
SELECT [object_id],
2245+
SELECT database_id, [object_id],
22442246
SUM(CASE is_nullable WHEN 1 THEN 0 ELSE 1 END) AS non_nullable_columns,
22452247
COUNT(*) AS total_columns
22462248
FROM #IndexColumns ic
22472249
WHERE index_id IN (1,0) /*Heap or clustered only*/
2248-
GROUP BY object_id
2250+
GROUP BY database_id, object_id
22492251
)
22502252
INSERT #BlitzIndexResults ( check_id, index_sanity_id, Priority, findings_group, finding, [database_name], URL, details, index_definition,
22512253
secret_columns, index_usage_summary, index_size_summary )
@@ -2267,6 +2269,7 @@ BEGIN;
22672269
FROM #IndexSanity i
22682270
JOIN #IndexSanitySize ip ON i.index_sanity_id = ip.index_sanity_id
22692271
JOIN count_columns AS cc ON i.[object_id]=cc.[object_id]
2272+
AND i.database_id = cc.database_id
22702273
WHERE i.index_id IN (1,0)
22712274
AND NOT (@GetAllDatabases = 1 OR @Mode = 0)
22722275
AND cc.non_nullable_columns < 2
@@ -2275,13 +2278,13 @@ BEGIN;
22752278

22762279
RAISERROR(N'check_id 26: Wide tables (35+ cols or > 2000 non-LOB bytes).', 0,1) WITH NOWAIT;
22772280
WITH count_columns AS (
2278-
SELECT [object_id],
2281+
SELECT database_id, [object_id],
22792282
SUM(CASE max_length WHEN -1 THEN 1 ELSE 0 END) AS count_lob_columns,
22802283
SUM(CASE max_length WHEN -1 THEN 0 ELSE max_length END) AS sum_max_length,
22812284
COUNT(*) AS total_columns
22822285
FROM #IndexColumns ic
22832286
WHERE index_id IN (1,0) /*Heap or clustered only*/
2284-
GROUP BY object_id
2287+
GROUP BY database_id, object_id
22852288
)
22862289
INSERT #BlitzIndexResults ( check_id, index_sanity_id, Priority, findings_group, finding, [database_name], URL, details, index_definition,
22872290
secret_columns, index_usage_summary, index_size_summary )
@@ -2307,6 +2310,7 @@ BEGIN;
23072310
FROM #IndexSanity i
23082311
JOIN #IndexSanitySize ip ON i.index_sanity_id = ip.index_sanity_id
23092312
JOIN count_columns AS cc ON i.[object_id]=cc.[object_id]
2313+
AND i.database_id = cc.database_id
23102314
WHERE i.index_id IN (1,0)
23112315
AND NOT (@GetAllDatabases = 1 OR @Mode = 0)
23122316
AND
@@ -2316,12 +2320,12 @@ BEGIN;
23162320

23172321
RAISERROR(N'check_id 27: Addicted to strings.', 0,1) WITH NOWAIT;
23182322
WITH count_columns AS (
2319-
SELECT [object_id],
2323+
SELECT database_id, [object_id],
23202324
SUM(CASE WHEN system_type_name IN ('varchar','nvarchar','char') OR max_length=-1 THEN 1 ELSE 0 END) AS string_or_LOB_columns,
23212325
COUNT(*) AS total_columns
23222326
FROM #IndexColumns ic
23232327
WHERE index_id IN (1,0) /*Heap or clustered only*/
2324-
GROUP BY object_id
2328+
GROUP BY database_id, object_id
23252329
)
23262330
INSERT #BlitzIndexResults ( check_id, index_sanity_id, Priority, findings_group, finding, [database_name], URL, details, index_definition,
23272331
secret_columns, index_usage_summary, index_size_summary )
@@ -2343,6 +2347,7 @@ BEGIN;
23432347
FROM #IndexSanity i
23442348
JOIN #IndexSanitySize ip ON i.index_sanity_id = ip.index_sanity_id
23452349
JOIN count_columns AS cc ON i.[object_id]=cc.[object_id]
2350+
AND i.database_id = cc.database_id
23462351
CROSS APPLY (SELECT cc.total_columns - string_or_LOB_columns AS non_string_or_lob_columns) AS calc1
23472352
WHERE i.index_id IN (1,0)
23482353
AND NOT (@GetAllDatabases = 1 OR @Mode = 0)
@@ -2363,7 +2368,9 @@ BEGIN;
23632368
N'Uniquifiers will be required! Clustered index: ' + i.db_schema_object_name
23642369
+ N' and all NC indexes. ' +
23652370
(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
23672374
AND i2.is_disabled=0 AND i2.is_hypothetical=0)
23682375
+ N' NC indexes on the table.'
23692376
AS details,
@@ -2406,11 +2413,14 @@ BEGIN;
24062413

24072414

24082415
END
2409-
----------------------------------------
2416+
----------------------------------------
24102417
--Feature-Phobic Indexes: Check_id 30-39
24112418
----------------------------------------
24122419
BEGIN
24132420
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+
*/
24142424

24152425
DECLARE @number_indexes_with_includes INT;
24162426
DECLARE @percent_indexes_with_includes NUMERIC(10, 1);
@@ -2499,6 +2509,7 @@ BEGIN;
24992509
sz.index_size_summary
25002510
FROM #IndexColumns ic
25012511
JOIN #IndexSanity i ON
2512+
ic.database_id = i.database_id AND
25022513
ic.[object_id]=i.[object_id] AND
25032514
ic.[index_id]=i.[index_id] AND
25042515
i.[index_id] > 1 /* non-clustered index */
@@ -3116,12 +3127,12 @@ BEGIN;
31163127

31173128
RAISERROR(N'check_id 69: Column collation does not match database collation', 0,1) WITH NOWAIT;
31183129
WITH count_columns AS (
3119-
SELECT [object_id],
3130+
SELECT database_id, [object_id],
31203131
COUNT(*) AS column_count
31213132
FROM #IndexColumns ic
31223133
WHERE index_id IN (1,0) /*Heap or clustered only*/
31233134
AND collation_name <> @collation
3124-
GROUP BY object_id
3135+
GROUP BY database_id, object_id
31253136
)
31263137
INSERT #BlitzIndexResults ( check_id, index_sanity_id, Priority, findings_group, finding, [database_name], URL, details, index_definition,
31273138
secret_columns, index_usage_summary, index_size_summary )
@@ -3143,19 +3154,19 @@ BEGIN;
31433154
ISNULL(ip.index_size_summary,'')
31443155
FROM #IndexSanity i
31453156
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
31473158
WHERE i.index_id IN (1,0)
31483159
AND NOT (@GetAllDatabases = 1 OR @Mode = 0)
31493160
ORDER BY i.db_schema_object_name DESC OPTION ( RECOMPILE );
31503161

31513162
RAISERROR(N'check_id 70: Replicated columns', 0,1) WITH NOWAIT;
31523163
WITH count_columns AS (
3153-
SELECT [object_id],
3164+
SELECT database_id, [object_id],
31543165
COUNT(*) AS column_count,
31553166
SUM(CASE is_replicated WHEN 1 THEN 1 ELSE 0 END) AS replicated_column_count
31563167
FROM #IndexColumns ic
31573168
WHERE index_id IN (1,0) /*Heap or clustered only*/
3158-
GROUP BY object_id
3169+
GROUP BY database_id, object_id
31593170
)
31603171
INSERT #BlitzIndexResults ( check_id, index_sanity_id, Priority, findings_group, finding, [database_name], URL, details, index_definition,
31613172
secret_columns, index_usage_summary, index_size_summary )
@@ -3178,7 +3189,7 @@ BEGIN;
31783189
ISNULL(ip.index_size_summary,'')
31793190
FROM #IndexSanity i
31803191
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
31823193
WHERE i.index_id IN (1,0)
31833194
AND replicated_column_count > 0
31843195
AND NOT (@GetAllDatabases = 1 OR @Mode = 0)

0 commit comments

Comments
 (0)