当前位置: 代码迷 >> SQL >> SQL Server 功课监控[转]
  详细解决方案

SQL Server 功课监控[转]

热度:74   发布时间:2016-05-05 11:59:25.0
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中执行以下脚本查找作业信息:

?

[sql]?view plaincopyprint?
  1. SELECT??[sJOB].[job_id]?AS?[作业ID]?,??
  2. ????????[sJOB].[name]?AS?[作业名称]?,??
  3. ????????[sDBP].[name]?AS?[作业创建者]?,??
  4. ????????[sCAT].[name]?AS?[作业种类]?,??
  5. ????????[sJOB].[description]?AS?[作业描述]?,??
  6. ????????CASE?[sJOB].[enabled]??
  7. ??????????WHEN?1?THEN?'已启用'??
  8. ??????????WHEN?0?THEN?'未启用'??
  9. ????????END?AS?[是否启用]?,--??
  10. ????????[sJOB].[date_created]?AS?[作业创建日期]?,??
  11. ????????[sJOB].[date_modified]?AS?[作业最后修改日期]?,??
  12. ????????[sSVR].[name]?AS?[作业运行服务器]?,??
  13. ????????[sJSTP].[step_id]?AS?[作业起始步骤]?,??
  14. ????????[sJSTP].[step_name]?AS?[步骤名称]?,??
  15. ????????CASE?WHEN?[sSCH].[schedule_uid]?IS?NULL?THEN?'否'??
  16. ?????????????ELSE?'是'??
  17. ????????END?AS?[是否分布式作业]?,??
  18. ????????[sSCH].[schedule_uid]?AS?[分布式作业ID]?,??
  19. ????????[sSCH].[name]?AS?[用户定义名称]?,??
  20. ????????CASE?[sJOB].[delete_level]??
  21. ??????????WHEN?0?THEN?'不删除'??
  22. ??????????WHEN?1?THEN?'成功后删除'??
  23. ??????????WHEN?2?THEN?'失败后删除'??
  24. ??????????WHEN?3?THEN?'完成时删除'??
  25. ????????END?AS?[完成时删除作业级别]??
  26. FROM????[msdb].[dbo].[sysjobs]?AS?[sJOB]??
  27. ????????LEFT?JOIN?[msdb].[sys].[servers]?AS?[sSVR]?ON?[sJOB].[originating_server_id]?=?[sSVR].[server_id]??
  28. ????????LEFT?JOIN?[msdb].[dbo].[syscategories]?AS?[sCAT]?ON?[sJOB].[category_id]?=?[sCAT].[category_id]??
  29. ????????LEFT?JOIN?[msdb].[dbo].[sysjobsteps]?AS?[sJSTP]?ON?[sJOB].[job_id]?=?[sJSTP].[job_id]??
  30. ???????????????????????????????????????????????????????????AND?[sJOB].[start_step_id]?=?[sJSTP].[step_id]??
  31. ????????LEFT?JOIN?[msdb].[sys].[database_principals]?AS?[sDBP]?ON?[sJOB].[owner_sid]?=?[sDBP].[sid]??
  32. ????????LEFT?JOIN?[msdb].[dbo].[sysjobschedules]?AS?[sJOBSCH]?ON?[sJOB].[job_id]?=?[sJOBSCH].[job_id]??
  33. ????????LEFT?JOIN?[msdb].[dbo].[sysschedules]?AS?[sSCH]?ON?[sJOBSCH].[schedule_id]?=?[sSCH].[schedule_id]??
  34. ORDER?BY?[作业名称]??

?

?

SQL Server Agent Job Execution Information:

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

?

