Skip to content

Commit bf57b8d

Browse files
Update sp_BlitzIndex.sql with better #tmp table names
Named the new tables #dm_db_partition_stats_etc and #dm_db_index_operational_stats Tested on a smaller db (27TB) where the new version runs in 3 seconds, and the old 11 seconds. Same output. I'll see if I can get a result set from the old version on the larger database, now that there are a couple of holidays. Previously I have given up after 11 hours. My new version ran in 3 hours.
1 parent e9887f5 commit bf57b8d

File tree

1 file changed

+90
-94
lines changed

1 file changed

+90
-94
lines changed

sp_BlitzIndex.sql

Lines changed: 90 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -257,10 +257,10 @@ IF OBJECT_ID('tempdb..#FilteredIndexes') IS NOT NULL
257257
IF OBJECT_ID('tempdb..#Ignore_Databases') IS NOT NULL
258258
DROP TABLE #Ignore_Databases
259259

260-
IF OBJECT_ID('tempdb..#H') IS NOT NULL
261-
DROP TABLE #H
262-
IF OBJECT_ID('tempdb..#OS') IS NOT NULL
263-
DROP TABLE #OS
260+
IF OBJECT_ID('tempdb..#dm_db_partition_stats_etc') IS NOT NULL
261+
DROP TABLE #dm_db_partition_stats_etc
262+
IF OBJECT_ID('tempdb..#dm_db_index_operational_stats') IS NOT NULL
263+
DROP TABLE #dm_db_index_operational_stats
264264

265265
RAISERROR (N'Create temp tables.',0,1) WITH NOWAIT;
266266
CREATE TABLE #BlitzIndexResults
@@ -1442,86 +1442,89 @@ BEGIN TRY
14421442

