Quantcast
Channel: SQL Server 2005 – SQL Steve
Viewing all articles
Browse latest Browse all 17

Long Running Task Progress Report

$
0
0

There are many operations that take a long time to execute in SQL Server including the following:

 

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

If you are interested the progress made on any one of the above tasks, you can execute the following query:

 

/*
    Author:        Steve Abraham
    Date:        12/7/2011
    Purpose:    List estimated completion times for processes that report progress
*/
 
use master
GO
 
SELECT
    command                                                        AS CommandType,
    [text]                                                        AS CommandText,
    start_time                                                    AS StartTime,
    DATEADD(MILLISECOND, estimated_completion_time, GETDATE())    AS EstimatedCompletionTime,
    percent_complete                                            AS PercentComplete,
    CONVERT(TIME, CONVERT(CHAR(4), DATEDIFF(s, start_time, GETDATE()) / 3600) + ‘:’ + CONVERT(CHAR(4), DATEDIFF(s, start_time, GETDATE()) % 3600 / 60) + ‘:’ + CONVERT(CHAR(4), DATEDIFF(s, start_time, GETDATE()) % 60))    AS RunningTime,
    CONVERT(TIME, CONVERT(CHAR(4), estimated_completion_time / 3600000) + ‘:’ + CONVERT(CHAR(4), estimated_completion_time % 3600000 / 60000) + ‘:’ + CONVERT(CHAR(4), estimated_completion_time % 60000 / 1000))            AS RemainingTime,
    r.wait_type        AS WaitType,
    r.wait_resource    AS WaitResource,
    CONVERT(TIME, CONVERT(CHAR(4), wait_time / 3600000) + ‘:’ + CONVERT(CHAR(4), wait_time % 3600000 / 60000) + ‘:’ + CONVERT(CHAR(4), wait_time % 60000 / 1000))    AS WaitTime
FROM
    sys.dm_exec_requests AS r
    CROSS APPLY
    sys.dm_exec_sql_text(r.sql_handle) s
WHERE
    percent_complete > 0
GO

This will return a result set with the following values:

 

  • CommandType
  • CommandText
  • StartTime
  • EstimatedCompletionTime
  • PercentComplete
  • RunningTime
  • RemainingTime
  • WaitType
  • WaitResource
  • WaitTime

I hope you find this useful.  Please feel free to ping me with any questions.


Filed under: Denali, SQL Server 2005, SQL Server 2008, SQL Server Programming, T-SQL Tagged: Backup, DBCC, Defragment, Denali, Encryption, Progress, RESTORE, Rollback, SQL Server

Viewing all articles
Browse latest Browse all 17

Trending Articles