SQL Server捕获发生The query processor ran out of internal resources and could not produce a...

栏目: 数据库 · 发布时间: 5年前

内容简介:最近收到一SQL Server数据库服务器的告警邮件,告警内容具体如下所示:DATE/TIME:10/23/2018 4:30:26 PM

最近收到一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

然后我们测试,使用网上一个脚本测试验证,如下所示,执行这个脚本就会报 8623 The query processor ran out of internal resources and could not produce a query plan” 错误,如下所示:

SQL Server捕获发生The query processor ran out of internal resources and could not produce a...

选中扩展事件(Extented Events)overly_complex_queries,单击右键 Watch Live Data"就能查看是那个SQL语句出现这个错误(sql_text),当然,也可以通过选项 View Target Data 查看所有捕获的数据。

SQL Server捕获发生The query processor ran out of internal resources and could not produce a...

注意:这个扩展事件只能运行在SQL Server 2012及后续版本,如果是SQL Server 2008的相关版本部署,就会报下面错误:

Msg 25706, Level 16, State 8, Line 1

The event attribute or predicate source, "error_number", could not be found.

Msg 15151, Level 16, State 1, Line 18

Cannot alter the event session 'overly_complex_queries', because it does not exist or you do not have permission.

服务器端跟踪(Server Side Trace)捕获

如上所示,刚好我们这台数据库服务器的版本为SQL Server 2008 R2,我们只能采取Server Side Trace来捕获这个错误的SQL语句。设置Server Side Trace脚本如下(相关参数需根据实际情况等设定):

-- 定义参数  
declare @rc int  
declare @TraceID int  
declare @maxfilesize bigint  
set @maxfilesize = 1024   
 
-- 初始化跟踪  
exec @rc = sp_trace_create @TraceID output, 0, N'D:\SQLScript\trace_error_8623', @maxfilesize, NULL   
--此处的D:\SQLScript\trace_error_8623是文件名(可自行修改),SQL会自动在后面加上.trc的扩展名  
if (@rc != 0) goto error  
 
-- 设置跟踪事件  
declare @on bit  
set @on = 1  
 
 
--trace_event_id=13  SQL:BatchStarting   trace_event_id=22 ErrorLog
exec sp_trace_setevent @TraceID, 13, 1,  @on    
exec sp_trace_setevent @TraceID, 13, 3,  @on  
exec sp_trace_setevent @TraceID, 13, 6,  @on  
exec sp_trace_setevent @TraceID, 13, 7,  @on  
exec sp_trace_setevent @TraceID, 13, 8,  @on  
exec sp_trace_setevent @TraceID, 13, 11, @on  
exec sp_trace_setevent @TraceID, 13, 12, @on 
exec sp_trace_setevent @TraceID, 13, 14, @on 
exec sp_trace_setevent @TraceID, 13, 15, @on 
exec sp_trace_setevent @TraceID, 13, 35, @on  
exec sp_trace_setevent @TraceID, 13, 63, @on  
 
exec sp_trace_setevent @TraceID, 22, 1,  @on    
exec sp_trace_setevent @TraceID, 22, 3,  @on  
exec sp_trace_setevent @TraceID, 22, 6,  @on  
exec sp_trace_setevent @TraceID, 22, 7,  @on  
exec sp_trace_setevent @TraceID, 22, 8,  @on  
exec sp_trace_setevent @TraceID, 22, 12, @on 
exec sp_trace_setevent @TraceID, 22, 11, @on  
exec sp_trace_setevent @TraceID, 22, 14, @on 
exec sp_trace_setevent @TraceID, 22, 14, @on 
exec sp_trace_setevent @TraceID, 22, 35, @on  
exec sp_trace_setevent @TraceID, 22, 63, @on  
-- 启动跟踪  
exec sp_trace_setstatus @TraceID, 1  
 
-- 记录下跟踪ID,以备后面使用  
select TraceID = @TraceID  
goto finish  
 
error:   
select ErrorCode=@rc  
 
finish:   
GO  

上面SQL会生成一个服务器端跟踪事件,并返回对应的id,如下查看所示:

SQL Server捕获发生The query processor ran out of internal resources and could not produce a...

注意:上面捕获 SQL:BatchStarting 事件( trace_event_id=13 ),是因为捕获 ErrorLogtrace_event_id=22) 等事件时,都

无法捕获到对应的 SQL( 对应的 trace column 没有捕获 SQL 语句,暂时还没有找到一个好的解决方法)。这里也有个弊端,就是会捕获大量无关的 SQL 语句

测试过后,你可以使用SQL Profile工具打开D:\SQLScript\trace_error_8623.trc找到错误信息,对应的SQL语句(在这个时间点附近的SQL语句,一般为是错误信息后面的第一个SQL语句,需要做判断),如下截图所示:

SQL Server捕获发生The query processor ran out of internal resources and could not produce a...

也可以使用脚本查询,如下所示,也是需要自己判断定位SQL语句,一般都是 8623 The query processor ran out of internal resources and could not produce a query plan 出现后紧接着的SQL。

SELECT StartTime,EndTime,
    TextData, ApplicationName,SPID,Duration,LoginName
FROM ::fn_trace_gettable(N'D:\SQLScript\trace_error_8623.trc',DEFAULT)
WHERE spid=64
ORDER BY StartTime

SQL Server捕获发生The query processor ran out of internal resources and could not produce a...

参考资料:

https://www.mssqltips.com/sqlservertip/5279/sql-server-error-query-processor-ran-out-of-internal-resources-and-could-not-produce-a-query-plan/

https://www.mssqltips.com/sqlservertip/1035/sql-server-performance-statistics-using-a-server-side-trace/


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

浴缸里的惊叹

浴缸里的惊叹

顾森 / 人民邮电出版社 / 2014-7 / 49.00元

《浴缸里的惊叹》是一本趣题集,里面的题目全部来自于作者顾森十余年来的精心收集,包括几何、组合、行程、数字、概率、逻辑、博弈、策略等诸多类别,其中既有小学奥数当中的经典题目,又有世界级的著名难题,但它们无一例外都是作者心目中的“好题”:题目本身简单而不容易,答案出人意料却又在情理之中,解法优雅精巧令人拍案叫绝。作者还有意设置了语言和情境两个类别的问题,希望让完全没有数学背景的读者也能体会到解题的乐趣......一起来看看 《浴缸里的惊叹》 这本书的介绍吧!

HTML 压缩/解压工具
HTML 压缩/解压工具

在线压缩/解压 HTML 代码

JS 压缩/解压工具
JS 压缩/解压工具

在线压缩/解压 JS 代码

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具