14431443
--NOTE: If you want to use the newer syntax for 2012+, you'll have to change 2147483647 to 11 on line ~819
14441444
--This change was made because on a table with lots of paritions, the OUTER APPLY was crazy slow.
1445-
DROP TABLE if exists #h
1446-
create table #h
1447-
(
1448-
database_id smallint not null
1449-
, object_id int not null
1450-
, sname sysname NULL
1451-
, index_id int
1452-
, partition_number int
1453-
, partition_id bigint
1454-
, row_count bigint
1455-
, reserved_MB bigint
1456-
, reserved_LOB_MB bigint
1457-
, reserved_row_overflow_MB bigint
1458-
, lock_escalation_desc varchar(1000)/*?*/
1459-
, data_compression_desc varchar(100)/*?*/
1460-
, reserved_dictionary_MB bigint
1461-
)
1462-
drop TABLE if exists #os
1463-
create table #os
1464-
(
1465-
database_id smallint not null
1466-
, object_id int not null
1467-
, index_id int
1468-
, partition_number int
1469-
, hobt_id bigint
1470-
, leaf_insert_count bigint
1471-
, leaf_delete_count bigint
1472-
, leaf_update_count bigint
1473-
, leaf_ghost_count bigint
1474-
, nonleaf_insert_count bigint
1475-
, nonleaf_delete_count bigint
1476-
, nonleaf_update_count bigint
1477-
, leaf_allocation_count bigint
1478-
, nonleaf_allocation_count bigint
1479-
, leaf_page_merge_count bigint
1480-
, nonleaf_page_merge_count bigint
1481-
, range_scan_count bigint
1482-
, singleton_lookup_count bigint
1483-
, forwarded_fetch_count bigint
1484-
, lob_fetch_in_pages bigint
1485-
, lob_fetch_in_bytes bigint
1486-
, lob_orphan_create_count bigint
1487-
, lob_orphan_insert_count bigint
1488-
, row_overflow_fetch_in_pages bigint
1489-
, row_overflow_fetch_in_bytes bigint
1490-
, column_value_push_off_row_count bigint
1491-
, column_value_pull_in_row_count bigint
1492-
, row_lock_count bigint
1493-
, row_lock_wait_count bigint
1494-
, row_lock_wait_in_ms bigint
1495-
, page_lock_count bigint
1496-
, page_lock_wait_count bigint
1497-
, page_lock_wait_in_ms bigint
1498-
, index_lock_promotion_attempt_count bigint
1499-
, index_lock_promotion_count bigint
1500-
, page_latch_wait_count bigint
1501-
, page_latch_wait_in_ms bigint
1502-
, page_io_latch_wait_count bigint
1503-
, page_io_latch_wait_in_ms bigint
1504-
, tree_page_latch_wait_count bigint
1505-
, tree_page_latch_wait_in_ms bigint
1506-
, tree_page_io_latch_wait_count bigint
1507-
, tree_page_io_latch_wait_in_ms bigint
1508-
, page_compression_attempt_count bigint
1509-
, page_compression_success_count bigint
1510-
, version_generated_inrow bigint
1511-
, version_generated_offrow bigint
1512-
, ghost_version_inrow bigint
1513-
, ghost_version_offrow bigint
1514-
, insert_over_ghost_version_inrow bigint
1515-
, insert_over_ghost_version_offrow bigint
1516-
)
1445+
1446+
-- get relevant columns from sys.dm_db_partition_stats, sys.partitions and sys.objects
1447+
DROP TABLE if exists #dm_db_partition_stats_etc
1448+
create table #dm_db_partition_stats_etc
1449+
(
1450+
database_id smallint not null
1451+
, object_id int not null
1452+
, sname sysname NULL
1453+
, index_id int
1454+
, partition_number int
1455+
, partition_id bigint
1456+
, row_count bigint
1457+
, reserved_MB bigint
1458+
, reserved_LOB_MB bigint
1459+
, reserved_row_overflow_MB bigint
1460+
, lock_escalation_desc nvarchar(60)
1461+
, data_compression_desc nvarchar(60)
1462+
)
1463+
1464+
-- get relevant info from sys.dm_db_index_operational_stats
1465+
drop TABLE if exists #dm_db_index_operational_stats
1466+
create table #dm_db_index_operational_stats
1467+
(
1468+
database_id smallint not null
1469+
, object_id int not null
1470+
, index_id int
1471+
, partition_number int
1472+
, hobt_id bigint
1473+
, leaf_insert_count bigint
1474+
, leaf_delete_count bigint
1475+
, leaf_update_count bigint
1476+
, leaf_ghost_count bigint
1477+
, nonleaf_insert_count bigint
1478+
, nonleaf_delete_count bigint
1479+
, nonleaf_update_count bigint
1480+
, leaf_allocation_count bigint
1481+
, nonleaf_allocation_count bigint
1482+
, leaf_page_merge_count bigint
1483+
, nonleaf_page_merge_count bigint
1484+
, range_scan_count bigint
1485+
, singleton_lookup_count bigint
1486+
, forwarded_fetch_count bigint
1487+
, lob_fetch_in_pages bigint
1488+
, lob_fetch_in_bytes bigint
1489+
, lob_orphan_create_count bigint
1490+
, lob_orphan_insert_count bigint
1491+
, row_overflow_fetch_in_pages bigint
1492+
, row_overflow_fetch_in_bytes bigint
1493+
, column_value_push_off_row_count bigint
1494+
, column_value_pull_in_row_count bigint
1495+
, row_lock_count bigint
1496+
, row_lock_wait_count bigint
1497+
, row_lock_wait_in_ms bigint
1498+
, page_lock_count bigint
1499+
, page_lock_wait_count bigint
1500+
, page_lock_wait_in_ms bigint
1501+
, index_lock_promotion_attempt_count bigint
1502+
, index_lock_promotion_count bigint
1503+
, page_latch_wait_count bigint
1504+
, page_latch_wait_in_ms bigint
1505+
, page_io_latch_wait_count bigint
1506+
, page_io_latch_wait_in_ms bigint
1507+
, tree_page_latch_wait_count bigint
1508+
, tree_page_latch_wait_in_ms bigint
1509+
, tree_page_io_latch_wait_count bigint
1510+
, tree_page_io_latch_wait_in_ms bigint
1511+
, page_compression_attempt_count bigint
1512+
, page_compression_success_count bigint
1513+
, version_generated_inrow bigint
1514+
, version_generated_offrow bigint
1515+
, ghost_version_inrow bigint
1516+
, ghost_version_offrow bigint
1517+
, insert_over_ghost_version_inrow bigint
1518+
, insert_over_ghost_version_offrow bigint
1519+
)
15171520

