SQL Server 2014,标准版
我已经读过 dm_exec_requests 中的 percent_complete 不适用于 CREATE INDEX,实际上,percent_complete 始终为 0。所以这无济于事。
我目前使用下面的方法,它至少向我展示了运动(索引创建没有被阻止)。但我对我是 %10 还是 %99 的过程一无所知。
我怎样才能得到线索?
SELECT percent_complete, estimated_completion_time, reads, writes, logical_reads, text_size, * FROM sys.dm_exec_requests AS r WHERE r.session_id <> @@SPID AND r.session_id = 58
我认为以下查询至少会让您非常接近。它利用了 SQL Server 2014 中引入的 DMV:sys.dm_exec_query_profiles
请注意:
SET STATISTICS PROFILE ON;
SET STATISTICS XML ON;``CREATE INDEX``CREATE INDEX``session_id
IN
Index Insert
TotalRows
row_count
ALTER TABLE [schema_name].[table_name] REBUILD;
ALTER INDEX ALL ON [schema_name].[table_name] REBUILD;
ALTER INDEX [index_name] ON [schema_name].[table_name] REBUILD;
DECLARE @SPID INT = 51; ;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'Index Scan', N'Sort') 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;
样本输出:
Rows Percent Elapsed Estimated Estimated CurrentStep TotalRows Processed RowsLeft Complete Seconds SecondsLeft CompletionTime ----------- --------- --------- -------- -------- ------- ----------- -------------- Clustered 11248640 4786937 6461703 42.56 4.89400 6.606223 2016-05-23 Index Scan 14:32:40.547