`
zhang_215
  • 浏览: 14696 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类

SQL Server 作业监控

阅读更多
在讲解SQLServer Agent Jobs之前,先要讲解msdb。
Msdb是SQLServer的系统数据库之一,用于存储SQLServer的配置、元数据等信息。包括:
l  SQLServer Agent Jobs,Job Steps,Job schedules,Alerts,Operators,等等。
l  Service Broker,Log Shipping,Backups/restore信息,维护计划、数据库邮件、基于策略管理信息等等。
l  SSIS包。
在这部分,主要集中在msdb的以下部分:
l  Job setup/configuration Information
l  Job Execution Information
l  Job Step(s) Setup/Configuration Information
l  Job Step(s) Execution Information
l  Schedule Information
————————————————————————————————————————————————————————————————————————————

SQLServer 允许在Replication,SSIS,存储过程,批处理上创建和执行各种自动化任务。允许使用GUI 或者T-SQL脚本创建。这些信息存储在msdb中。SQL Server Agent Job Setup andConfiguration Information:
可以在SSMS中执行以下脚本查找作业信息:

SELECT  [sJOB].[job_id] AS [作业ID] ,
        [sJOB].[name] AS [作业名称] ,
        [sDBP].[name] AS [作业创建者] ,
        [sCAT].[name] AS [作业种类] ,
        [sJOB].[description] AS [作业描述] ,
        CASE [sJOB].[enabled]
          WHEN 1 THEN '已启用'
          WHEN 0 THEN '未启用'
        END AS [是否启用] ,--
        [sJOB].[date_created] AS [作业创建日期] ,
        [sJOB].[date_modified] AS [作业最后修改日期] ,
        [sSVR].[name] AS [作业运行服务器] ,
        [sJSTP].[step_id] AS [作业起始步骤] ,
        [sJSTP].[step_name] AS [步骤名称] ,
        CASE WHEN [sSCH].[schedule_uid] IS NULL THEN '否'
             ELSE '是'
        END AS [是否分布式作业] ,
        [sSCH].[schedule_uid] AS [分布式作业ID] ,
        [sSCH].[name] AS [用户定义名称] ,
        CASE [sJOB].[delete_level]
          WHEN 0 THEN '不删除'
          WHEN 1 THEN '成功后删除'
          WHEN 2 THEN '失败后删除'
          WHEN 3 THEN '完成时删除'
        END AS [完成时删除作业级别]
FROM    [msdb].[dbo].[sysjobs] AS [sJOB]
        LEFT JOIN [msdb].[sys].[servers] AS [sSVR] ON [sJOB].[originating_server_id] = [sSVR].[server_id]
        LEFT JOIN [msdb].[dbo].[syscategories] AS [sCAT] ON [sJOB].[category_id] = [sCAT].[category_id]
        LEFT JOIN [msdb].[dbo].[sysjobsteps] AS [sJSTP] ON [sJOB].[job_id] = [sJSTP].[job_id]
                                                           AND [sJOB].[start_step_id] = [sJSTP].[step_id]
        LEFT JOIN [msdb].[sys].[database_principals] AS [sDBP] ON [sJOB].[owner_sid] = [sDBP].[sid]
        LEFT JOIN [msdb].[dbo].[sysjobschedules] AS [sJOBSCH] ON [sJOB].[job_id] = [sJOBSCH].[job_id]
        LEFT JOIN [msdb].[dbo].[sysschedules] AS [sSCH] ON [sJOBSCH].[schedule_id] = [sSCH].[schedule_id]
ORDER BY [作业名称]

SQL Server Agent Job Execution Information:
SQLServer同时存放作业执行信息在msdb中。可以执行以下脚本查询作业执行情况:

SELECT  [sJOB].[job_id] AS [作业ID] ,
        [sJOB].[name] AS [作业名] ,
        CASE WHEN [sJOBH].[run_date] IS NULL
                  OR [sJOBH].[run_time] IS NULL THEN NULL
             ELSE CAST(CAST([sJOBH].[run_date] AS CHAR(8)) + ' '
                  + STUFF(STUFF(RIGHT('000000'
                                      + CAST([sJOBH].[run_time] AS VARCHAR(6)),
                                      6), 3, 0, ':'), 6, 0, ':') AS DATETIME)
        END AS [最近执行时间] ,
        CASE [sJOBH].[run_status]
          WHEN 0 THEN '失败'
          WHEN 1 THEN '成功'
          WHEN 2 THEN '重试'
          WHEN 3 THEN '取消'
          WHEN 4 THEN '正在运行' -- In Progress
        END AS [最近执行状态] ,
        STUFF(STUFF(RIGHT('000000'
                          + CAST([sJOBH].[run_duration] AS VARCHAR(6)), 6), 3,
                    0, ':'), 6, 0, ':') AS [LastRunDuration (HH:MM:SS)] ,
        [sJOBH].[message] AS [最近运行状态信息] ,
        CASE [sJOBSCH].[NextRunDate]
          WHEN 0 THEN NULL
          ELSE CAST(CAST([sJOBSCH].[NextRunDate] AS CHAR(8)) + ' '
               + STUFF(STUFF(RIGHT('000000'
                                   + CAST([sJOBSCH].[NextRunTime] AS VARCHAR(6)),
                                   6), 3, 0, ':'), 6, 0, ':') AS DATETIME)
        END AS [下次运行时间]
FROM    [msdb].[dbo].[sysjobs] AS [sJOB]
        LEFT JOIN ( SELECT  [job_id] ,
                            MIN([next_run_date]) AS [NextRunDate] ,
                            MIN([next_run_time]) AS [NextRunTime]
                    FROM    [msdb].[dbo].[sysjobschedules]
                    GROUP BY [job_id]
                  ) AS [sJOBSCH] ON [sJOB].[job_id] = [sJOBSCH].[job_id]
        LEFT JOIN ( SELECT  [job_id] ,
                            [run_date] ,
                            [run_time] ,
                            [run_status] ,
                            [run_duration] ,
                            [message] ,
                            ROW_NUMBER() OVER ( PARTITION BY [job_id] ORDER BY [run_date] DESC, [run_time] DESC ) AS RowNumber
                    FROM    [msdb].[dbo].[sysjobhistory]
                    WHERE   [step_id] = 0
                  ) AS [sJOBH] ON [sJOB].[job_id] = [sJOBH].[job_id]
                                  AND [sJOBH].[RowNumber] = 1
ORDER BY [作业名]

SQL Server Anget Job Steps Setup andconfiguration Information:
在作业系统中,一个作业是有层级的,可以包含一个或多个步骤。
运行以下脚本查看作业步骤信息:

[sql] view plaincopyprint?
SELECT  [sJOB].[job_id] AS [作业ID] , 
        [sJOB].[name] AS [作业名] , 
        [sJSTP].[step_uid] AS [步骤ID] , 
        [sJSTP].[step_id] AS [步骤序号] , 
        [sJSTP].[step_name] AS [步骤名] , 
        CASE [sJSTP].[subsystem] 
          WHEN 'ActiveScripting' THEN 'ActiveX Script' 
          WHEN 'CmdExec' THEN 'Operating system (CmdExec)' 
          WHEN 'PowerShell' THEN 'PowerShell' 
          WHEN 'Distribution' THEN 'Replication Distributor' 
          WHEN 'Merge' THEN 'Replication Merge' 
          WHEN 'QueueReader' THEN 'Replication Queue Reader' 
          WHEN 'Snapshot' THEN 'Replication Snapshot' 
          WHEN 'LogReader' THEN 'Replication Transaction-Log Reader' 
          WHEN 'ANALYSISCOMMAND' THEN 'SQL Server Analysis Services Command' 
          WHEN 'ANALYSISQUERY' THEN 'SQL Server Analysis Services Query' 
          WHEN 'SSIS' THEN 'SQL Server Integration Services Package' 
          WHEN 'TSQL' THEN 'Transact-SQL script (T-SQL)' 
          ELSE sJSTP.subsystem 
        END AS [作业子系统类型] , 
        [sPROX].[name] AS [作业运行账号] , 
        [sJSTP].[database_name] AS [执行数据库名] , 
        [sJSTP].[command] AS [执行命令] , 
        CASE [sJSTP].[on_success_action] 
          WHEN 1 THEN 'Quit the job reporting success' 
          WHEN 2 THEN 'Quit the job reporting failure' 
          WHEN 3 THEN 'Go to the next step' 
          WHEN 4 
          THEN 'Go to Step: ' 
               + QUOTENAME(CAST([sJSTP].[on_success_step_id] AS VARCHAR(3))) 
               + ' ' + [sOSSTP].[step_name] 
        END AS [执行成功后反应] , 
        [sJSTP].[retry_attempts] AS [失败时的重试次数] , 
        [sJSTP].[retry_interval] AS [重试间的等待时间 (Minutes)] , 
        CASE [sJSTP].[on_fail_action] 
          WHEN 1 THEN 'Quit the job reporting success' 
          WHEN 2 THEN 'Quit the job reporting failure' 
          WHEN 3 THEN 'Go to the next step' 
          WHEN 4 
          THEN 'Go to Step: ' 
               + QUOTENAME(CAST([sJSTP].[on_fail_step_id] AS VARCHAR(3))) 
               + ' ' + [sOFSTP].[step_name] 
        END AS [执行失败后反映] 
FROM    [msdb].[dbo].[sysjobsteps] AS [sJSTP] 
        INNER JOIN [msdb].[dbo].[sysjobs] AS [sJOB] ON [sJSTP].[job_id] = [sJOB].[job_id] 
        LEFT JOIN [msdb].[dbo].[sysjobsteps] AS [sOSSTP] ON [sJSTP].[job_id] = [sOSSTP].[job_id] 
                                                            AND [sJSTP].[on_success_step_id] = [sOSSTP].[step_id] 
        LEFT JOIN [msdb].[dbo].[sysjobsteps] AS [sOFSTP] ON [sJSTP].[job_id] = [sOFSTP].[job_id] 
                                                            AND [sJSTP].[on_fail_step_id] = [sOFSTP].[step_id] 
        LEFT JOIN [msdb].[dbo].[sysproxies] AS [sPROX] ON [sJSTP].[proxy_id] = [sPROX].[proxy_id] 
ORDER BY [作业名] , 
        [步骤序号] 


SQL Server Anget Job Steps ExecutionInformation:
在msdb中同样存储了步骤的执行计划,执行以下语句检查:


[sql] view plaincopyprint?
SELECT  [sJOB].[job_id] AS [作业ID] , 
        [sJOB].[name] AS [作业名称] , 
        [sJSTP].[step_uid] AS [步骤ID] , 
        [sJSTP].[step_id] AS [步骤序号] , 
        [sJSTP].[step_name] AS [步骤名称] , 
        CASE [sJSTP].[last_run_outcome] 
          WHEN 0 THEN '失败' 
          WHEN 1 THEN '成功' 
          WHEN 2 THEN '重试' 
          WHEN 3 THEN '取消' 
          WHEN 5 THEN '未知' 
        END AS [上次运行状态] , 
        STUFF(STUFF(RIGHT('000000' 
                          + CAST([sJSTP].[last_run_duration] AS VARCHAR(6)), 6), 
                    3, 0, ':'), 6, 0, ':') AS [LastRunDuration (HH:MM:SS)] , 
        [sJSTP].[last_run_retries] AS [上次重试次数] , 
        CASE [sJSTP].[last_run_date] 
          WHEN 0 THEN NULL 
          ELSE CAST(CAST([sJSTP].[last_run_date] AS CHAR(8)) + ' ' 
               + STUFF(STUFF(RIGHT('000000' 
                                   + CAST([sJSTP].[last_run_time] AS VARCHAR(6)), 
                                   6), 3, 0, ':'), 6, 0, ':') AS DATETIME) 
        END AS [上次运行时间] 
FROM    [msdb].[dbo].[sysjobsteps] AS [sJSTP] 
        INNER JOIN [msdb].[dbo].[sysjobs] AS [sJOB] ON [sJSTP].[job_id] = [sJOB].[job_id] 
ORDER BY [作业名称] , 
        [步骤序号] 


SQL Server Agent Job Sechdule Information:
SQLServer允许在特定时间创建各种计划,每个计划能组合成一个或多个SQLServer Agent Jobs。执行以下脚本查询情况:
[sql] view plaincopyprint?
SELECT  [schedule_uid] AS [作业计划ID] , 
        [name] AS [作业计划名称] , 
        CASE [enabled] 
          WHEN 1 THEN '已启用' 
          WHEN 0 THEN '未启用' 
        END AS [是否启用] , 
        CASE WHEN [freq_type] = 64 
             THEN 'Start automatically when SQL Server Agent starts' 
             WHEN [freq_type] = 128 THEN 'Start whenever the CPUs become idle' 
             WHEN [freq_type] IN ( 4, 8, 16, 32 ) THEN 'Recurring' 
             WHEN [freq_type] = 1 THEN 'One Time' 
        END [作业计划类型] , 
        CASE [freq_type] 
          WHEN 1 THEN 'One Time' 
          WHEN 4 THEN 'Daily' 
          WHEN 8 THEN 'Weekly' 
          WHEN 16 THEN 'Monthly' 
          WHEN 32 THEN 'Monthly - Relative to Frequency Interval' 
          WHEN 64 THEN 'Start automatically when SQL Server Agent starts' 
          WHEN 128 THEN 'Start whenever the CPUs become idle' 
        END [作业运行频率] , 
        CASE [freq_type] 
          WHEN 4 
          THEN 'Occurs every ' + CAST([freq_interval] AS VARCHAR(3)) 
               + ' day(s)' 
          WHEN 8 
          THEN 'Occurs every ' + CAST([freq_recurrence_factor] AS VARCHAR(3)) 
               + ' week(s) on ' 
               + CASE WHEN [freq_interval] & 1 = 1 THEN 'Sunday' 
                      ELSE '' 
                 END + CASE WHEN [freq_interval] & 2 = 2 THEN ', Monday' 
                            ELSE '' 
                       END 
               + CASE WHEN [freq_interval] & 4 = 4 THEN ', Tuesday' 
                      ELSE '' 
                 END + CASE WHEN [freq_interval] & 8 = 8 THEN ', Wednesday' 
                            ELSE '' 
                       END 
               + CASE WHEN [freq_interval] & 16 = 16 THEN ', Thursday' 
                      ELSE '' 
                 END + CASE WHEN [freq_interval] & 32 = 32 THEN ', Friday' 
                            ELSE '' 
                       END 
               + CASE WHEN [freq_interval] & 64 = 64 THEN ', Saturday' 
                      ELSE '' 
                 END 
          WHEN 16 
          THEN 'Occurs on Day ' + CAST([freq_interval] AS VARCHAR(3)) 
               + ' of every ' + CAST([freq_recurrence_factor] AS VARCHAR(3)) 
               + ' month(s)' 
          WHEN 32 
          THEN 'Occurs on ' + CASE [freq_relative_interval] 
                                WHEN 1 THEN 'First' 
                                WHEN 2 THEN 'Second' 
                                WHEN 4 THEN 'Third' 
                                WHEN 8 THEN 'Fourth' 
                                WHEN 16 THEN 'Last' 
                              END + ' ' + CASE [freq_interval] 
                                            WHEN 1 THEN 'Sunday' 
                                            WHEN 2 THEN 'Monday' 
                                            WHEN 3 THEN 'Tuesday' 
                                            WHEN 4 THEN 'Wednesday' 
                                            WHEN 5 THEN 'Thursday' 
                                            WHEN 6 THEN 'Friday' 
                                            WHEN 7 THEN 'Saturday' 
                                            WHEN 8 THEN 'Day' 
                                            WHEN 9 THEN 'Weekday' 
                                            WHEN 10 THEN 'Weekend day' 
                                          END + ' of every ' 
               + CAST([freq_recurrence_factor] AS VARCHAR(3)) + ' month(s)' 
        END AS [循环间隔] , 
        CASE [freq_subday_type] 
          WHEN 1 
          THEN 'Occurs once at ' + STUFF(STUFF(RIGHT('000000' 
                                                     + CAST([active_start_time] AS VARCHAR(6)), 
                                                     6), 3, 0, ':'), 6, 0, ':') 
          WHEN 2 
          THEN 'Occurs every ' + CAST([freq_subday_interval] AS VARCHAR(3)) 
               + ' Second(s) between ' + STUFF(STUFF(RIGHT('000000' 
                                                           + CAST([active_start_time] AS VARCHAR(6)), 
                                                           6), 3, 0, ':'), 6, 
                                               0, ':') + ' & ' 
               + STUFF(STUFF(RIGHT('000000' 
                                   + CAST([active_end_time] AS VARCHAR(6)), 6), 
                             3, 0, ':'), 6, 0, ':') 
          WHEN 4 
          THEN 'Occurs every ' + CAST([freq_subday_interval] AS VARCHAR(3)) 
               + ' Minute(s) between ' + STUFF(STUFF(RIGHT('000000' 
                                                           + CAST([active_start_time] AS VARCHAR(6)), 
                                                           6), 3, 0, ':'), 6, 
                                               0, ':') + ' & ' 
               + STUFF(STUFF(RIGHT('000000' 
                                   + CAST([active_end_time] AS VARCHAR(6)), 6), 
                             3, 0, ':'), 6, 0, ':') 
          WHEN 8 
          THEN 'Occurs every ' + CAST([freq_subday_interval] AS VARCHAR(3)) 
               + ' Hour(s) between ' + STUFF(STUFF(RIGHT('000000' 
                                                         + CAST([active_start_time] AS VARCHAR(6)), 
                                                         6), 3, 0, ':'), 6, 0, 
                                             ':') + ' & ' 
               + STUFF(STUFF(RIGHT('000000' 
                                   + CAST([active_end_time] AS VARCHAR(6)), 6), 
                             3, 0, ':'), 6, 0, ':') 
        END [计划运行频率] , 
        STUFF(STUFF(CAST([active_start_date] AS VARCHAR(8)), 5, 0, '-'), 8, 0, 
              '-') AS [作业启用开始时间] , 
        STUFF(STUFF(CAST([active_end_date] AS VARCHAR(8)), 5, 0, '-'), 8, 0, 
              '-') AS [作业启用结束时间] , 
        [date_created] AS [作业创建日期] , 
        [date_modified] AS [作业上次修改日期] 
FROM    [msdb].[dbo].[sysschedules] 
ORDER BY [作业计划名称] 



转自:http://blog.csdn.net/dba_huangzj/article/details/7608844
分享到:
评论

相关推荐

    使用flink-connector-sqlserver-cdc 2.3.0把数据从SQL Server实时同步到MySQL中

    - 首先,你需要在你的Flink项目中添加`flink-connector-sqlserver-cdc_2.11-2.3.0`依赖。这可以通过Maven或Gradle的依赖管理来完成。 - 接着,配置连接器以指向你的SQL Server实例,包括服务器地址、端口、数据库...

    Sql Server 定时 作业 任务 创建 详细步骤

    ### SQL Server 定时作业任务创建详细步骤 #### 前言 在SQL Server中,通过设置定时作业任务,可以实现数据库的自动化管理和维护工作,比如定期备份、清理日志等。本文档将详细介绍如何在SQL Server Management ...

    SQL-server作业指导书.doc

    SQL Server作业指导书是针对使用Microsoft SQL Server进行数据库管理和安全维护的一个重要文档,它涵盖了多个关键领域的检查和最佳实践。以下是对指导书中提及的一些主要知识点的详细解释: 1. **数据库系统应用...

    SQL Server Agent Job检查工具

    SMO类库提供了丰富的接口和类,例如`Microsoft.SqlServer.Management.Smo.Agent.Job`代表一个作业,`Microsoft.SqlServer.Management.Smo.Agent.Server`则代表SQL Server实例。开发者可以通过这些类的属性和方法来...

    SqlServer数据库OGG安装部署及数据初始化.docx

    接着,将OGG for Sqlserver的软件包解压缩到指定目录,比如"ogg",然后通过`install addservice`命令在CMD中注册Windows服务,包括源端和目标端的Manager进程。 创建ODBC数据源命名(DSN)是连接SQL Server的关键...

    sqlserver2008,2005添加作业-数据库定时自动备份

    本文将详细介绍如何在SQL Server Management Studio (SSMS) 中创建和配置作业以实现这一功能。 首先,打开SQL Server Management Studio并连接到你的SQL Server实例。在对象资源管理器中,导航至“SQL Server Agent...

    SQL Server 数据库实现定时自动备份

    之后通过查看“SQL Server 代理”->“历史”来监控备份作业的执行情况。 此外,还可以考虑使用第三方工具或者SQL Server维护计划来简化这个过程。这些工具通常提供更直观的界面,方便配置备份策略,并且可能包含...

    SQLServer 2008 出现红叉错误的解决方法

    ### SQL Server 2008 出现红叉错误的解决方法 在使用SQL Server 2008的过程中,用户可能会遇到服务管理器中的服务图标显示为红色叉号的情况,这意味着SQL Server服务出现了问题,无法正常启动。这种情况下,用户...

    基于SQL Server作业 扩展存储过程实现数据库自动远程备份.pdf

    "基于SQL Server作业扩展存储过程实现数据库自动远程备份.pdf" 数据库自动远程备份是数据库管理中非常重要的一环,尤其是在大型企业中,数据的安全和可靠性对业务的影响非常大。如果数据库发生崩溃或遭受攻击,导致...

    启动SQL Server服务.txt

    - **SQL Server服务**:SQL Server是由微软开发的一款关系型数据库管理系统,它提供了多种服务来支持不同的功能需求,例如SQL Server Database Engine(主要负责数据存储和查询)、SQL Server Agent(用于计划作业)...

    使用CPU计数器监视SQL Server性能的

    本文主要探讨了几个关键的CPU资源消耗点,以及如何通过监控和调整来提升SQL Server的性能。 首先,上下文切换是SQL Server中常见的CPU资源消费者。当SQL Server在处理多线程时,频繁地在不同CPU之间切换线程会产生...

    sql server 上机指导

    管理工具和服务管理器则是用于管理和监控SQL Server实例的工具。 4. **掌握SQL Server 2000服务管理器的启动:** 服务管理器是用于控制SQL Server服务的启动、停止和暂停等功能的工具。通过服务管理器可以确保SQL ...

    SQL Server 2000系统表地图.rar_sql_sql server_sql server 2000_多变量

    掌握SQL Server 2000的系统表知识,可以帮助数据库管理员更有效地进行性能监控、问题诊断和优化工作。这不仅包括了解每个系统表的基本结构,还包括理解如何通过查询这些表来获取关键信息,以及如何利用这些信息来...

    SQL Server自动备份脚本 bat

    在SQL Server管理数据库的过程中,定期备份是至关重要的,以防数据丢失或系统故障。"SQL Server自动备份脚本 bat" 提供了一种自动化解决方案,确保你可以按照预定的时间表执行数据库备份。下面将详细介绍如何利用SQL...

    Sqlserver2005数据库还原备份工具

    本文将深入探讨“Sqlserver2005数据库还原备份工具”以及相关的知识点。 首先,我们关注的是“备份”。在SQL Server 2005中,备份是预防数据丢失的关键步骤。备份操作可以创建数据库的完整副本,以便在系统故障、...

    SQLServer代理服务器

    8. 代理服务器的监控:定期检查SQL Server代理服务器的状态,确保其正常运行,避免因服务未启动或配置错误导致的作业执行失败。 理解并掌握SQL Server代理服务器的这些核心概念和功能,对于SQL Server的日常管理和...

    SQL_Server_2008维护计划实现数据库定时自动备份

    - **SQL Server Agent**:这是SQL Server的一个关键组件,用于执行自动化任务,如备份、性能监控和数据清理等。通过SQL Server Agent,用户可以创建作业,这些作业包含一系列步骤,可在特定的时间或事件触发下执行。...

    sql server 2005安装截图

    这个图形化界面工具提供了对SQL Server的全面管理,包括创建数据库、编写查询、监控性能、备份与恢复、安全管理和配置服务器。 总之,SQL Server 2005的安装涉及到多个步骤和配置选项,需要根据实际需求进行选择。...

    SQL Server2019帮助文档

    - **数据加载**:提供了多种方式将数据导入到SQL Server,如curl、SQL语句和Spark作业。 - **机器学习服务**:支持Python和R语言,用于训练和部署机器学习模型,实现预测分析。 **监控与应用** - **监控应用**:...

    SQLserver AlwaysOn维护手册

    SQL Server AlwaysOn 维护手册 SQL Server AlwaysOn 是一种高可用性的解决方案,能够提供实时的数据库冗余和故障转移功能,确保...可以通过 SQL Server 自带监控面板或 SQL Server 故障转移群集监来查看集群状态。

Global site tag (gtag.js) - Google Analytics