@@ -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
724773FROM sys.dm_db_index_physical_stats(@DBID, {0}, {1}, {2}, '{3}')
725774WHERE [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
749800FROM sys.partitions WITH(NOLOCK)
750801WHERE [object_id] = {0}
751802 AND [index_id] = {1}
0 commit comments