SQL Server警报解决性能问题:如何创建和使用SQL Server警报

Before creating a SQL Server alert, make sure that Database Mail and SQL Server Agent properties are properly configured

在创建SQL Server警报之前,请确保正确配置了数据库邮件SQL Server代理属性。

配置数据库邮件 (Configure Database Mail)

  1. Management node in SQL Server Management Studio管理”节点
  2. Right-click Database Mail and select Send Test Database mail
    If you receive the message that there are no Database mail profiles, you have to create an email profile first

    右键单击“ 数据库邮件”,然后选择“ 发送测试数据库邮件”
    如果收到消息说没有数据库邮件配置文件,则必须先创建一个电子邮件配置文件

  3. Yes. Another option is to right-click 。 另一个选项是右键单击“ Database Mail and select 数据库邮件”,然后选择“ Configure Database Mail配置数据库邮件”
  4. Create a database mail account using your mail server parameters and credentials

    使用您的邮件服务器参数和凭据创建数据库邮件帐户

    Database mail configuration wizard - manage an existing account dialog

  5. Create a database mail profile and add the mail account created in the previous step

    创建数据库邮件配置文件并添加在上一步中创建的邮件帐户

    Database mail configuration wizard - manage an existing profile dialog

配置SQL Server代理 (Configure SQL Server Agent)

Another very important step, often skipped when creating an alert, is setting SQL Server Agent properties:

创建警报时通常会跳过的另一个非常重要的步骤是设置SQL Server代理属性:

  1. SQL Server Agent in 对象资源管理器”中的“ Object Explorer and select SQL Server代理” ,然后选择“ Properties属性”
  2. Alert System tab警报系统选项卡
  3. Enable mail profile option启用邮件配置文件选项
  4. Mail system is Database Mail邮件系统是数据库邮件
  5. Mail profile from the drop-down list邮件配置文件
  6. Make sure that Include body of e-mail in notification message is selected

    确保已选中在通知消息包括电子邮件正文

    SQL Server agent properties dialog

  7. Enable fail-safe operator option and select the operator from the list启用故障安全操作员”选项,然后从列表中选择操作员
  8. OK确定
  9. SQL Server Agent, as changes might not be applied automaticallySQL Server代理 ,因为更改可能不会自动应用

If the mail profile is not enabled for SQL Server Agent, you will be able to create SQL Server alerts and set email notifications successfully. There will be no error messages, nor warnings, yet emails will not be sent. To find out why the email notifications are not sent, you will have to check the SQL Server error logs, they will clearly indicate that the problem is that Database Mail is not enabled for SQL Server Agent notifications

如果未为SQL Server代理启用邮件配置文件,则可以创建SQL Server警报并成功设置电子邮件通知。 没有错误消息,也没有警告,但不会发送电子邮件。 要找出为什么不发送电子邮件通知的原因,您将必须检查SQL Server错误日志,它们将清楚表明问题是未为SQL Server代理通知启用数据库邮件

Dialog showing SQL Server error logs

创建一个SQL Server警报 (Creating a SQL Server alert)

Alerts can be created using SQL Server Management Studio or T-SQL. When creating an alert, you have to specify its name, the event or threshold value that fires the alert, and the action executed when the alert is fired