15181521
SET @dsql = N'
15191522
DECLARE @d VARCHAR(19) = CONVERT(VARCHAR(19), GETDATE(), 121)
1520-
RAISERROR (N''start getting data into #h at %s'',0,1, @d) WITH NOWAIT;
1523+
RAISERROR (N''start getting data into #dm_db_partition_stats_etc at %s'',0,1, @d) WITH NOWAIT;
15211524
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;
1522-
INSERT INTO #h
1525+
INSERT INTO #dm_db_partition_stats_etc
15231526
(
1524-
database_id, object_id, sname, index_id, partition_number, partition_id, row_count, reserved_MB, reserved_LOB_MB, reserved_row_overflow_MB, lock_escalation_desc, data_compression_desc, reserved_dictionary_MB
1527+
database_id, object_id, sname, index_id, partition_number, partition_id, row_count, reserved_MB, reserved_LOB_MB, reserved_row_overflow_MB, lock_escalation_desc, data_compression_desc
15251528
)
15261529
SELECT ' + CAST(@DatabaseID AS NVARCHAR(10)) + N' AS database_id,
15271530
ps.object_id,
@@ -1534,16 +1537,9 @@ create table #os
15341537
ps.lob_reserved_page_count * 8. / 1024. AS reserved_LOB_MB,
15351538
ps.row_overflow_reserved_page_count * 8. / 1024. AS reserved_row_overflow_MB,
15361539
le.lock_escalation_desc,
1537-
' + CASE WHEN @SQLServerProductVersion NOT LIKE '9%' THEN N'par.data_compression_desc ' ELSE N'null as data_compression_desc ' END + N',
1540+
' + CASE WHEN @SQLServerProductVersion NOT LIKE '9%' THEN N'par.data_compression_desc ' ELSE N'null as data_compression_desc ' END + N'
15381541
';
15391542

1540-
/* Get columnstore dictionary size - more info: https://github.com/BrentOzarULTD/SQL-Server-First-Responder-Kit/issues/2585 */
1541-
IF EXISTS (SELECT * FROM sys.all_objects WHERE name = 'column_store_dictionaries')
1542-
SET @dsql = @dsql + N' COALESCE((SELECT SUM (on_disk_size / 1024.0 / 1024) FROM ' + QUOTENAME(@DatabaseName) + N'.sys.column_store_dictionaries dict WHERE dict.partition_id = ps.partition_id),0) AS reserved_dictionary_MB ';
1543-
ELSE
1544-
SET @dsql = @dsql + N' 0 AS reserved_dictionary_MB ';
1545-
1546-
15471543
SET @dsql = @dsql + N'
15481544
FROM ' + QUOTENAME(@DatabaseName) + N'.sys.dm_db_partition_stats AS ps
15491545
JOIN ' + QUOTENAME(@DatabaseName) + N'.sys.partitions AS par on ps.partition_id=par.partition_id
@@ -1574,9 +1570,9 @@ create table #os
15741570
OPTION ( RECOMPILE , min_grant_percent = 1);
15751571
15761572
SET @d = CONVERT(VARCHAR(19), GETDATE(), 121)
1577-
RAISERROR (N''start getting data into #os at %s.'',0,1, @d) WITH NOWAIT;
1573+
RAISERROR (N''start getting data into #dm_db_index_operational_stats at %s.'',0,1, @d) WITH NOWAIT;
15781574
1579-
insert into #os
1575+
insert into #dm_db_index_operational_stats
15801576
(
15811577
database_id
15821578
, object_id
@@ -1686,7 +1682,7 @@ create table #os
16861682
OPTION ( RECOMPILE , min_grant_percent = 1);
16871683
16881684
SET @d = CONVERT(VARCHAR(19), GETDATE(), 121)
1689-
RAISERROR (N''finished getting data into #os at %s.'',0,1, @d) WITH NOWAIT;
1685+
RAISERROR (N''finished getting data into #dm_db_index_operational_stats at %s.'',0,1, @d) WITH NOWAIT;
16901686
';
16911687
END;
16921688
ELSE
@@ -1843,11 +1839,11 @@ create table #os
18431839
SUM(os.page_latch_wait_in_ms),
18441840
SUM(os.page_io_latch_wait_count),
18451841
SUM(os.page_io_latch_wait_in_ms)
1846-
, h.reserved_dictionary_MB
1847-
from #h h
1848-
left JOIN #os as os ON
1842+
,COALESCE((SELECT SUM (dict.on_disk_size / 1024.0 / 1024) FROM sys.column_store_dictionaries dict WHERE dict.partition_id = h.partition_id),0) AS reserved_dictionary_MB
1843+
from #dm_db_partition_stats_etc h
1844+
left JOIN #dm_db_index_operational_stats as os ON
18491845
h.object_id=os.object_id and h.index_id=os.index_id and h.partition_number=os.partition_number
1850-
group by h.database_id, h.object_id, h.sname, h.index_id, h.partition_number, h.partition_id, h.row_count, h.reserved_MB, h.reserved_LOB_MB, h.reserved_row_overflow_MB, h.lock_escalation_desc, h.data_compression_desc, h.reserved_dictionary_MB
1846+
group by h.database_id, h.object_id, h.sname, h.index_id, h.partition_number, h.partition_id, h.row_count, h.reserved_MB, h.reserved_LOB_MB, h.reserved_row_overflow_MB, h.lock_escalation_desc, h.data_compression_desc
18511847

18521848
END; --End Check For @SkipPartitions = 0
18531849

0 commit comments

Comments
 (0)