SQL Server查看索引重建、重组索引进度

栏目: 数据库 · 发布时间: 5年前

相信很多 SQL Server DBA 或开发人员在重建或重组大表索引时,都会相当郁闷,不知道索引重建的进度,这个对于 DBA 完全是一个黑盒子,对于系统负载非常大的系统或维护窗口较短的系统,你会遇到一些挑战。例如,你创建索引的时候,很多会话被阻塞,你只能取消创建索引的任务。查看这些索引维护操作的进度、预估时间对于我们有较大的意义,需要根据这个做一些决策。下面我们来看看看看如何获取 CREATE INDEXALTER INDEX REBUILDALTER INDEX ORGANIZE 的进度。

索引重组

SQL Server 2008 开始,有个 DMV 视图 sys.dm_exec_requests ,里面有个字段 percent_complete 表示以下命令完成的工作的百分比,这里面就包括索引重组( ALTER INDEX REORGANIZE ),这其中不包括 ALTER INDEX REBUILD ,可以查看索引重组( ALTER INDEX ORGANIZE )完成的百分比。也就是说在 SQL Server 2008 之前是无法获取索引重组的进度情况的。

percent_complete

real

Percentage of work completed for the following commands:
ALTER INDEX REORGANIZE
AUTO_SHRINK option with ALTER DATABASE
BACKUP DATABASE
DBCC CHECKDB
DBCC CHECKFILEGROUP
DBCC CHECKTABLE
DBCC INDEXDEFRAG
DBCC SHRINKDATABASE
DBCC SHRINKFILE
RECOVERY
RESTORE DATABASE
ROLLBACK
TDE ENCRYPTION
Is not nullable.

测试环境: SQL Server 2008 2017 RTM CU13

SELECT  er.session_id ,
        er.blocking_session_id ,
        er.status ,
        er.command ,
        DB_NAME(er.database_id) DB_name ,
        er.wait_type ,
        et.text SQLText ,
        er.percent_complete
FROM    sys.dm_exec_requests er
        CROSS APPLY sys.dm_exec_sql_text(er.sql_handle) et
WHERE   er.session_id = 57
        AND er.session_id <> @@SPID;

SQL Server查看索引重建、重组索引进度

索引重建

上面 DMV 视图 sys.dm_exec_requests 是否也可以查看索引重建的进度呢 ? 答案是不行,测试发现 percent_complete 这个进度一直为 0 ,那么要如何查看索引重建( INDEX REBUILD )的进度呢?

不过自 SQL Server 2014 开始, SQL Server 提供了一个新特性: sys.dm_exec_query_profiles ,它可以实时监控正在执行的查询的进度情况( Monitors real time query progress while the query is in execution )。当然,需要启用实时查询监控才行。一般只需启用会话级别的实时查询监控,可以通过启用 SET STATISTICS XML ON;   SET STATISTICS PROFILE ON; 开启。而从 SQL Server 2016 (13.x)SP1 开始,您可以或者开启跟踪标志 7412 或使用 query_thread_profile 扩展的事件。下面是官方文档的描述:

In SQL Server 2014 (12.x) SP2 and later use SET STATISTICS PROFILE ON or SET STATISTICS XML ON together with the query under investigation. This enables the profiling infrastructure and produces results in the DMV for the session where the SET command was executed. If you are investigating a query running from an application and cannot enable SET options with it, you can create an Extended Event using the query_post_execution_showplan event which will turn on the profiling infrastructure.

In SQL Server 2016 (13.x) SP1, you can either turn on trace flag 7412 or use the query_thread_profile extended event.

--Configure query for profiling with sys.dm_exec_query_profiles  

SET STATISTICS PROFILE ON;  

GO  

--Or enable query profiling globally under SQL Server 2016 SP1 or above  

DBCC TRACEON (7412, -1);  

GO

ALTER INDEX Your_Index_Name ON Your_Table_Name REBUILD ;

GO

DECLARE @SPID INT = 53;
 
;WITH agg AS
(
     SELECT SUM(qp.[row_count]) AS [RowsProcessed],
            SUM(qp.[estimate_row_count]) AS [TotalRows],
            MAX(qp.last_active_time) - MIN(qp.first_active_time) AS [ElapsedMS],
            MAX(IIF(qp.[close_time] = 0 AND qp.[first_row_time] > 0,
                    [physical_operator_name],
                    N'<Transition>')) AS [CurrentStep]
     FROM sys.dm_exec_query_profiles qp
     WHERE qp.[physical_operator_name] IN (N'Table Scan', N'Clustered Index Scan', N'Sort' , N'Index Scan')
     AND   qp.[session_id] = @SPID
), comp AS
(
     SELECT *,
            ([TotalRows] - [RowsProcessed]) AS [RowsLeft],
            ([ElapsedMS] / 1000.0) AS [ElapsedSeconds]
     FROM   agg
)
SELECT [CurrentStep],
       [TotalRows],
       [RowsProcessed],
       [RowsLeft],
       CONVERT(DECIMAL(5, 2),
               (([RowsProcessed] * 1.0) / [TotalRows]) * 100) AS [PercentComplete],
       [ElapsedSeconds],
       (([ElapsedSeconds] / [RowsProcessed]) * [RowsLeft]) AS [EstimatedSecondsLeft],
       DATEADD(SECOND,
               (([ElapsedSeconds] / [RowsProcessed]) * [RowsLeft]),
               GETDATE()) AS [EstimatedCompletionTime]
