|
| 1 | +/* |
| 2 | +<documentation> |
| 3 | + <summary>Track progrees CREATE or ALTER Index commands</summary> |
| 4 | + <returns>1 data set</returns> |
| 5 | + <issues>No</issues> |
| 6 | + <author>Solomon Rutzky</author> |
| 7 | + <created>2019-12-02</created> |
| 8 | + <modified>2019-12-02</modified> |
| 9 | + <version>1.0</version> |
| 10 | + <originalLink>https://dba.stackexchange.com/a/139225/107045</originalLink> |
| 11 | + <sourceLink>https://github.com/ktaranov/sqlserver-kit/blob/master/Scripts/Get_Create_or_Alter_Index_Progress.sql</sourceLink> |
| 12 | +</documentation> |
| 13 | +*/ |
| 14 | + |
| 15 | +DECLARE @SPID int = 51; |
| 16 | + |
| 17 | +WITH agg AS |
| 18 | +( |
| 19 | + SELECT SUM(qp.[row_count]) AS [RowsProcessed], |
| 20 | + SUM(qp.[estimate_row_count]) AS [TotalRows], |
| 21 | + MAX(qp.last_active_time) - MIN(qp.first_active_time) AS [ElapsedMS], |
| 22 | + MAX(IIF(qp.[close_time] = 0 AND qp.[first_row_time] > 0, |
| 23 | + [physical_operator_name], |
| 24 | + N'<Transition>')) AS [CurrentStep] |
| 25 | + FROM sys.dm_exec_query_profiles qp |
| 26 | + WHERE qp.[physical_operator_name] IN (N'Table Scan', N'Clustered Index Scan', |
| 27 | + N'Index Scan', N'Sort') |
| 28 | + AND qp.[session_id] = @SPID |
| 29 | +), comp AS |
| 30 | +( |
| 31 | + SELECT *, |
| 32 | + ([TotalRows] - [RowsProcessed]) AS [RowsLeft], |
| 33 | + ([ElapsedMS] / 1000.0) AS [ElapsedSeconds] |
| 34 | + FROM agg |
| 35 | +) |
| 36 | +SELECT [CurrentStep], |
| 37 | + [TotalRows], |
| 38 | + [RowsProcessed], |
| 39 | + [RowsLeft], |
| 40 | + CONVERT(DECIMAL(5, 2), |
| 41 | + (([RowsProcessed] * 1.0) / [TotalRows]) * 100) AS [PercentComplete], |
| 42 | + [ElapsedSeconds], |
| 43 | + (([ElapsedSeconds] / [RowsProcessed]) * [RowsLeft]) AS [EstimatedSecondsLeft], |
| 44 | + DATEADD(SECOND, |
| 45 | + (([ElapsedSeconds] / [RowsProcessed]) * [RowsLeft]), |
| 46 | + GETDATE()) AS [EstimatedCompletionTime] |
| 47 | +FROM comp; |
0 commit comments