最近收到一SQL Server数据库服务器的告警邮件,告警内容具体如下所示:
DATE/TIME: 10/23/2018 4:30:26 PM
DESCRIPTION: The query processor ran out of internal resources and could not produce a query plan. This is a rare event and only expected for extremely complex queries or queries that reference a very large number of tables or partitions. Please simplify the query. If you believe you have received this message in error, contact Customer Support Services for more information.
COMMENT: (None)
JOB RUN: (None)
关于“8623 The query processor ran out of internal resources and could not produce a query plan”这个错误,这篇文章不分析错误产生的原因以及解决方案。这里仅仅介绍如何捕获产生这个错误的SQL语句。因为出现这个错误,具体对应的SQL语句不会写入到错误日志。不能定位到具体SQL语句,很难解决这错误。所以解决问题的前提是先定位SQL语句。我们可以通过扩展事件或服务器端跟踪两种方式来定位SQL语句。
扩展事件(Extended Events)捕获
如下所示,脚本只需根据实际情况修改filename、metadatafile参数对应的值。就会创建扩展事件(Extented Events)overly_complex_queries
CREATE EVENT SESSION
overly_complex_queries
ON SERVER
ADD EVENT sqlserver.error_reported
(
ACTION (sqlserver.sql_text, sqlserver.tsql_stack, sqlserver.database_id, sqlserver.username)
WHERE ([severity] = 16
AND [error_number] = 8623)
)
ADD TARGET package0.asynchronous_file_target
(set filename = 'D:\DB_BACKUP\overly_complex_queries.xel' ,
metadatafile = 'D:\DB_BACKUP\overly_complex_queries.xem',
max_file_size = 10,
max_rollover_files = 5)
WITH (MAX_DISPATCH_LATENCY = 5SECONDS)
GO
-- Start the session
ALTER EVENT SESSION overly_complex_queries
ON SERVER STATE = START
GO