[sql]?view plaincopyprint?
  1. SELECT??[sJOB].[job_id]?AS?[作业ID]?,??
  2. ????????[sJOB].[name]?AS?[作业名]?,??
  3. ????????CASE?WHEN?[sJOBH].[run_date]?IS?NULL??
  4. ??????????????????OR?[sJOBH].[run_time]?IS?NULL?THEN?NULL??
  5. ?????????????ELSE?CAST(CAST([sJOBH].[run_date]?AS?CHAR(8))?+?'?'??
  6. ??????????????????+?STUFF(STUFF(RIGHT('000000'??
  7. ??????????????????????????????????????+?CAST([sJOBH].[run_time]?AS?VARCHAR(6)),??
  8. ??????????????????????????????????????6),?3,?0,?':'),?6,?0,?':')?AS?DATETIME)??
  9. ????????END?AS?[最近执行时间]?,??
  10. ????????CASE?[sJOBH].[run_status]??
  11. ??????????WHEN?0?THEN?'失败'??
  12. ??????????WHEN?1?THEN?'成功'??
  13. ??????????WHEN?2?THEN?'重试'??
  14. ??????????WHEN?3?THEN?'取消'??
  15. ??????????WHEN?4?THEN?'正在运行'?--?In?Progress??
  16. ????????END?AS?[最近执行状态]?,??
  17. ????????STUFF(STUFF(RIGHT('000000'??
  18. ??????????????????????????+?CAST([sJOBH].[run_duration]?AS?VARCHAR(6)),?6),?3,??
  19. ????????????????????0,?':'),?6,?0,?':')?AS?[LastRunDuration?(HH:MM:SS)]?,??
  20. ????????[sJOBH].[message]?AS?[最近运行状态信息]?,??
  21. ????????CASE?[sJOBSCH].[NextRunDate]??
  22. ??????????WHEN?0?THEN?NULL??
  23. ??????????ELSE?CAST(CAST([sJOBSCH].[NextRunDate]?AS?CHAR(8))?+?'?'??
  24. ???????????????+?STUFF(STUFF(RIGHT('000000'??
  25. ???????????????????????????????????+?CAST([sJOBSCH].[NextRunTime]?AS?VARCHAR(6)),??
  26. ???????????????????????????????????6),?3,?0,?':'),?6,?0,?':')?AS?DATETIME)??
  27. ????????END?AS?[下次运行时间]??
  28. FROM????[msdb].[dbo].[sysjobs]?AS?[sJOB]??
  29. ????????LEFT?JOIN?(?SELECT??[job_id]?,??
  30. ????????????????????????????MIN([next_run_date])?AS?[NextRunDate]?,??
  31. ????????????????????????????MIN([next_run_time])?AS?[NextRunTime]??
  32. ????????????????????FROM????[msdb].[dbo].[sysjobschedules]??
  33. ????????????????????GROUP?BY?[job_id]??
  34. ??????????????????)?AS?[sJOBSCH]?ON?[sJOB].[job_id]?=?[sJOBSCH].[job_id]??
  35. ????????LEFT?JOIN?(?SELECT??[job_id]?,??
  36. ????????????????????????????[run_date]?,??
  37. ????????????????????????????[run_time]?,??
  38. ????????????????????????????[run_status]?,??
  39. ????????????????????????????[run_duration]?,??
  40. ????????????????????????????[message]?,??
  41. ????????????????????????????ROW_NUMBER()?OVER?(?PARTITION?BY?[job_id]?ORDER?BY?[run_date]?DESC,?[run_time]?DESC?)?AS?RowNumber??
  42. ????????????????????FROM????[msdb].[dbo].[sysjobhistory]??
  43. ????????????????????WHERE???[step_id]?=?0??
  44. ??????????????????)?AS?[sJOBH]?ON?[sJOB].[job_id]?=?[sJOBH].[job_id]??
  45. ??????????????????????????????????AND?[sJOBH].[RowNumber]?=?1??
  46. ORDER?BY?[作业名]??

?

?

SQL Server Anget Job Steps Setup andconfiguration Information:

在作业系统中,一个作业是有层级的,可以包含一个或多个步骤。

运行以下脚本查看作业步骤信息:

?

