代码改变世界

SQL Server重建索引与重组索引会更新统计信息吗?

2019-10-15 08:58  潇湘隐者  阅读(2130)  评论(6编辑  收藏  举报

在SQL Server中重建索引(Rebuild Index)与重组索引(Reorganize Index)会触发统计信息更新吗? 那么我们先来测试、验证一下:

 

我们以AdventureWorks2014为测试环境,如下所示:

 

Person.Person表的统计信息最后一次更新为2014-07-17 16:11:31,如下截图所示:

 

 
DECLARE @table_name NVARCHAR(32);
SET @table_name='Person.Person'
SELECT sch.name + '.' + so.name AS table_name
      , so.object_id
      , ss.name  AS stat_name
      , ds.stats_id
      , ds.last_updated
      , ds.rows
      , ds.rows_sampled
      , ds.rows_sampled*1.0/ds.rows *100 AS sample_rate
      , ds.steps
      , ds.unfiltered_rows
      --, ds.persisted_sample_percent
      , ds.modification_counter 
      , 'UPDATE STATISTICS ' + QUOTENAME(DB_NAME()) + '.' + QUOTENAME(sch.name) + '.' + QUOTENAME( so.name) + ' "' +  RTRIM(LTRIM(ss.name)) + '" WITH SAMPLE 80 PERCENT;'
        AS update_stat_script
FROM sys.stats ss
JOIN sys.objects so ON ss.object_id = so.object_id
JOIN sys.schemas sch ON so.schema_id = sch.schema_id
CROSS APPLY sys.dm_db_stats_properties(ss.object_id,ss.stats_id) ds
WHERE  so.is_ms_shipped = 0 
        AND so.object_id NOT IN (
        SELECT  major_id
        FROM    sys.extended_properties (NOLOCK)
        WHERE   name = N'microsoft_database_tools_support' )
        AND so.object_id =OBJECT_ID(@table_name)

 

clip_image001

 

 

ALTER INDEX IX_Person_LastName_FirstName_MiddleName ON Person.Person REORGANIZE;
 
 
 
ALTER INDEX PK_Person_BusinessEntityID ON Person.Person REORGANIZE;

 

 

重组索引(Reorganize Index)后,验证发现,索引重组不会触发索引对应的统计信息更新。验证发现其不会触发任何统计信息更新。

 

 

 

clip_image002

 

 

结论:重组索引(Reorganize Index)不会触发对应索引的统计信息更新. 也不会触发其它统计信息更新。也就说,重组索引(Reorganize Index)不会触发任何统计信息更新。

 

 

那么重建索引(Rebuild Index)会更新对应的统计信息吗? 你可以测试、验证一下:如下所示,索引重建后,索引对应的统计信息更新了。

 

 

ALTER INDEX PK_Person_BusinessEntityID ON Person.Person REBUILD;

 

 

 

clip_image003

 

 

 

结论:重建索引(Rebuild Index)会触发对应索引的统计信息更新。但是,重建索引(Rebuild Index)不会触发其它统计信息更新。

 

 

 

 

 

重建索引会触发对应索引的统计信息更新,那么统计信息更新的采样比例是多少? 根据测试验证,采样比例为100%,如上截图所示,也就说索引重建使用WITH FULLSCAN更新索引统计信息. 如果表是分区表呢?分区表的分区索引使用默认采样算法(default sampling rate),对于这个默认采样算法,没有找到详细的官方资料。

 

官方文档:https://docs.microsoft.com/zh-cn/sql/relational-databases/partitions/partitioned-tables-and-indexes?view=sql-server-ver15里面有简单介绍:

 

 

已分区索引操作期间统计信息计算中的行为更改

 

从SQL Server 2012 (11.x)开始,当创建或重新生成已分区索引时,不会通过扫描表中的所有行来创建统计信息。 相反,查询优化器使用默认采样算法来生成统计信息。 在升级具有已分区索引的数据库后,您可以在直方图数据中注意到针对这些索引的差异。 此行为更改可能不会影响查询性能。 若要通过扫描表中所有行的方法获得有关已分区索引的统计信息,请使用 CREATE STATISTICS  UPDATE STATISTICS 以及 FULLSCAN 子句。

 

Starting with SQL Server 2012 (11.x), statistics are not created by scanning all the rows in the table when a partitioned index is created or rebuilt. Instead, the query optimizer uses the default sampling algorithm to generate statistics. After upgrading a database with partitioned indexes, you may notice a difference in the histogram data for these indexes. This change in behavior may not affect query performance. To obtain statistics on partitioned indexes by scanning all the rows in the table, use CREATE STATISTICS or UPDATE STATISTICS with the FULLSCAN clause.