Skip to content

Commit d92404a

Browse files
committed
better comments around incremental stats compatiblity
1 parent 413fcb8 commit d92404a

File tree

2 files changed

+23
-7
lines changed

2 files changed

+23
-7
lines changed

stats out of date whileloop.sql

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,24 @@ declare @tsql nvarchar(max) =
1515
-- , Object_Type = ISNULL(i.type_desc + '' Index'', ''Statistics Object'')
1616
-- , Stats_Last_Updated = ISNULL(sp.last_updated, o.create_date)
1717
-- , Rows_Changed = ISNULL(sp.modification_counter,0) --Rows Changed since last update
18-
-- , PartitionNumber = CASE WHEN MAX(p.partition_number) OVER (PARTITION by STA.name, i.name) > 1 THEN p.partition_number ELSE null END
19-
---- , STA.is_incremental --Only works in SQL 2014+, comment out this line in prior versions.
20-
-- , ''[?]'' AS [?],
18+
--Below block only works in SQL 2014+, comment out this line in prior versions.
19+
/*
20+
, PartitionNumber = CASE WHEN MAX(p.partition_number) OVER (PARTITION by STA.name, i.name) > 1 THEN p.partition_number ELSE null END
21+
, STA.is_incremental --Only works in SQL 2014+, comment out this line in prior versions.
22+
*/
23+
-- , ''use [?]'' AS [?],
2124
TSQL = CASE WHEN i.type_desc like ''%columnstore%'' THEN NULL ELSE
2225
N''USE [?]; '' +
2326
N''UPDATE STATISTICS ''
2427
+ QUOTENAME(s.name) + N''.'' + QUOTENAME(o.name) + N'' ''
2528
+ QUOTENAME(STA.name) + N'' ''
2629
+ ''WITH RESAMPLE''
30+
--Below block only works in SQL 2014+, comment out this line in prior versions.
31+
/*
2732
+ CASE WHEN
2833
STA.Is_Incremental = 1 and --Only works in SQL 2014+, comment out this line in prior versions.
2934
MAX(p.partition_number) OVER (PARTITION by STA.name, i.name) > 1 THEN '' ON PARTITIONS ('' + cast(p.partition_number as varchar(5)) + '') '' ELSE '''' END
30-
35+
*/
3136
END
3237
FROM sys.objects o
3338
INNER JOIN sys.stats STA ON STA.object_id = o.object_id
@@ -37,12 +42,15 @@ declare @tsql nvarchar(max) =
3742
LEFT OUTER JOIN sys.indexes as i
3843
on i.index_id = STA.stats_id
3944
and (i.type_desc not like ''%columnstore%'')
45+
--Below block only works in SQL 2014+, comment out this line in prior versions.
46+
/*
4047
LEFT OUTER join sys.dm_db_partition_stats p
4148
on (
4249
STA.Is_Incremental = 1 and --Only works in SQL 2014+, comment out this line in prior versions.
4350
p.object_id = o.object_id and
4451
i.index_id = p.index_id
4552
)
53+
*/
4654
LEFT JOIN
4755
(SELECT IUS.object_id
4856
,MIN(ISNULL(IUS.last_user_update, IUS.last_system_update)) AS LastUpdate

stats out of date.sql

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,22 @@
1515
, Object_Type = ISNULL(i.type_desc + ' Index', 'Statistics Object')
1616
, Stats_Last_Updated = ISNULL(sp.last_updated, o.create_date)
1717
, Rows_Changed = ISNULL(sp.modification_counter,0) --Rows Changed since last update
18+
--Below block only works in SQL 2014+, comment out this line in prior versions.
19+
/*
1820
, PartitionNumber = CASE WHEN MAX(p.partition_number) OVER (PARTITION by STA.name, i.name) > 1 THEN p.partition_number ELSE null END
1921
, STA.is_incremental --Only works in SQL 2014+, comment out this line in prior versions.
22+
*/
2023
, TSQL = CASE WHEN i.type_desc like '%columnstore%' THEN NULL ELSE
2124
N'UPDATE STATISTICS '
2225
+ QUOTENAME(s.name) + N'.' + QUOTENAME(o.name) + N' '
2326
+ QUOTENAME(STA.name) + N' '
2427
+ 'WITH RESAMPLE'
28+
--Below block only works in SQL 2014+, comment out this line in prior versions.
29+
/*
2530
+ CASE WHEN
26-
-- STA.Is_Incremental = 1 and --Only works in SQL 2014+, comment out this line in prior versions.
31+
STA.Is_Incremental = 1 and
2732
MAX(p.partition_number) OVER (PARTITION by STA.name, i.name) > 1 THEN ' ON PARTITIONS (' + cast(p.partition_number as varchar(5)) + ') ' ELSE '' END
28-
33+
*/
2934
END
3035
FROM sys.objects o
3136
INNER JOIN sys.stats STA ON STA.object_id = o.object_id
@@ -35,12 +40,15 @@
3540
LEFT OUTER JOIN sys.indexes as i
3641
on i.index_id = STA.stats_id
3742
and (i.type_desc not like '%columnstore%')
38-
LEFT OUTER join sys.dm_db_partition_stats p
43+
--Below block only works in SQL 2014+, comment out this line in prior versions.
44+
/*
45+
LEFT OUTER join sys.dm_db_partition_stats p
3946
on (
4047
STA.Is_Incremental = 1 and --Only works in SQL 2014+, comment out this line in prior versions.
4148
p.object_id = o.object_id and
4249
i.index_id = p.index_id
4350
)
51+
*/
4452
LEFT JOIN
4553
(SELECT IUS.object_id
4654
,MIN(ISNULL(IUS.last_user_update, IUS.last_system_update)) AS LastUpdate

0 commit comments

Comments
 (0)