[sql]?view plaincopyprint?
  1. SELECT??[sJOB].[job_id]?AS?[作业ID]?,??
  2. ????????[sJOB].[name]?AS?[作业名]?,??
  3. ????????[sJSTP].[step_uid]?AS?[步骤ID]?,??
  4. ????????[sJSTP].[step_id]?AS?[步骤序号]?,??
  5. ????????[sJSTP].[step_name]?AS?[步骤名]?,??
  6. ????????CASE?[sJSTP].[subsystem]??
  7. ??????????WHEN?'ActiveScripting'?THEN?'ActiveX?Script'??
  8. ??????????WHEN?'CmdExec'?THEN?'Operating?system?(CmdExec)'??
  9. ??????????WHEN?'PowerShell'?THEN?'PowerShell'??
  10. ??????????WHEN?'Distribution'?THEN?'Replication?Distributor'??
  11. ??????????WHEN?'Merge'?THEN?'Replication?Merge'??
  12. ??????????WHEN?'QueueReader'?THEN?'Replication?Queue?Reader'??
  13. ??????????WHEN?'Snapshot'?THEN?'Replication?Snapshot'??
  14. ??????????WHEN?'LogReader'?THEN?'Replication?Transaction-Log?Reader'??
  15. ??????????WHEN?'ANALYSISCOMMAND'?THEN?'SQL?Server?Analysis?Services?Command'??
  16. ??????????WHEN?'ANALYSISQUERY'?THEN?'SQL?Server?Analysis?Services?Query'??
  17. ??????????WHEN?'SSIS'?THEN?'SQL?Server?Integration?Services?Package'??
  18. ??????????WHEN?'TSQL'?THEN?'Transact-SQL?script?(T-SQL)'??
  19. ??????????ELSE?sJSTP.subsystem??
  20. ????????END?AS?[作业子系统类型]?,??
  21. ????????[sPROX].[name]?AS?[作业运行账号]?,??
  22. ????????[sJSTP].[database_name]?AS?[执行数据库名]?,??
  23. ????????[sJSTP].[command]?AS?[执行命令]?,??
  24. ????????CASE?[sJSTP].[on_success_action]??
  25. ??????????WHEN?1?THEN?'Quit?the?job?reporting?success'??
  26. ??????????WHEN?2?THEN?'Quit?the?job?reporting?failure'??
  27. ??????????WHEN?3?THEN?'Go?to?the?next?step'??
  28. ??????????WHEN?4??
  29. ??????????THEN?'Go?to?Step:?'??
  30. ???????????????+?QUOTENAME(CAST([sJSTP].[on_success_step_id]?AS?VARCHAR(3)))??
  31. ???????????????+?'?'?+?[sOSSTP].[step_name]??
  32. ????????END?AS?[执行成功后反应]?,??
  33. ????????[sJSTP].[retry_attempts]?AS?[失败时的重试次数]?,??
  34. ????????[sJSTP].[retry_interval]?AS?[重试间的等待时间?(Minutes)]?,??
  35. ????????CASE?[sJSTP].[on_fail_action]??
  36. ??????????WHEN?1?THEN?'Quit?the?job?reporting?success'??
  37. ??????????WHEN?2?THEN?'Quit?the?job?reporting?failure'??
  38. ??????????WHEN?3?THEN?'Go?to?the?next?step'??
  39. ??????????WHEN?4??
  40. ??????????THEN?'Go?to?Step:?'??
  41. ???????????????+?QUOTENAME(CAST([sJSTP].[on_fail_step_id]?AS?VARCHAR(3)))??
  42. ???????????????+?'?'?+?[sOFSTP].[step_name]??
  43. ????????END?AS?[执行失败后反映]??
  44. FROM????[msdb].[dbo].[sysjobsteps]?AS?[sJSTP]??
  45. ????????INNER?JOIN?[msdb].[dbo].[sysjobs]?AS?[sJOB]?ON?[sJSTP].[job_id]?=?[sJOB].[job_id]??
  46. ????????LEFT?JOIN?[msdb].[dbo].[sysjobsteps]?AS?[sOSSTP]?ON?[sJSTP].[job_id]?=?[sOSSTP].[job_id]??
  47. ????????????????????????????????????????????????????????????AND?[sJSTP].[on_success_step_id]?=?[sOSSTP].[step_id]??
  48. ????????LEFT?JOIN?[msdb].[dbo].[sysjobsteps]?AS?[sOFSTP]?ON?[sJSTP].[job_id]?=?[sOFSTP].[job_id]??
  49. ????????????????????????????????????????????????????????????AND?[sJSTP].[on_fail_step_id]?=?[sOFSTP].[step_id]??
  50. ????????LEFT?JOIN?[msdb].[dbo].[sysproxies]?AS?[sPROX]?ON?[sJSTP].[proxy_id]?=?[sPROX].[proxy_id]??
  51. ORDER?BY?[作业名]?,??
  52. ????????[步骤序号]??

?

?

?

SQL Server Anget Job Steps ExecutionInformation:

在msdb中同样存储了步骤的执行计划,执行以下语句检查:

?

?

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

?

?

?

SQL Server Agent Job Sechdule Information:

SQLServer允许在特定时间创建各种计划,每个计划能组合成一个或多个SQLServer Agent Jobs。执行以下脚本查询情况:

?

