在SQL Server中重建索引(Rebuild Index)与重组索引(Reorganize Index)会触发统计信息更新吗? 那么咱们先来测试、验证一下:算法
咱们以AdventureWorks2014为测试环境,以下所示:sql
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)
ALTER INDEX IX_Person_LastName_FirstName_MiddleName ON Person.Person REORGANIZE;
ALTER INDEX PK_Person_BusinessEntityID ON Person.Person REORGANIZE;
重组索引(Reorganize Index)后,验证发现,索引重组不会触发索引对应的统计信息更新。验证发现其不会触发任何统计信息更新。app
结论:重组索引(Reorganize Index)不会触发对应索引的统计信息更新. 也不会触发其它统计信息更新。也就说,重组索引(Reorganize Index)不会触发任何统计信息更新。性能
那么重建索引(Rebuild Index)会更新对应的统计信息吗? 你能够测试、验证一下:以下所示,索引重建后,索引对应的统计信息更新了。测试
ALTER INDEX PK_Person_BusinessEntityID ON Person.Person REBUILD;优化
结论:重建索引(Rebuild Index)会触发对应索引的统计信息更新。可是,重建索引(Rebuild Index)不会触发其它统计信息更新。ui
重建索引会触发对应索引的统计信息更新,那么统计信息更新的采样比例是多少? 根据测试验证,采样比例为100%,如上截图所示,也就说索引重建使用WITH FULLSCAN更新索引统计信息. 若是表是分区表呢?分区表的分区索引使用默认采样算法(default sampling rate),对于这个默认采样算法,没有找到详细的官方资料。spa
官方文档:https://docs.microsoft.com/zh-cn/sql/relational-databases/partitions/partitioned-tables-and-indexes?view=sql-server-ver15里面有简单介绍:code
已分区索引操做期间统计信息计算中的行为更改
从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.