1.1代碼檢查從昨天到現在,SQL代理Job有沒有運行失敗的,會把運行失敗的Job名字,步驟,運行時間,錯誤等級,錯誤原因羅列出來,方便查看,
----1.1 Check Job Fail List From Last Day To NowSELECT j.[name],h.step_id,h.step_name,h.run_date,h.run_time,h.sql_severity,h.message,h.serverFROM msdb.dbo.sysjobhistory hINNER JOIN msdb.dbo.sysjobs jON h.job_id = j.job_idINNER JOIN msdb.dbo.sysjobsteps sON j.job_id = s.job_idAND h.step_id = s.step_idWHERE h.run_status = 0 -- FailureAND h.run_date > CONVERT(int,CONVERT(varchar(10), DATEADD(DAY, -1, GETDATE()), 112))ORDER BY h.instance_id DESC;
1.2 檢查兩天內,運行時間超過30分鐘的Job,并按執行時間長短排序,時間2天和運行時間30分鐘,都是可以調整的,可以調整為自己需要的檢查范圍,代碼會把執行Job的名稱,運行時間,平均執行時間列出來,看是否有突然變化的運行情況,
----1.2 Check Jobs With Long Duration:30minutes(can modify to other value) From Last 2 Day To NowSELECT sj.name, sja.start_execution_date,DATEDIFF (minute ,sja.start_execution_date,sja.stop_execution_date ) AS ExecutedMin,ja.AvgRuntimeOnSucceed/60 as AvgRuntimeOnSucceedMinFROM msdb.dbo.sysjobactivity AS sjaINNER JOIN msdb.dbo.sysjobs AS sj ON sja.job_id = sj.job_id INNERjoin(SELECT job_id,AVG((run_duration/10000 * 3600) + ((run_duration%10000)/100*60) + (run_duration%10000)%100)+NULLIF(0,STDEV((run_duration/10000 * 3600) + ((run_duration%10000)/100*60) + (run_duration%10000)%100)) AS 'AvgRuntimeOnSucceed'FROM msdb.dbo.sysjobhistoryWHERE step_id = 0 AND run_status = 1GROUP BY job_id) jaON sj.job_id = ja.job_idWHERE sja.start_execution_date IS NOT NULL --作業有開始AND sja.stop_execution_date IS not NULL --作業結束AND sja.start_execution_date>DATEADD(DAY,-2,GETDATE()) --作業2天內開始and DATEDIFF (minute ,sja.start_execution_date,sja.stop_execution_date )>30order by ExecutedMin desc
1.3 檢查資料庫錯誤日志,默認讀取的是當前log,篩選的是Error開頭的錯誤日志,可以根據需要篩選其他關鍵字,
----1.3 Check SQL Error LogDROP TABLE IF EXISTS #errorLog; -- this is new syntax in SQL 2016 and laterCREATE TABLE #errorLog (LogDate DATETIME, ProcessInfo VARCHAR(64), [Text] VARCHAR(MAX));INSERT INTO #errorLogEXEC sp_readerrorlogSELECT *FROM #errorLog aWHERE EXISTS (SELECT *FROM #errorLog bWHERE [Text] like 'Error:%'AND a.LogDate = b.LogDateAND a.ProcessInfo = b.ProcessInfo)
1.4 檢查含有資料庫檔案的磁盤的空間大小,可以看到剩余百分比,實際大小,使用大小等,
----1.4 Check HDD Free Spaceselect distinctconvert(varchar(512), b.volume_mount_point) as [volume_mount_point], convert(varchar(512), b.logical_volume_name) as [logical_volume_name], convert(decimal(18,1), round(((convert(float, b.available_bytes) / convert(float, b.total_bytes)) * 100),1)) as [Drive_Free_Percent], convert(bigint, round(((b.available_bytes / 1024.0)/1024.0),0)) as [Drive_Free_MB], convert(bigint, round(((b.total_bytes / 1024.0)/1024.0),0)) as [Drive_Total_MB], convert(bigint, round((((b.total_bytes - b.available_bytes) / 1024.0)/1024.0),0)) as [Drive_Used_MB]from sys.master_files as [a]CROSS APPLY sys.dm_os_volume_stats(a.database_id, a.[file_id]) as [b]order by volume_mount_point
1.5 檢查資料庫資料檔案和日志檔案的大小,默認排除了id為4以下的系統資料庫,可以根據需要調整,只觀察自己需要的資料庫,
----1.5 Check DB Data And Log Sizeselect distinct db_name(a.database_id) as [DatabaseName],database_id, a.name as [Logical_Name], convert(decimal(28,2), round(((a.size * 8.0) / 1024.00),2)) as [SizeMB]from sys.master_files as [a]where database_id>4order by DatabaseName,Logical_Name
1.1代碼檢查從昨天到現在,SQL代理Job有沒有運行失敗的,會把運行失敗的Job名字,步驟,運行時間,錯誤等級,錯誤原因羅列出來,方便查看,
----1.1 Check Job Fail List From Last Day To NowSELECT j.[name],h.step_id,h.step_name,h.run_date,h.run_time,h.sql_severity,h.message,h.serverFROM msdb.dbo.sysjobhistory hINNER JOIN msdb.dbo.sysjobs jON h.job_id = j.job_idINNER JOIN msdb.dbo.sysjobsteps sON j.job_id = s.job_idAND h.step_id = s.step_idWHERE h.run_status = 0 -- FailureAND h.run_date > CONVERT(int,CONVERT(varchar(10), DATEADD(DAY, -1, GETDATE()), 112))ORDER BY h.instance_id DESC;
1.2 檢查兩天內,運行時間超過30分鐘的Job,并按執行時間長短排序,時間2天和運行時間30分鐘,都是可以調整的,可以調整為自己需要的檢查范圍,代碼會把執行Job的名稱,運行時間,平均執行時間列出來,看是否有突然變化的運行情況,
----1.2 Check Jobs With Long Duration:30minutes(can modify to other value) From Last 2 Day To NowSELECT sj.name, sja.start_execution_date,DATEDIFF (minute ,sja.start_execution_date,sja.stop_execution_date ) AS ExecutedMin,ja.AvgRuntimeOnSucceed/60 as AvgRuntimeOnSucceedMinFROM msdb.dbo.sysjobactivity AS sjaINNER JOIN msdb.dbo.sysjobs AS sj ON sja.job_id = sj.job_id INNERjoin(SELECT job_id,AVG((run_duration/10000 * 3600) + ((run_duration%10000)/100*60) + (run_duration%10000)%100)+NULLIF(0,STDEV((run_duration/10000 * 3600) + ((run_duration%10000)/100*60) + (run_duration%10000)%100)) AS 'AvgRuntimeOnSucceed'FROM msdb.dbo.sysjobhistoryWHERE step_id = 0 AND run_status = 1GROUP BY job_id) jaON sj.job_id = ja.job_idWHERE sja.start_execution_date IS NOT NULL --作業有開始AND sja.stop_execution_date IS not NULL --作業結束AND sja.start_execution_date>DATEADD(DAY,-2,GETDATE()) --作業2天內開始and DATEDIFF (minute ,sja.start_execution_date,sja.stop_execution_date )>30order by ExecutedMin desc
1.3 檢查資料庫錯誤日志,默認讀取的是當前log,篩選的是Error開頭的錯誤日志,可以根據需要篩選其他關鍵字,
----1.3 Check SQL Error LogDROP TABLE IF EXISTS #errorLog; -- this is new syntax in SQL 2016 and laterCREATE TABLE #errorLog (LogDate DATETIME, ProcessInfo VARCHAR(64), [Text] VARCHAR(MAX));INSERT INTO #errorLogEXEC sp_readerrorlogSELECT *FROM #errorLog aWHERE EXISTS (SELECT *FROM #errorLog bWHERE [Text] like 'Error:%'AND a.LogDate = b.LogDateAND a.ProcessInfo = b.ProcessInfo)
1.4 檢查含有資料庫檔案的磁盤的空間大小,可以看到剩余百分比,實際大小,使用大小等,
----1.4 Check HDD Free Spaceselect distinctconvert(varchar(512), b.volume_mount_point) as [volume_mount_point], convert(varchar(512), b.logical_volume_name) as [logical_volume_name], convert(decimal(18,1), round(((convert(float, b.available_bytes) / convert(float, b.total_bytes)) * 100),1)) as [Drive_Free_Percent], convert(bigint, round(((b.available_bytes / 1024.0)/1024.0),0)) as [Drive_Free_MB], convert(bigint, round(((b.total_bytes / 1024.0)/1024.0),0)) as [Drive_Total_MB], convert(bigint, round((((b.total_bytes - b.available_bytes) / 1024.0)/1024.0),0)) as [Drive_Used_MB]from sys.master_files as [a]CROSS APPLY sys.dm_os_volume_stats(a.database_id, a.[file_id]) as [b]order by volume_mount_point
1.5 檢查資料庫資料檔案和日志檔案的大小,默認排除了id為4以下的系統資料庫,可以根據需要調整,只觀察自己需要的資料庫,
----1.5 Check DB Data And Log Sizeselect distinct db_name(a.database_id) as [DatabaseName],database_id, a.name as [Logical_Name], convert(decimal(28,2), round(((a.size * 8.0) / 1024.00),2)) as [SizeMB]from sys.master_files as [a]where database_id>4order by DatabaseName,Logical_Name
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/544657.html
標籤:SQL Server
