Skip to content

Commit 136de61

Browse files
authored
Merge pull request #3394 from HenrikStaunPoulsen/dev
improved performance
2 parents f2b7a80 + 9a026de commit 136de61

File tree

1 file changed

+173
-40
lines changed

1 file changed

+173
-40
lines changed

sp_BlitzIndex.sql

Lines changed: 173 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -253,9 +253,14 @@ IF OBJECT_ID('tempdb..#CheckConstraints') IS NOT NULL
253253

254254
IF OBJECT_ID('tempdb..#FilteredIndexes') IS NOT NULL
255255
DROP TABLE #FilteredIndexes;
256-
256+
257257
IF OBJECT_ID('tempdb..#Ignore_Databases') IS NOT NULL
258258
DROP TABLE #Ignore_Databases
259+
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
259264

260265
RAISERROR (N'Create temp tables.',0,1) WITH NOWAIT;
261266
CREATE TABLE #BlitzIndexResults
@@ -1065,6 +1070,8 @@ FROM sys.databases
10651070
----------------------------------------
10661071
BEGIN TRY
10671072
BEGIN
1073+
DECLARE @d VARCHAR(19) = CONVERT(VARCHAR(19), GETDATE(), 121);
1074+
RAISERROR (N'starting at %s',0,1, @d) WITH NOWAIT;
10681075

10691076
--Validate SQL Server Version
10701077

@@ -1435,47 +1442,79 @@ BEGIN TRY
14351442

