首页 > 数据库 >SQL Server数据库日常检查

SQL Server数据库日常检查

时间:2023-02-21 12:55:47浏览次数:53  
标签:convert run 数据库 Server job SQL date sja id

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.server  FROM    msdb.dbo.sysjobhistory h          INNER JOIN msdb.dbo.sysjobs j              ON h.job_id = j.job_id          INNER JOIN msdb.dbo.sysjobsteps s              ON j.job_id = s.job_id                 AND h.step_id = s.step_id  WHERE    h.run_status = 0 -- Failure           AND 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.sysjobhistory    WHERE step_id = 0 AND run_status = 1    GROUP BY job_id) ja     ON 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 later

CREATE TABLE #errorLog (LogDate DATETIME, ProcessInfo VARCHAR(64), [Text] VARCHAR(MAX));

INSERT INTO #errorLogEXEC sp_readerrorlog

SELECT * FROM #errorLog aWHERE EXISTS (SELECT * FROM #errorLog b WHERE [Text] like 'Error:%' AND a.LogDate = b.LogDate AND a.ProcessInfo = b.ProcessInfo)

1.4 检查含有数据库文件的磁盘的空间大小,可以看到剩余百分比,实际大小,使用大小等。

----1.4  Check HDD Free Spaceselect distinct convert(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.server  FROM    msdb.dbo.sysjobhistory h          INNER JOIN msdb.dbo.sysjobs j              ON h.job_id = j.job_id          INNER JOIN msdb.dbo.sysjobsteps s              ON j.job_id = s.job_id                 AND h.step_id = s.step_id  WHERE    h.run_status = 0 -- Failure           AND 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.sysjobhistory    WHERE step_id = 0 AND run_status = 1    GROUP BY job_id) ja     ON 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 later

CREATE TABLE #errorLog (LogDate DATETIME, ProcessInfo VARCHAR(64), [Text] VARCHAR(MAX));

INSERT INTO #errorLogEXEC sp_readerrorlog

SELECT * FROM #errorLog aWHERE EXISTS (SELECT * FROM #errorLog b WHERE [Text] like 'Error:%' AND a.LogDate = b.LogDate AND a.ProcessInfo = b.ProcessInfo)

1.4 检查含有数据库文件的磁盘的空间大小,可以看到剩余百分比,实际大小,使用大小等。

----1.4  Check HDD Free Spaceselect distinct convert(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

标签:convert,run,数据库,Server,job,SQL,date,sja,id
From: https://www.cnblogs.com/kingster/p/17140585.html

相关文章

  • Excel文件 利用MySQL/Python 实现自动处理数据的功能
    目录问题描述:解决方案:一、SQL查询二、SQL、python处理三、python处理四、优化python处理1.手动执行代码2.开机自动执行代码对比四种方案:总结:问题描述:在没有服务器存储数......
  • 部署堡垒机4——安装JumpServer
    部署jumpserver服务一、前期准备一个后台程序,基本上都是需要依赖于数据库才能运行,后台程序在启动的时候,代码就回去连接数据库,保证数据库,正确启动,且可以正确连接,否则后台......
  • windows 安装 mysql-8.0.32 压缩包方式
    下载地址https://dev.mysql.com/downloads/mysql/5.0.html解压把解压好的文件夹放到D盘(注意所有路径中都不要包含中文路径)D:\mysql-8.0.32-winx64创建一个空文......
  • NETCORE - IdentityServer4 客户端凭据模式
    NETCORE-IdentityServer4客户端凭据模式                     参考:http://www.identityserver.com.cn/Home/Detail/shiyon......
  • javax.servlet.ServletContext.getVirtualServerName()Ljava/lang/String; spring
    2023-02-2111:44:13.924ERROR27256---[main]o.s.b.d.LoggingFailureAnalysisReporter:***************************APPLICATIONFAILEDTOSTART************......
  • Centos7 安装MySQL8
    1.删除之前安装的MySQL包[root@localhost~]#rpm-qa|grepmariadbmariadb-libs-5.5.60-1.el7_5.x86_64[root@localhost~]#yumerase-ymariadb-libs-5.5.60-1.el......
  • idea中配置mybatis 映射文件模版及 mybatis plus 自定义sql
    本文为博主原创,未经允许不得转载:mybatisplus 使用过程中已经很大程度提升了我们开发的效率,因为它内部已经对单表的操作进行了完美的封装,但是关联表操作时,这时就需要自......
  • linux mysql操作命令大全
    1.linux下启动mysql的命令:mysqladminstart/ect/init.d/mysqlstart(前面为mysql的安装路径)2.linux下重启mysql的命令:mysqladminrestart/ect/init.d/mysqlrestart(前面......
  • MySQL按照汉字的拼音排序
    按照汉字的拼音排序,用的比较多是在人名的排序中,按照姓氏的拼音字母,从A到Z排序; 如果存储姓名的字段采用的是GBK字符集,那就好办了,因为GBK内码编码时本身就采用了拼音排序的方......
  • mysql中if()函数使用
    博主原创,转载请注明出处:     在mysql中if()函数的用法类似于java中的三目表达式,其用处也比较多,具体语法如下:IF(expr1,expr2,expr3),如果expr1的值为true,则返回expr2的......