最近收到一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”错误,如下所示:

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

 

注意:这个扩展事件只能运行在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:BatchStarting事件(trace_event_id=13),是因为捕获ErrorLog(trace_event_id=22)等事件时,都

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

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

也可以使用脚本查询,如下所示,也是需要自己判断定位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

参考资料:

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/

SQL Server捕获发生The query processor ran out of internal resources and could not produce a query plan...错误的SQL语句的更多相关文章

  1. 捕获和记录SQL Server中发生的死锁

    经带在论坛上看到有人在问怎么捕获和记录死锁信息,在这里,我将自己的一些心得贡献出来,与大家分享,也请各位指正. 我们知道,可以使用SQL Server自带的Profiler工具来跟踪死锁信息.但这种方 ...

  2. (4.7)怎么捕获和记录SQL Server中发生的死锁?

    转自:https://blog.csdn.net/c_enhui/article/details/19498327 怎么捕获和记录SQL Server中发生的死锁? 关键词:死锁记录,死锁捕获 sql ...

  3. 怎么捕获和记录SQL Server中发生的死锁

    我们知道,可以使用SQL Server自带的Profiler工具来跟踪死锁信息.但这种方式有一个很大的敝端,就是消耗很大.据国外某大神测试,profiler甚至可以占到服 务器总带宽的35%,所以,在 ...

  4. SQL SERVER数据库备份时出现“操作系统错误5(拒绝访问)。BACKUP DATABASE 正在异常终止。”错误的解决办法

    一般备份文件选择的目录为磁盘根目录或备份所选分区未授予sqlserver用户读写权限时会出现此错误. 解决办法就是给sqlserver用户授予权限: 选择要备份的文件夹 ,右键-->属性--&g ...

  5. SQL Server 之 在与SQLServer建立连接时出现与网络相关的或特定于实例的错误

    背景:在用数据库时,打开SQL Server 2008 R2 的 SQL Server Management Studio,输入sa的密码发现,无法登陆数据库,提示信息如上: 解决方案: 1.打开Sq ...

  6. 解决SQL SERVER数据库备份时出现“操作系统错误5(拒绝访问)。BACKUP DATABASE 正在异常终止。”错误的解决办法

    SQL SERVER数据库进行备份时出现“操作系统错误5(拒绝访问).BACKUP DATABASE 正在异常终止.”错误.我们应该如何解决这个问题?小编今天为大家推荐一个解决办法. 一般备份文件选择 ...

  7. sql server 2005/2008R2 报“红叉”错,即“不允许所请求的注册表访问权”的错误

    一.使用报错展示:           1.红叉错: 2.报错文字信息: 解决办法:可以鼠标右键,以管理员的身份运行即可,但这治标不治本,按如下方法可以彻底解决:把“以管理员身份运行此程序”勾上,即可

  8. sql server报【从varchar数据类型到datetime数据类型的转换产生一个超出范围的值】错误的解决办法

    产生这个错误的原因是在使用convert函数将给定的日期字符串转换为日期类型的时候,因为datetime这个数据类型有时间数值的范围限定,当超出时间范围时就抛出这个错误. 如果类型是[datetime ...

  9. Microsoft SQL Server Version List [sqlserver 7.0-------sql server 2016]

    http://sqlserverbuilds.blogspot.jp/   What version of SQL Server do I have? This unofficial build ch ...

随机推荐

  1. [Swift]LeetCode485. 最大连续1的个数 | Max Consecutive Ones

    Given a binary array, find the maximum number of consecutive 1s in this array. Example 1: Input: [1, ...

  2. [Swift]LeetCode707. 设计链表 | Design Linked List

    Design your implementation of the linked list. You can choose to use the singly linked list or the d ...

  3. SSM框架报HTTP Status 500 - Request processing failed; nested exception is java.lang.NullPointerException错

    如下图 一番排查之后发现原来是server层写漏注释了 粗心大意,一天内出现两次写漏注释,SSM框架有意思.

  4. python高级-装饰器(19)

    一.什么是闭包 先看一个例子: #定义一个函数 def test(number): #在函数内部在定义一个函数,并且这个函数用到外围函数的变量 #那么将这个函数及用到的一些变量称之为闭包 def te ...

  5. TDD in .NET Core - 简介

    本文很多内容来自选自TDD实例一书. 预备知识 最好有一些预备知识,例如xUnit,Moq,如何编写易于测试的代码,这些内容我都写了文章:https://www.cnblogs.com/cgzl/p/ ...

  6. [Abp 源码分析]一、Abp 框架启动流程分析

    Abp 不一定仅用于 Asp.Net Core 项目,他也可以在 Console 与 WinFrom 项目当中进行使用,所以关于启动流程可以分为两种,一种是 Asp.Net Core 项目的启动流程, ...

  7. github pages代码高亮highlighter

    github pages 一直想添加代码高亮 highlighter ,基于 jekyll 3.0 的 rouge 终于搞定了: 下载代码高亮库 在 cmd 中输入: rougify style mo ...

  8. Python 字典和集合基于哈希表实现

    哈希表作为基础数据结构我不多说,有兴趣的可以百度,或者等我出一篇博客来细谈哈希表.我这里就简单讲讲:哈希表不过就是一个定长数组,元素找位置,遇到哈希冲突则利用 hash 算法解决找另一个位置,如果数组 ...

  9. 网络协议 4 - 交换机与 VLAN:办公室太复杂,我要回学校

        上一次,我们通过宿舍联网打魔兽的需求,认识了如何通过物理层和链路层组建一个宿舍局域网.今天,让我们切换到稍微复杂点的场景,办公室.     在这个场景里,就不像在宿舍那样,搞几根网线,拉一拉, ...

  10. .NET Core实战项目之CMS 第七章 设计篇-用户权限极简设计全过程

    写在前面 这篇我们对用户权限进行极简设计并保留其扩展性.首先很感谢大家的阅读,前面六章我带着大家快速入门了ASP.NET Core.ASP.NET Core的启动过程源码解析及配置文件的加载过程源码解 ...