
Resource Governor (Enterprise edition) puts incoming sessions into workload groups and limits the CPU, memory and I/O each group can use. The typical use is stopping reporting or ad hoc queries from starving the main application.
Tested on SQL Server 2022 (Developer edition, single test instance).
When to use Resource Governor
Resource Governor is worth setting up when a few heavy workloads share a server with an application that needs steady response times: overnight reports that overlap with the working day, analysts running ad hoc queries against production, or maintenance jobs that compete with users. It works by matching each new session to a workload group, and each group draws from a resource pool with its own limits. It does not make queries faster; it decides who gets what when the server is busy. For the full reference, see Microsoft’s Resource Governor documentation.
Step 1: Pool and workload group
USE master;
CREATE RESOURCE POOL ReportingPool
WITH (MAX_CPU_PERCENT = 30, MAX_MEMORY_PERCENT = 30);
CREATE WORKLOAD GROUP ReportingGroup
WITH (REQUEST_MAX_MEMORY_GRANT_PERCENT = 15, MAX_DOP = 4)
USING ReportingPool;
GO
The maximum CPU percent setting only applies when there is CPU contention. Use the CPU cap setting for a hard limit.
Step 2: Classifier function
The classifier runs for every new connection and returns the name of the workload group to use.
USE master;
GO
CREATE FUNCTION dbo.fn_RGClassifier()
RETURNS sysname
WITH SCHEMABINDING
AS
BEGIN
DECLARE @group sysname = N'default';
IF APP_NAME() LIKE N'%Report%' OR SUSER_SNAME() = N'DOMAIN\svc_reporting'
SET @group = N'ReportingGroup';
RETURN @group;
END;
GO
Keep it short and avoid table lookups. A slow or failing classifier delays or blocks every login. If that happens, connect with the dedicated admin connection (ADMIN:ServerName), which bypasses it.
Step 3: Switch it on
ALTER RESOURCE GOVERNOR WITH (CLASSIFIER_FUNCTION = dbo.fn_RGClassifier);
ALTER RESOURCE GOVERNOR RECONFIGURE;
Check it is working
-- Which group each user session landed in
SELECT s.session_id, s.login_name, s.program_name, g.name AS workload_group
FROM sys.dm_exec_sessions s
JOIN sys.dm_resource_governor_workload_groups g ON g.group_id = s.group_id
WHERE s.is_user_process = 1;
-- Usage by pool
SELECT name, total_cpu_usage_ms, used_memory_kb, max_memory_kb
FROM sys.dm_resource_governor_resource_pools;
Switching it off
ALTER RESOURCE GOVERNOR WITH (CLASSIFIER_FUNCTION = NULL);
ALTER RESOURCE GOVERNOR RECONFIGURE;
-- or, to stop it completely
ALTER RESOURCE GOVERNOR DISABLE;
Watch out for
- Sessions keep their group until they reconnect, so connection pools may take a while to pick up a change.
- Memory limits apply to query memory grants, not the buffer pool. Resource Governor will not stop a report from pushing other data out of cache.
- I/O can be limited per volume by setting minimum and maximum IOPS per volume on the pool.
From my SQL Server DBA handbook.