可以使用SQL Server Management Studio或T-SQL创建警报。 创建警报时,必须指定其名称,触发警报的事件或阈值以及触发警报时执行的操作

  1. Make sure that SQL Server Agent is running. If not, right click SQL Server Agent in SQL Server Management Studio Object Explorer and select Start. Consider setting SQL Server Agent to start up automatically
  2. 确保SQL Server代理正在运行。 如果不是,请在SQL Server Management Studio 对象资源管理器中右键单击“ SQL Server代理” ,然后选择“ 启动” 。 考虑将SQL Server代理设置为自动启动
  3. Alerts node in the SQL Server代理”中的“ SQL Server Agent and select 警报”节点,然后选择“ New Alert新建警报”
  4. SQL Server performance condition as the alert type from the drop-down menu. Other two available options are SQL Server event and WMI eventSQL Server性能条件作为警报类型。 其他两个可用选项是SQL Server事件和WMI事件
  5. Buffer Manager. Other available objects are Access Methods, Availability Replica, Buffer Node, Catalog Metadata, Databases, General Statistics, Latches, Locks, and many more Buffer Manager 。 其他可用对象包括访问方法,可用性副本,缓冲区节点,目录元数据,数据库,常规统计信息,闩锁,锁等

    These object types can be obtained by executing

    这些对象类型可以通过执行

     
    select distinct object_name 
    from sys.dm_os_performance_counters
     
    	
    
  6. The list of available counters shows all Buffer Memory counters. Again, you can see them when querying the sys. dm_os_performance_counters view, or in Performance Monitor. In this example, we’ll select Page Life Expectancy
  7. 可用计数器列表显示所有缓冲存储器计数器。 再次,您可以在查询系统时看到它们 dm_os_performance_counters视图或在Performance Monitor中 。 在此示例中,我们将选择“预期页面寿命”
  8. The instance name is the database name, Total if the SQL Server alert is applied to all databases, counter specific information, or blank. This is the information returned in the instance_name column by the sys. dm_os_performance_counters view
  9. 实例名称是数据库名称,如果SQL Server警报应用于所有数据库,则为Total,计数器特定信息,或为空白。 这是sysinstance_name列中返回的信息 dm_os_performance_counters视图
  10. The Alert if counter field can have falls below, becomes equal to, or rises above values

    As the normal values for Page Life Expectancy are above 300 seconds (5 minutes), we’ll select falls under and enter 300 in the Value field

    Configuring the SQL Server alert based on Page Life Expectancy counter

    Note that the counters are queried periodically and that there might be a slight delay before the alert is triggered

    Here is an example for setting Memory Grants Pending. Its value represents the total number of SQL Server processes waiting to be granted workspace in the memory. The threshold value is zero. If any processes are waiting for the memory workspace, they are queued and the Memory Grants Pending value is higher. The alert will be fired if the counter rises above 0

  11. 如果计数器字段可以低于,等于高于,则发出 警报

    由于“ 网页预期寿命”的正常值超过300秒(5分钟),因此我们将选择“ 低于” ,然后在“ 值”字段中输入300

    根据“页面预期寿命”计数器配置SQL Server警报

    请注意,会定期查询计数器,并且触发警报之前可能会稍有延迟

    这是设置Memory Grants Pending的示例。 它的值表示等待在内存中被授予工作区SQL Server进程总数。 阈值为零。 如果有任何进程在等待内存工作空间,则它们会排队,并且“ 内存授权未决”值会更高。 如果计数器上升到0以上,则会触发警报

  12. Response tab, specify what event type you want to trigger by the alarm. Available options are to execute a SQL Server job, and to send a notification to an operator. You can select both for a single SQL Server alert. To be able to create a response, you must be a member of the sysadmin role响应”选项卡中,指定要由警报触发的事件类型。 可用选项包括执行SQL Server作业以及向操作员发送通知。 您可以为单个SQL Server警报选择两者。 为了能够创建响应,您必须是sysadmin角色的成员
  13. Execute job option, and an existing job from the drop-down list. If the job doesn’t exist, click the 执行作业选项,然后从下拉列表中选择一个现有的作业。 如果作业不存在,请单击“ New Job button and create a new job新建作业”按钮并创建一个新作业
  14. To send a notification, select Notify operators

    要发送通知,请选择“ 通知操作员”

    Dialog showing the Execute job option

  15. If the operator list is empty, select New Operator and fill in the records

    如果操作员列表为空,请选择“ 新操作员”并填写记录

    Dialog showing New operator set up

  16. In the Notifications tab, select E-mail for the alert type

    在“ 通知”选项卡中,选择“电子邮件”作为警报类型

    Another way to add a notification is using the sp_add_notification stored procedure

    添加通知的另一种方法是使用sp_add_notification存储过程

    USE [msdb]
    GO
     
    EXEC dbo.sp_add_notification
     @alert_name = N'PLE_Alert',
     @operator_name = N'Millie',
     @notification_method = 1 
    GO
     
    

    The @notification_method parameter can be 1,2, or 4 for emails, pager, and net send, respectively. Only members of the sysadmin SQL Server role can execute the sp_add_notification stored procedure

    对于电子邮件,寻呼机和网络发送,@ notification_method参数可以分别为1,2或4。 只有sysadmin SQL Server角色成员才能执行sp_add_notification存储过程

  17. OK and the new Operator will appear in the Operator list of the 确定 ,新的操作员将出现在“ Alert Properties dialog警报属​​性”对话框的“操作员”列表中
  18. Options tab in the 警报属​​性”对话框中打开“ Alert Properties dialog选项”选项
  19. Select to include the error message into emails sent and add additional text that will provide more details about the situation that triggered the alert

    选择以将错误消息包含在已发送的电子邮件中,并添加其他文本,以提供有关触发警报情况的更多详细信息

    Alert Properties - include the error message dialog

  20. Set the delay between responses. The default is 0 minutes and 0 seconds. Keep in mind that sending frequent email notifications until the issue is fixed and the condition doesn’t trigger the alarm might lead to sending too many emails, block your email client and affect the complete system performance

    设置响应之间的延迟。 默认值为0分钟0秒。 请记住,在问题解决之前,要频繁发送电子邮件通知,并且情况不会触发警报,否则可能会导致发送过多电子邮件,阻塞电子邮件客户端并影响整个系统性能
  21. Open the History tab, to see how many times the SQL Server alert has occurred, the last occurrence, and the last response to the alert. To reset the number of captured alerts and other parameters shown in this tab, check the Reset count check box

    打开“ 历史记录”选项卡,以查看SQL Server警报发生了多少次,最后一次发生以及对警报的最后响应。 要重置此选项卡中显示的已捕获警报的数量和其他参数,请选中“ 重置计数”复选框

    History tab - see how many times the SQL Server alert has occurred

The same alert can be created using T-SQL and the dbo.sp_add_alert stored procedure. Note that it has to be executed against the msdb database by a member of a sysadmin role