[sql]?view plaincopyprint?
  1. SELECT??[schedule_uid]?AS?[作业计划ID]?,??
  2. ????????[name]?AS?[作业计划名称]?,??
  3. ????????CASE?[enabled]??
  4. ??????????WHEN?1?THEN?'已启用'??
  5. ??????????WHEN?0?THEN?'未启用'??
  6. ????????END?AS?[是否启用]?,??
  7. ????????CASE?WHEN?[freq_type]?=?64??
  8. ?????????????THEN?'Start?automatically?when?SQL?Server?Agent?starts'??
  9. ?????????????WHEN?[freq_type]?=?128?THEN?'Start?whenever?the?CPUs?become?idle'??
  10. ?????????????WHEN?[freq_type]?IN?(?4,?8,?16,?32?)?THEN?'Recurring'??
  11. ?????????????WHEN?[freq_type]?=?1?THEN?'One?Time'??
  12. ????????END?[作业计划类型]?,??
  13. ????????CASE?[freq_type]??
  14. ??????????WHEN?1?THEN?'One?Time'??
  15. ??????????WHEN?4?THEN?'Daily'??
  16. ??????????WHEN?8?THEN?'Weekly'??
  17. ??????????WHEN?16?THEN?'Monthly'??
  18. ??????????WHEN?32?THEN?'Monthly?-?Relative?to?Frequency?Interval'??
  19. ??????????WHEN?64?THEN?'Start?automatically?when?SQL?Server?Agent?starts'??
  20. ??????????WHEN?128?THEN?'Start?whenever?the?CPUs?become?idle'??
  21. ????????END?[作业运行频率]?,??
  22. ????????CASE?[freq_type]??
  23. ??????????WHEN?4??
  24. ??????????THEN?'Occurs?every?'?+?CAST([freq_interval]?AS?VARCHAR(3))??
  25. ???????????????+?'?day(s)'??
  26. ??????????WHEN?8??
  27. ??????????THEN?'Occurs?every?'?+?CAST([freq_recurrence_factor]?AS?VARCHAR(3))??
  28. ???????????????+?'?week(s)?on?'??
  29. ???????????????+?CASE?WHEN?[freq_interval]?&?1?=?1?THEN?'Sunday'??
  30. ??????????????????????ELSE?''??
  31. ?????????????????END?+?CASE?WHEN?[freq_interval]?&?2?=?2?THEN?',?Monday'??
  32. ????????????????????????????ELSE?''??
  33. ???????????????????????END??
  34. ???????????????+?CASE?WHEN?[freq_interval]?&?4?=?4?THEN?',?Tuesday'??
  35. ??????????????????????ELSE?''??
  36. ?????????????????END?+?CASE?WHEN?[freq_interval]?&?8?=?8?THEN?',?Wednesday'??
  37. ????????????????????????????ELSE?''??
  38. ???????????????????????END??
  39. ???????????????+?CASE?WHEN?[freq_interval]?&?16?=?16?THEN?',?Thursday'??
  40. ??????????????????????ELSE?''??
  41. ?????????????????END?+?CASE?WHEN?[freq_interval]?&?32?=?32?THEN?',?Friday'??
  42. ????????????????????????????ELSE?''??
  43. ???????????????????????END??
  44. ???????????????+?CASE?WHEN?[freq_interval]?&?64?=?64?THEN?',?Saturday'??
  45. ??????????????????????ELSE?''??
  46. ?????????????????END??
  47. ??????????WHEN?16??
  48. ??????????THEN?'Occurs?on?Day?'?+?CAST([freq_interval]?AS?VARCHAR(3))??
  49. ???????????????+?'?of?every?'?+?CAST([freq_recurrence_factor]?AS?VARCHAR(3))??
  50. ???????????????+?'?month(s)'??
  51. ??????????WHEN?32??
  52. ??????????THEN?'Occurs?on?'?+?CASE?[freq_relative_interval]??
  53. ????????????????????????????????WHEN?1?THEN?'First'??
  54. ????????????????????????????????WHEN?2?THEN?'Second'??
  55. ????????????????????????????????WHEN?4?THEN?'Third'??
  56. ????????????????????????????????WHEN?8?THEN?'Fourth'??
  57. ????????????????????????????????WHEN?16?THEN?'Last'??
  58. ??????????????????????????????END?+?'?'?+?CASE?[freq_interval]??
  59. ????????????????????????????????????????????WHEN?1?THEN?'Sunday'??
  60. ????????????????????????????????????????????WHEN?2?THEN?'Monday'??
  61. ????????????????????????????????????????????WHEN?3?THEN?'Tuesday'??
  62. ????????????????????????????????????????????WHEN?4?THEN?'Wednesday'??
  63. ????????????????????????????????????????????WHEN?5?THEN?'Thursday'??
  64. ????????????????????????????????????????????WHEN?6?THEN?'Friday'??
  65. ????????????????????????????????????????????WHEN?7?THEN?'Saturday'??
  66. ????????????????????????????????????????????WHEN?8?THEN?'Day'??
  67. ????????????????????????????????????????????WHEN?9?THEN?'Weekday'??
  68. ????????????????????????????????????????????WHEN?10?THEN?'Weekend?day'??
  69. ??????????????????????????????????????????END?+?'?of?every?'??
  70. ???????????????+?CAST([freq_recurrence_factor]?AS?VARCHAR(3))?+?'?month(s)'??
  71. ????????END?AS?[循环间隔]?,??
  72. ????????CASE?[freq_subday_type]??
  73. ??????????WHEN?1??
  74. ??????????THEN?'Occurs?once?at?'?+?STUFF(STUFF(RIGHT('000000'??
  75. ?????????????????????????????????????????????????????+?CAST([active_start_time]?AS?VARCHAR(6)),??
  76. ?????????????????????????????????????????????????????6),?3,?0,?':'),?6,?0,?':')??
  77. ??????????WHEN?2??
  78. ??????????THEN?'Occurs?every?'?+?CAST([freq_subday_interval]?AS?VARCHAR(3))??
  79. ???????????????+?'?Second(s)?between?'?+?STUFF(STUFF(RIGHT('000000'??
  80. ???????????????????????????????????????????????????????????+?CAST([active_start_time]?AS?VARCHAR(6)),??
  81. ???????????????????????????????????????????????????????????6),?3,?0,?':'),?6,??
  82. ???????????????????????????????????????????????0,?':')?+?'?&?'??
  83. ???????????????+?STUFF(STUFF(RIGHT('000000'??
  84. ???????????????????????????????????+?CAST([active_end_time]?AS?VARCHAR(6)),?6),??
  85. ?????????????????????????????3,?0,?':'),?6,?0,?':')??
  86. ??????????WHEN?4??
  87. ??????????THEN?'Occurs?every?'?+?CAST([freq_subday_interval]?AS?VARCHAR(3))??
  88. ???????????????+?'?Minute(s)?between?'?+?STUFF(STUFF(RIGHT('000000'??
  89. ???????????????????????????????????????????????????????????+?CAST([active_start_time]?AS?VARCHAR(6)),??
  90. ???????????????????????????????????????????????????????????6),?3,?0,?':'),?6,??
  91. ???????????????????????????????????????????????0,?':')?+?'?&?'??
  92. ???????????????+?STUFF(STUFF(RIGHT('000000'??
  93. ???????????????????????????????????+?CAST([active_end_time]?AS?VARCHAR(6)),?6),??
  94. ?????????????????????????????3,?0,?':'),?6,?0,?':')??
  95. ??????????WHEN?8??
  96. ??????????THEN?'Occurs?every?'?+?CAST([freq_subday_interval]?AS?VARCHAR(3))??
  97. ???????????????+?'?Hour(s)?between?'?+?STUFF(STUFF(RIGHT('000000'??
  98. ?????????????????????????????????????????????????????????+?CAST([active_start_time]?AS?VARCHAR(6)),??
  99. ?????????????????????????????????????????????????????????6),?3,?0,?':'),?6,?0,??
  100. ?????????????????????????????????????????????':')?+?'?&?'??
  101. ???????????????+?STUFF(STUFF(RIGHT('000000'??
  102. ???????????????????????????????????+?CAST([active_end_time]?AS?VARCHAR(6)),?6),??
  103. ?????????????????????????????3,?0,?':'),?6,?0,?':')??
  104. ????????END?[计划运行频率]?,??
  105. ????????STUFF(STUFF(CAST([active_start_date]?AS?VARCHAR(8)),?5,?0,?'-'),?8,?0,??
  106. ??????????????'-')?AS?[作业启用开始时间]?,??
  107. ????????STUFF(STUFF(CAST([active_end_date]?AS?VARCHAR(8)),?5,?0,?'-'),?8,?0,??
  108. ??????????????'-')?AS?[作业启用结束时间]?,??
  109. ????????[date_created]?AS?[作业创建日期]?,??
  110. ????????[date_modified]?AS?[作业上次修改日期]??
  111. FROM????[msdb].[dbo].[sysschedules]??
  112. ORDER?BY?[作业计划名称]??
  相关解决方案