Skip to content

Commit 7f6ca22

Browse files
Update StatsSampled/RowsSampled columns during maintenance
1 parent bcd0562 commit 7f6ca22

File tree

2 files changed

+70
-17
lines changed

2 files changed

+70
-17
lines changed

Server/Query.cs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -721,6 +721,55 @@ FROM sys.partitions WITH(NOLOCK)
721721
, RowsCount = ISNULL(@RowsCount, 0)
722722
, IndexStats = DATEADD(MINUTE, -DATEDIFF(MINUTE, GETUTCDATE(), GETDATE()), STATS_DATE({0}, {1}))
723723
, DataCompression = @Compression
724+
, StatsSampled = NULL
725+
, RowsSampled = NULL
726+
FROM sys.dm_db_index_physical_stats(@DBID, {0}, {1}, {2}, '{3}')
727+
WHERE [index_level] = 0
728+
AND [alloc_unit_type_desc] = 'IN_ROW_DATA'
729+
";
730+
731+
public const string AfterFixIndexWithStats = @"
732+
DECLARE @UnusedPagesCount BIGINT
733+
, @ReservedPageCount BIGINT
734+
, @RowsCount BIGINT
735+
, @Compression TINYINT
736+
, @DBID INT
737+
, @DBNAME SYSNAME
738+
, @StatsSampled FLOAT
739+
, @RowsSampled BIGINT
740+
741+
SET @DBNAME = DB_NAME()
742+
SELECT @DBID = [database_id]
743+
FROM sys.databases WITH(NOLOCK)
744+
WHERE [name] = @DBNAME
745+
746+
SELECT TOP(1) @UnusedPagesCount = [used_page_count]
747+
, @ReservedPageCount = [reserved_page_count]
748+
, @RowsCount = [row_count]
749+
FROM sys.dm_db_partition_stats WITH(NOLOCK)
750+
WHERE [object_id] = {0}
751+
AND [index_id] = {1}
752+
AND [partition_number] = {2}
753+
754+
SELECT @Compression = [data_compression]
755+
FROM sys.partitions WITH(NOLOCK)
756+
WHERE [object_id] = {0}
757+
AND [index_id] = {1}
758+
AND [partition_number] = {2}
759+
760+
SELECT TOP(1) @StatsSampled = [rows_sampled] * 100. / NULLIF([rows], 0)
761+
, @RowsSampled = [rows_sampled]
762+
FROM sys.dm_db_stats_properties({0}, {1})
763+
764+
SELECT Fragmentation = [avg_fragmentation_in_percent]
765+
, PageSpaceUsed = [avg_page_space_used_in_percent]
766+
, PagesCount = ISNULL(@ReservedPageCount, 0)
767+
, UnusedPagesCount = ISNULL(CASE WHEN ABS(@ReservedPageCount - @UnusedPagesCount) > 32 THEN @ReservedPageCount - @UnusedPagesCount END, 0)
768+
, RowsCount = ISNULL(@RowsCount, 0)
769+
, IndexStats = DATEADD(MINUTE, -DATEDIFF(MINUTE, GETUTCDATE(), GETDATE()), STATS_DATE({0}, {1}))
770+
, DataCompression = @Compression
771+
, StatsSampled = @StatsSampled
772+
, RowsSampled = @RowsSampled
724773
FROM sys.dm_db_index_physical_stats(@DBID, {0}, {1}, {2}, '{3}')
725774
WHERE [index_level] = 0
726775
AND [alloc_unit_type_desc] = 'IN_ROW_DATA'
@@ -746,6 +795,8 @@ FROM sys.fn_column_store_row_groups({0})
746795
, RowsCount = ISNULL([rows], 0)
747796
, IndexStats = NULL
748797
, DataCompression = [data_compression]
798+
, StatsSampled = NULL
799+
, RowsSampled = NULL
749800
FROM sys.partitions WITH(NOLOCK)
750801
WHERE [object_id] = {0}
751802
AND [index_id] = {1}