FROM   comp;
 

SQL Server查看索引重建、重组索引进度

注意事项: SQL Server 2016 SP1 之前,如果要使用 sys.dm_exec_query_profiles 查看索引重建的进度,那么就必须在索引重建之前设置 SET STATISTICS PROFILE ON or SET STATISTICS XML ON 。 而自

SQL Server 2016 SP1 之后,可以使用 DBCC TRACEON (7412, -1); 开启全局会话的跟踪标记,或者开启某个会话的跟踪标记,当然如果要使用 sys.dm_exec_query_profiles 查看索引重建的进度,也必须开启 7412 跟踪标记

,然后重建索引,否则也没有值。

注意事项: :索引重组时, sys.dm_exec_query_profiles 中没有数据。所以 sys.dm_exec_query_profiles 不能用来查看索引重组的进度。

新建索引

新建索引进度的查询,也可以使用下面 SQL 语句。这里不做展开。

DECLARE @SPID INT = 56;
 
;WITH agg AS
(
     SELECT SUM(qp.[row_count]) AS [RowsProcessed],
            SUM(qp.[estimate_row_count]) AS [TotalRows],
            MAX(qp.last_active_time) - MIN(qp.first_active_time) AS [ElapsedMS],
            MAX(IIF(qp.[close_time] = 0 AND qp.[first_row_time] > 0,
                    [physical_operator_name],
                    N'<Transition>')) AS [CurrentStep]
     FROM sys.dm_exec_query_profiles qp
     WHERE qp.[physical_operator_name] IN (N'Table Scan', N'Clustered Index Scan', N'Sort' , N'Index Scan')
     AND 
       qp.[session_id] = @SPID
), comp AS
(
     SELECT *,
            ([TotalRows] - [RowsProcessed]) AS [RowsLeft],
            ([ElapsedMS] / 1000.0) AS [ElapsedSeconds]
     FROM   agg
)
SELECT [CurrentStep],
       [TotalRows],
       [RowsProcessed],
       [RowsLeft],
       CONVERT(DECIMAL(5, 2),
               (([RowsProcessed] * 1.0) / [TotalRows]) * 100) AS [PercentComplete],
       [ElapsedSeconds],
       (([ElapsedSeconds] / [RowsProcessed]) * [RowsLeft]) AS [EstimatedSecondsLeft],
       DATEADD(SECOND,
               (([ElapsedSeconds] / [RowsProcessed]) * [RowsLeft]),
               GETDATE()) AS [EstimatedCompletionTime]
FROM   comp;
 
 
 
SELECT   
       node_id,
       physical_operator_name, 
       SUM(row_count) row_count, 
       SUM(estimate_row_count) AS estimate_row_count,
       CAST(SUM(row_count)*100 AS float)/SUM(estimate_row_count)  as estimate_percent_complete
FROM sys.dm_exec_query_profiles   
WHERE session_id=@SPID  
GROUP BY node_id,physical_operator_name  
ORDER BY node_id desc;

参考资料:

https://docs.microsoft.com/zh-cn/sql/relational-databases/system-dynamic-management-views/sys-dm-exec-query-profiles-transact-sql?view=sql-server-2017

https://docs.microsoft.com/en-us/sql/relational-databases/system-dynamic-management-views/sys-dm-exec-requests-transact-sql?view=sql-server-2017

https://dba.stackexchange.com/questions/139191/sql-server-how-to-track-progress-of-create-index-command

https://support.microsoft.com/zh-cn/help/4053291/fix-sys-dm-exec-query-profiles-dmv-returns-wrong-estimate-row-count-in

https://blogs.msdn.microsoft.com/sql_pfe_blog/2016/12/22/create-index-monitoring-progress/


以上所述就是小编给大家介绍的《SQL Server查看索引重建、重组索引进度》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

人工智能

人工智能

S. Russell、P. Norvig / 清华大学出版社 / 2006-5-1 / 128.00元

本书被全世界89个国家的900多所大学用作教材。 本书以详尽和丰富的资料,从理性智能体的角度,全面阐述了人工智能领域的核心内容,并深入介绍了各个主要的研究方向。全书分为8大部分:第一部分“人工智能”,第二部分“问题求解”,第三部分“知识与推理”,第四部分“规划”,第五部分“不确定知识与推理”,第六部分“学习”,第七部分“通信、感知与行动”,第八部分“结论”。本书既详细介绍了人工智能的基本概念......一起来看看 《人工智能》 这本书的介绍吧!

RGB转16进制工具
RGB转16进制工具

RGB HEX 互转工具

UNIX 时间戳转换
UNIX 时间戳转换

UNIX 时间戳转换

HEX CMYK 转换工具
HEX CMYK 转换工具

HEX CMYK 互转工具