使用查询调控器防止查询长时间运行

在SQL Server服务器属性--连接中,有一个名为“使用查询调控器防止查询长时间运行”的选项:

官网解释如下:

query governor cost limit option:

Use the query governor cost limit option to specify an upper limit on the time period in which a query can run. Queries that have estimated run times greater than this limit, return an error and are not executed. Query cost refers to the estimated elapsed time, in seconds, required to complete a query on a specific hardware configuration.If you specify a nonzero, nonnegative value, the query governor disallows execution of any query that has an estimated cost exceeding that value. Specifying 0 (the default) for this option turns off the query governor, and all queries are allowed to run without any time limitation.If you use sp_configure to change the value of query governor cost limit, the changed value is server wide. To change the value on a per-connection basis, use the SET QUERY_GOVERNOR_COST_LIMIT statement.The query governor cost limit option is an advanced option. If you are using the sp_configure system stored procedure to change the setting, you can change query governor cost limit only when show advanced options is set to 1. The setting takes effect immediately (without a server restart).
 
说明此设置是一个cost阈值,无单位。如果某个query的subtreecost大于设置值,则不会执行,而是直接报如下错误:

消息 8649,级别 17,状态 1,服务器 LEOSWONDERWORLD,第 1 行
查询已取消,因为此查询的估计开销(59)超出了配置的阈值 5。请与系统管理员联系。

需要特别注意的一点是,这个设置貌似对sqlserver management studio(ssms.exe)工具无效,无论是在DB Server本地还是远程连接,使用ssms进行的查询都不会受此参数影响。

但是通过链接服务器查询或者osql、sqlcmd则可以验证。

原文地址:https://www.cnblogs.com/leohahah/p/7761483.html