terça-feira, 16 de janeiro de 2018

Verificando tempo de execução no SQL Server


SELECT
 command, s.text, start_time, percent_complete, 
         RIGHT('0' + Cast((Datediff(s, start_time, Getdate())/3600) AS VARCHAR) + ':', 3) 
       + RIGHT('0' + Cast((Datediff(s, start_time, Getdate())%3600)/60 AS VARCHAR) + ':', 3) 
       + RIGHT('0' + Cast((Datediff(s, start_time, Getdate())%60) AS VARCHAR), 2 
       ) AS running_time, 
         RIGHT('0' + Cast((estimated_completion_time/3600000) AS VARCHAR) + ':', 3 ) 
       + RIGHT('0' + Cast((estimated_completion_time %3600000)/60000 AS VARCHAR) + ':', 3) 
       + RIGHT('0' + Cast((estimated_completion_time %60000)/1000 AS VARCHAR), 3       
       )
 AS est_time_to_go, 
       Dateadd(second, estimated_completion_time / 1000, Getdate()) 
       AS est_completion_time 
FROM   sys.dm_exec_requests r 
       CROSS apply sys.Dm_exec_sql_text(r.sql_handle) s 
WHERE  r.command IN ( 'RESTORE DATABASE', 'BACKUP DATABASE', 'RESTORE LOG', 
                      'BACKUP LOG','DbccFilesCompact' )