可以使用T-SQL和dbo.sp_add_alert存储过程来创建相同的警报。 请注意,它必须由sysadmin角色的成员针对msdb数据库执行

 
    
USE [msdb]
GO
 
EXEC msdb.dbo.sp_add_alert @name=N'PLE_Alert', 
		@message_id=0, 
		@severity=0, 
		@enabled=1, 
		@delay_between_responses=300, 
		@include_event_description_in=1, 
		@notification_message=N'Page Life Expectancy has fallen under 300 seconds', 
		@category_name=N'[Uncategorized]', 
		@performance_condition=N'Buffer Manager|Page life expectancy||<|300', 
		@job_id=N'00000000-0000-0000-0000-000000000000'
GO
 
 

When an alert is fired on the FUJITSU\SQL2012 instance, you’ll get an email with the SQL Server Alert System: ‘PLE_Alert’ occurred on \\FUJITSU\SQL2012 subject and the following text in the email body:

在FUJITSU \ SQL2012实例上触发警报时,您将收到一封包含SQL Server警报系统的电子邮件:\\ FUJITSU \ SQL2012主题上出现了“ PLE_Alert”,并且电子邮件正文中包含以下文本:

DATE/TIME: 2/19/2014 1:08:41 PM

日期/时间:2014/2/19下午1:18:41

DESCRIPTION: The SQL Server performance counter ‘s’ (instance ‘s’) of object ‘s’ is now below the threshold of s (the current value is s).

描述:对象“ s”SQL Server性能计数器“ s”(实例“ s”)现在低于s的阈值(当前值为s)。

COMMENT: Page Life Expectancy has fallen under 300 seconds

评论:预期页面寿命低于300秒

JOB RUN: (None)

求职:(无)

Disabling an alert
To stop the alert from being fired, but still keep it as inactive, disable it. You can easily enable it and reuse later

禁用警报
要停止触发警报,但仍将其保持为非活动状态,请将其禁用。 您可以轻松启用它,以后再使用

  1. Expand the SQL Server Agent node in SQL Server Management Studio Object Explorer
  2. 在SQL Server Management Studio 对象资源管理器中展开“ SQL Server代理”节点
  3. Select the alert you want to disable and right-click it

    选择要禁用的警报,然后右键单击它
  4. Disable禁用

The same can be done using T-SQL. Only members of the sysadmin SQL server role can execute the sp_update_alert stored procedure

使用T-SQL可以完成相同的操作。 只有sysadmin SQL Server角色成员才能执行sp_update_alert存储过程

 
USE msdb
GO
 
EXEC dbo.sp_update_alert
    @name = N'Test Alert',
    @enabled = 0
GO
 
 

When set to 0, the @enabled parameter disables the alarm. To enable the alarm, use same code except for @enabled = 1

设置为0时,@enabled参数禁用警报。 要启用警报,请使用相同的代码,但@enabled = 1除外

Note that all T-SQL commands given in this article, unless qualified as msdn. dbo.<procedure_name> have to be executed against the msdb database. Otherwise, you’ll get the error message that the procedure cannot be found

请注意,除非具有msdn的资格,否则本文中提供的所有T-SQL命令。 dbo。<procedure_name>必须针对msdb数据库执行。 否则,您将收到错误消息,指出找不到该过程

As shown, the Alert feature in SQL Server Agent is very useful for automating performance issue notifications. It enables creating SQL Server alerts for every available SQL Server performance counter. For each counter, you can set a custom threshold value and specify whether the expected values should be higher or lower. However, it is not possible to set two values for a counter in the same alert. For example, if the counter value is lower than the value A – fire an alarm with a notification about a medium severity issue, and if the counter value falls below the value B – fire a critical alarm

如图所示, SQL Server代理中警报功能对于自动执行性能问题通知非常有用。 它允许为每个可用SQL Server性能计数器创建SQL Server警报。 对于每个计数器,您可以设置一个自定义阈值并指定期望值应该更高还是更低。 但是,不可能在同一警报中为计数器设置两个值。 例如,如果计数器值小于值A –发出带有中等严重性问题通知的警报,并且如果计数器值小于值B则触发严重警报

Email notifications can be seamlessly sent to a number of operators, or to a mailing list. Also, sending pager and net send notifications are available, but will be depreciated in the future SQL Server versions

电子邮件通知可以无缝地发送给许多操作员或邮件列表。 此外,还可以使用发送寻呼机和网络发送通知,但在将来SQL Server版本中将不推荐使用

Setting multiple alarms is not very user friendly, as you have to create a single alarm for each counter you want to monitor, therefore go through a number of steps. The feature is supported only in Enterprise and Business Intelligence SQL Server editions

设置多个警报不是非常用户友好,因为您必须为要监视的每个计数器创建一个警报,因此需要执行许多步骤。 仅企业版和商业智能SQL Server版本支持该功能

翻译自: https://www.sqlshack.com/sql-server-alerts-troubleshooting-performance-issues-create-use-sql-server-alerts/

  • 0
    点赞
  • 0
    评论
  • 1
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

表情包
插入表情
评论将由博主筛选后显示,对所有人可见 | 还能输入1000个字符
©️2021 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值