Sample Header Ad - 728x90

SQL Server - Maintenance - How to obtain duration per database from the commandlog table

0 votes
1 answer
198 views
Attempting to obtain the duration for individual maintenance operations per database in the commandlog table which is available in Ola Hallengren's maintenance scripts enter image description here If anyone has a better solution to this, I'd love to hear from you. set dateformat mdy DECLARE @months AS int = -3 /* DBCC_CHECKDB,UPDATE_STATISTICS,ALTER_INDEX,BACKUP_DATABASE,BACKUP_LOG */ DECLARE @operation AS varchar(50) = 'BACKUP_DATABASE' DECLARE @datbasename as varchar(255) = 'Logs' SELECT @operation DECLARE @commandsql as varchar(max) set @commandsql = ' ;WITH reindex_table as ( SELECT TOP (1000) [DatabaseName] ,[StartTime] ,[EndTime] ,DATEDIFF(MI,StartTime,EndTime) as duration ,convert(varchar, StartTime, 23) as operationdate ,ROW_NUMBER() over (PARTITION by convert(varchar, StartTime, 23) order by convert(varchar, StartTime, 23) desc) ''rownumber'' FROM [_DBA_Tools].[dbo].[CommandLog] where 1=1 And CommandType = '''+ @operation +''' And DatabaseName = ''' + @datbasename + ''' and StartTime >= DATEADD(MONTH, '+cast(@months as varchar(100))+', GETDATE()) and StartTime 1 group by operationdate, DatabaseName order by operationdate ' PRINT @commandsql EXEC (@commandsql) The commandlog doesn't have an id that regroups operations that were all run from the same job execution.
Asked by user4659
Aug 20, 2022, 01:45 PM
Last activity: Jun 28, 2025, 06:11 AM