SQL Server查看索引重建、重组索引进度
相信很多sql server dba或开发人员在重建或重组大表索引时,都会相当郁闷,不知道索引重建的进度,这个对于dba完全是一个黑盒子,对于系统负载非常大的系统或维护窗口较短的系统,你会遇到一些挑战。例如,你创建索引的时候,很多会话被阻塞,你只能取消创建索引的任务。查看这些索引维护操作的进度、预估时间对于我们有较大的意义,需要根据这个做一些决策。下面我们来看看看看如何获取create index、alter index rebuild、alter 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: |
测试环境: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;
索引重建
上面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 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/
上一篇: C语言字符串操作
下一篇: C语言:时间复杂度为O(1)的两则小程序