Server/QueryEngine.cs

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -358,20 +358,23 @@ public static void GetColumnstoreFragmentation(SqlConnection connection, Index i
358358

359359
public static string FixIndex(SqlConnection connection, Index ix) {
360360
int indexId = ix.FixType == IndexOp.CREATE_COLUMNSTORE_INDEX && ix.IndexType == IndexType.HEAP ? 1 : ix.IndexId;
361-
362-
string sqlInfo = string.Format(ix.IsColumnstore ? Query.AfterFixColumnstoreIndex : Query.AfterFixIndex,
363-
ix.ObjectId, indexId, ix.PartitionNumber, Settings.Options.ScanMode);
364-
361+
string sql;
365362
string query = ix.GetQuery();
366-
string sql = ix.FixType == IndexOp.DISABLE_INDEX
367-
|| ix.FixType == IndexOp.DROP_INDEX
368-
|| ix.FixType == IndexOp.DROP_TABLE
369-
|| ix.FixType == IndexOp.CREATE_INDEX
370-
|| ix.FixType == IndexOp.UPDATE_STATISTICS_FULL
371-
|| ix.FixType == IndexOp.UPDATE_STATISTICS_RESAMPLE
372-
|| ix.FixType == IndexOp.UPDATE_STATISTICS_SAMPLE
373-
? query
374-
: $"{query} \n {sqlInfo}";
363+
364+
if (ix.FixType == IndexOp.DISABLE_INDEX || ix.FixType == IndexOp.DROP_INDEX || ix.FixType == IndexOp.DROP_TABLE || ix.FixType == IndexOp.CREATE_INDEX) {
365+
sql = query;
366+
}
367+
else {
368+
string sqlInfo;
369+
if (ix.IsColumnstore)
370+
sqlInfo = Query.AfterFixColumnstoreIndex;
371+
else if (Settings.ServerInfo.IsFullStats && (ix.IndexType == IndexType.CLUSTERED || ix.IndexType == IndexType.NONCLUSTERED) && !ix.IsPartitioned)
372+
sqlInfo = Query.AfterFixIndexWithStats;
373+
else
374+
sqlInfo = Query.AfterFixIndex;
375+
376+
sql = $"{query} \n {string.Format(sqlInfo, ix.ObjectId, indexId, ix.PartitionNumber, Settings.Options.ScanMode)}";
377+
}
375378

376379
SqlCommand cmd = new SqlCommand(sql, connection) { CommandTimeout = Settings.Options.CommandTimeout };
377380
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
@@ -386,10 +389,7 @@ public static string FixIndex(SqlConnection connection, Index ix) {
386389

387390
if (string.IsNullOrEmpty(ix.Error)) {
388391
try {
389-
if (ix.FixType == IndexOp.UPDATE_STATISTICS_FULL || ix.FixType == IndexOp.UPDATE_STATISTICS_RESAMPLE || ix.FixType == IndexOp.UPDATE_STATISTICS_SAMPLE) {
390-
ix.IndexStats = DateTime.UtcNow;
391-
}
392-
else if (ix.FixType == IndexOp.CREATE_INDEX) {
392+
if (ix.FixType == IndexOp.CREATE_INDEX) {
393393
ix.IndexStats = DateTime.UtcNow;
394394
ix.Fragmentation = 0;
395395
}
@@ -411,6 +411,8 @@ public static string FixIndex(SqlConnection connection, Index ix) {
411411
ix.RowsCount = row.Field<long>(Resources.RowsCount);
412412
ix.DataCompression = ((DataCompression)row.Field<byte>(Resources.DataCompression));
413413
ix.IndexStats = row.Field<DateTime?>(Resources.IndexStats);
414+
ix.StatsSampled = row.Field<double?>(Resources.StatsSampled) ?? ix.StatsSampled;
415+
ix.RowsSampled = row.Field<long?>(Resources.RowsSampled) ?? ix.RowsSampled;
414416

415417
if (ix.FixType == IndexOp.CREATE_COLUMNSTORE_INDEX) {
416418
ix.IndexName = "CCL";

0 commit comments

Comments
 (0)