14361443
--NOTE: If you want to use the newer syntax for 2012+, you'll have to change 2147483647 to 11 on line ~819
14371444
--This change was made because on a table with lots of paritions, the OUTER APPLY was crazy slow.
1438-
SET @dsql = N'SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;
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+
, range_scan_count bigint
1477+
, singleton_lookup_count bigint
1478+
, forwarded_fetch_count bigint
1479+
, lob_fetch_in_pages bigint
1480+
, lob_fetch_in_bytes bigint
1481+
, row_overflow_fetch_in_pages bigint
1482+
, row_overflow_fetch_in_bytes bigint
1483+
, row_lock_count bigint
1484+
, row_lock_wait_count bigint
1485+
, row_lock_wait_in_ms bigint
1486+
, page_lock_count bigint
1487+
, page_lock_wait_count bigint
1488+
, page_lock_wait_in_ms bigint
1489+
, index_lock_promotion_attempt_count bigint
1490+
, index_lock_promotion_count bigint
1491+
, page_latch_wait_count bigint
1492+
, page_latch_wait_in_ms bigint
1493+
, page_io_latch_wait_count bigint
1494+
, page_io_latch_wait_in_ms bigint
1495+
)
1496+
1497+
SET @dsql = N'
1498+
DECLARE @d VARCHAR(19) = CONVERT(VARCHAR(19), GETDATE(), 121)
1499+
RAISERROR (N''start getting data into #dm_db_partition_stats_etc at %s'',0,1, @d) WITH NOWAIT;
1500+
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;
1501+
INSERT INTO #dm_db_partition_stats_etc
1502+
(
1503+
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
1504+
)
14391505
SELECT ' + CAST(@DatabaseID AS NVARCHAR(10)) + N' AS database_id,
14401506
ps.object_id,
1441-
s.name,
1507+
s.name as sname,
14421508
ps.index_id,
14431509
ps.partition_number,
1510+
ps.partition_id,
14441511
ps.row_count,
14451512
ps.reserved_page_count * 8. / 1024. AS reserved_MB,
14461513
ps.lob_reserved_page_count * 8. / 1024. AS reserved_LOB_MB,
14471514
ps.row_overflow_reserved_page_count * 8. / 1024. AS reserved_row_overflow_MB,
14481515
le.lock_escalation_desc,
1449-
' + CASE WHEN @SQLServerProductVersion NOT LIKE '9%' THEN N'par.data_compression_desc ' ELSE N'null as data_compression_desc ' END + N',
1450-
SUM(os.leaf_insert_count),
1451-
SUM(os.leaf_delete_count),
1452-
SUM(os.leaf_update_count),
1453-
SUM(os.range_scan_count),
1454-
SUM(os.singleton_lookup_count),
1455-
SUM(os.forwarded_fetch_count),
1456-
SUM(os.lob_fetch_in_pages),
1457-
SUM(os.lob_fetch_in_bytes),
1458-
SUM(os.row_overflow_fetch_in_pages),
1459-
SUM(os.row_overflow_fetch_in_bytes),
1460-
SUM(os.row_lock_count),
1461-
SUM(os.row_lock_wait_count),
1462-
SUM(os.row_lock_wait_in_ms),
1463-
SUM(os.page_lock_count),
1464-
SUM(os.page_lock_wait_count),
1465-
SUM(os.page_lock_wait_in_ms),
1466-
SUM(os.index_lock_promotion_attempt_count),
1467-
SUM(os.index_lock_promotion_count),
1468-
SUM(os.page_latch_wait_count),
1469-
SUM(os.page_latch_wait_in_ms),
1470-
SUM(os.page_io_latch_wait_count),
1471-
SUM(os.page_io_latch_wait_in_ms), ';
1472-
1473-
/* Get columnstore dictionary size - more info: https://github.com/BrentOzarULTD/SQL-Server-First-Responder-Kit/issues/2585 */
1474-
IF EXISTS (SELECT * FROM sys.all_objects WHERE name = 'column_store_dictionaries')
1475-
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 ';
1476-
ELSE
1477-
SET @dsql = @dsql + N' 0 AS reserved_dictionary_MB ';
1478-
1516+
' + CASE WHEN @SQLServerProductVersion NOT LIKE '9%' THEN N'par.data_compression_desc ' ELSE N'null as data_compression_desc ' END + N'
1517+
';
14791518

14801519
SET @dsql = @dsql + N'
14811520
FROM ' + QUOTENAME(@DatabaseName) + N'.sys.dm_db_partition_stats AS ps
@@ -1484,9 +1523,6 @@ BEGIN TRY
14841523
AND so.is_ms_shipped = 0 /*Exclude objects shipped by Microsoft*/
14851524
AND so.type <> ''TF'' /*Exclude table valued functions*/
14861525
JOIN ' + QUOTENAME(@DatabaseName) + N'.sys.schemas AS s ON s.schema_id = so.schema_id
1487-
LEFT JOIN ' + QUOTENAME(@DatabaseName) + N'.sys.dm_db_index_operational_stats('
1488-
+ CAST(@DatabaseID AS NVARCHAR(10)) + N', NULL, NULL,NULL) AS os ON
1489-
ps.object_id=os.object_id and ps.index_id=os.index_id and ps.partition_number=os.partition_number
14901526
OUTER APPLY (SELECT st.lock_escalation_desc
14911527
FROM ' + QUOTENAME(@DatabaseName) + N'.sys.tables st
14921528
WHERE st.object_id = ps.object_id
@@ -1506,7 +1542,75 @@ BEGIN TRY
15061542
le.lock_escalation_desc,
15071543
' + CASE WHEN @SQLServerProductVersion NOT LIKE '9%' THEN N'par.data_compression_desc ' ELSE N'null as data_compression_desc ' END + N'
15081544
ORDER BY ps.object_id, ps.index_id, ps.partition_number
1509-
OPTION ( RECOMPILE );
1545+
/*OPTION ( RECOMPILE );*/
1546+
OPTION ( RECOMPILE , min_grant_percent = 1);
1547+
1548+
SET @d = CONVERT(VARCHAR(19), GETDATE(), 121)
1549+
RAISERROR (N''start getting data into #dm_db_index_operational_stats at %s.'',0,1, @d) WITH NOWAIT;
1550+
1551+
insert into #dm_db_index_operational_stats
1552+
(
1553+
database_id
1554+
, object_id
1555+
, index_id
1556+
, partition_number
1557+
, hobt_id
1558+
, leaf_insert_count
1559+
, leaf_delete_count
1560+
, leaf_update_count
1561+
, range_scan_count
1562+
, singleton_lookup_count
1563+
, forwarded_fetch_count
1564+
, lob_fetch_in_pages
1565+
, lob_fetch_in_bytes
1566+
, row_overflow_fetch_in_pages
1567+
, row_overflow_fetch_in_bytes
1568+
, row_lock_count
1569+
, row_lock_wait_count
1570+
, row_lock_wait_in_ms
1571+
, page_lock_count
1572+
, page_lock_wait_count
1573+
, page_lock_wait_in_ms
1574+
, index_lock_promotion_attempt_count
1575+
, index_lock_promotion_count
1576+
, page_latch_wait_count
1577+
, page_latch_wait_in_ms
1578+
, page_io_latch_wait_count
1579+
, page_io_latch_wait_in_ms
1580+
)
1581+
1582+
select os.database_id
1583+
, os.object_id
1584+
, os.index_id
1585+
, os.partition_number
1586+
, os.hobt_id
1587+
, os.leaf_insert_count
1588+
, os.leaf_delete_count
1589+
, os.leaf_update_count
1590+
, os.range_scan_count
1591+
, os.singleton_lookup_count
1592+
, os.forwarded_fetch_count
1593+
, os.lob_fetch_in_pages
1594+
, os.lob_fetch_in_bytes
1595+
, os.row_overflow_fetch_in_pages
1596+
, os.row_overflow_fetch_in_bytes
1597+
, os.row_lock_count
1598+
, os.row_lock_wait_count
1599+
, os.row_lock_wait_in_ms
1600+
, os.page_lock_count
1601+
, os.page_lock_wait_count
1602+
, os.page_lock_wait_in_ms
1603+
, os.index_lock_promotion_attempt_count
1604+
, os.index_lock_promotion_count
1605+
, os.page_latch_wait_count
1606+
, os.page_latch_wait_in_ms
1607+
, os.page_io_latch_wait_count
1608+
, os.page_io_latch_wait_in_ms
1609+
from ' + QUOTENAME(@DatabaseName) + N'.sys.dm_db_index_operational_stats('+ CAST(@DatabaseID AS NVARCHAR(10)) +', NULL, NULL,NULL) AS os
1610+
OPTION ( RECOMPILE , min_grant_percent = 1);
1611+
1612+
SET @d = CONVERT(VARCHAR(19), GETDATE(), 121)
1613+
RAISERROR (N''finished getting data into #dm_db_index_operational_stats at %s.'',0,1, @d) WITH NOWAIT;
15101614
';
15111615
END;
15121616
ELSE
@@ -1605,6 +1709,7 @@ BEGIN TRY
16051709
PRINT SUBSTRING(@dsql, 32000, 36000);
16061710
PRINT SUBSTRING(@dsql, 36000, 40000);
16071711
END;
1712+
EXEC sp_executesql @dsql;
16081713
INSERT #IndexPartitionSanity ( [database_id],
16091714
[object_id],
16101715
[schema_name],
@@ -1639,8 +1744,35 @@ BEGIN TRY
16391744
page_io_latch_wait_count,
16401745
page_io_latch_wait_in_ms,
16411746
reserved_dictionary_MB)
1642-
EXEC sp_executesql @dsql;
1643-
1747+
select h.database_id, h.object_id, h.sname, h.index_id, h.partition_number, h.row_count, h.reserved_MB, h.reserved_LOB_MB, h.reserved_row_overflow_MB, h.lock_escalation_desc, h.data_compression_desc,
1748+
SUM(os.leaf_insert_count),
1749+
SUM(os.leaf_delete_count),
1750+
SUM(os.leaf_update_count),
1751+
SUM(os.range_scan_count),
1752+
SUM(os.singleton_lookup_count),
1753+
SUM(os.forwarded_fetch_count),
1754+
SUM(os.lob_fetch_in_pages),
1755+
SUM(os.lob_fetch_in_bytes),
1756+
SUM(os.row_overflow_fetch_in_pages),
1757+
SUM(os.row_overflow_fetch_in_bytes),
1758+
SUM(os.row_lock_count),
1759+
SUM(os.row_lock_wait_count),
1760+
SUM(os.row_lock_wait_in_ms),
1761+
SUM(os.page_lock_count),
1762+
SUM(os.page_lock_wait_count),
1763+
SUM(os.page_lock_wait_in_ms),
1764+
SUM(os.index_lock_promotion_attempt_count),
1765+
SUM(os.index_lock_promotion_count),
1766+
SUM(os.page_latch_wait_count),
1767+
SUM(os.page_latch_wait_in_ms),
1768+
SUM(os.page_io_latch_wait_count),
1769+
SUM(os.page_io_latch_wait_in_ms)
1770+
,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
1771+
from #dm_db_partition_stats_etc h
1772+
left JOIN #dm_db_index_operational_stats as os ON
1773+
h.object_id=os.object_id and h.index_id=os.index_id and h.partition_number=os.partition_number
1774+
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
1775+
16441776
END; --End Check For @SkipPartitions = 0
16451777

16461778

@@ -6178,7 +6310,8 @@ BEGIN
61786310

61796311

61806312
END; /* End @Mode=3 (index detail)*/
6181-
6313+
SET @d = CONVERT(VARCHAR(19), GETDATE(), 121);
6314+
RAISERROR (N'finishing at %s',0,1, @d) WITH NOWAIT;
61826315
END /* End @TableName IS NULL (mode 0/1/2/3/4) */
61836316
END TRY
61846317

0 commit comments

Comments
 (0)