Automatically reject certain expensive query statements

The main options are the query governor cost limit and Resource Governor.

The former is a bit limited because it applies to all queries and limits them based on estimated cost, which can be very inaccurate.

Further, SQL Server Management Studio issues SET QUERY_GOVERNOR_COST_LIMIT 0 when connecting (with SSMS options set to default). This overrides the instance-level setting and so renders this option pretty ineffective.

The better option is Resource Governor (if it is available to you) when combined with:

Update adds CPU timeout setting to Resource Governor workgroup REQUEST_MAX_CPU_TIME_SEC in SQL Server 2014, 2016 and 2017

When Trace Flag (TF) 2422 is enabled, and if a request exceeds the maximum time set by REQUEST_MAX_CPU_TIME_SEC, then the request is aborted. Additionally, you may receive an error message 10961 that resembles the following:

The request has been aborted because its CPU usage exceeds the maximum configured limit for the workload group.

It isn't possible to manage this with Extended Events.

Your other options were described by Dominique Boucher:

Why not simply talk to those people who are running those query and told them not to? [...] Remove access from those who run the queries after you told them not to and that should do it. Otherwise, [...] you could have a job that run all the time and check running queries and kill the one you don't what to have running. They will still start to execute but technically, the end user will get errors all the time so they may eventually give up.

Erik Darling had a similar suggestion:

If I knew the query text ahead of time, I'd probably just set up a while loop in an Agent Job to find when those queries are executing, collect the SPID, and kill the session.


Thanks Paul, I had trouble getting governor_cost_limit to kick in when doing a test in SSMS, but as you said, SSMS seem to overrule the server-wide setting.

And I found the option in SSMS here: enter image description here

And of course the server-wide option in GUI (in properties for the server): enter image description here

To set it by SQL statement:

USE AdventureWorks2012;
GO  
EXEC sp_configure 'show advanced options', 1;  
GO  
RECONFIGURE;  
GO  
EXEC sp_configure 'query governor cost limit', 120 ;  
GO  
RECONFIGURE;  
GO  

From docs.microsoft.com

After setting the SSMS option it now kick in when I do a test. It will cancel the query almost immediately (when the server has calculated an estimate), not when the query has elapsed the specified seconds. I get this "error" from the server:

The query has been canceled because the estimated cost of this query (965) exceeds the configured threshold of 10. Contact the system administrator.

Look forward to receive my first mail/phone calls from the clients whining. I have wasted hours on these timeout SQL issues.