
Configuring Resource Governor to Limit CPU Usage
In this Resource Governor scenario, configuration comprises the following basic steps:
-
Create and configure a Resource Governor resource pool that limits the maximum average CPU bandwidth that will be given to requests in the resource pool when CPU contention occurs.
-
Create and configure a Resource Governor workload group that uses this pool.
-
Create a classifier function, which is a user-defined function (UDF) whose return values are used by Resource Governor for classifying sessions so that they are routed to the appropriate workload group.
-
Register the classifier function with Resource Governor.
-
Apply the changes to the Resource Governor in-memory configuration.
Note: |
|---|
|
For information about Resource Governor resource pools, workload groups, and classification, see Resource Governor Concepts.
|
The Transact-SQL statements for these steps are described in the procedure, "To configure Resource Governor for limiting CPU usage," which is followed by a Transact-SQL example of the procedure.
To configure Resource Governor (SQL Server Management Studio)
To configure Resource Governor for limiting CPU usage (Transact-SQL)
-
Issue a CREATE RESOURCE POOL statement to create a resource pool. The example for this procedure uses the following syntax:
CREATE RESOURCE POOL pool_name WITH ( MAX_CPU_PERCENT = value );
Value is an integer from 1 to 100 that indicates the percentage of maximum average CPU bandwidth. The appropriate value depends on your environment. For the purpose of illustration, the example in this topic uses 20% percent (MAX_CPU_PERCENT = 20.)
-
Issue a CREATE WORKLOAD GROUP statement to create a workload group for low-priority operations whose CPU usage you want to govern. The example for this procedure uses the following syntax:
CREATE WORKLOAD GROUP group_name USING pool_name;
-
Issue a CREATE FUNCTION statement to create a classifier function that maps the workload group created in the preceding step to the user of the low-priority login. The example for this procedure uses the following syntax:
CREATE FUNCTION [schema_name.]function_name() RETURNS sysname
WITH SCHEMABINDING
AS
BEGIN
DECLARE @workload_group_name AS sysname
IF (SUSER_NAME() = 'user_of_low_priority_login')
SET @workload_group_name = 'workload_group_name'
RETURN @workload_group_name
END
For information about the components of this CREATE FUNCTION statement, see:
-
Issue an ALTER RESOURCE GOVERNOR statement to register the classifier function with Resource Governor. The example for this procedure uses the following syntax:
ALTER RESOURCE GOVERNOR WITH (CLASSIFIER_FUNCTION = schema_name.function_name);
-
Issue a second ALTER RESOURCE GOVERNOR statement to apply the changes to the Resource Governor in-memory configuration, as follows:
ALTER RESOURCE GOVERNOR RECONFIGURE;
Example B: Configuring Resource Governor (Transact-SQL)
The following example performs the following steps within a single transaction:
-
Creates the
pMAX_CPU_PERCENT_20 resource pool.
-
Creates the
gMAX_CPU_PERCENT_20 workload group.
-
Creates the
rgclassifier_MAX_CPU() classifier function, which uses the user name created in the preceding example.
-
Registers the classifier function with Resource Governor.
After committing the transaction, the example applies the configuration changes requested in the ALTER WORKLOAD GROUP or ALTER RESOURCE POOL statements.
Important: |
|---|
The following example uses the user name of the sample SQL Server user created in "Example A: Setting Up a Login and User (Transact-SQL)," domain_name\MAX_CPU. Replace this with the name of the user of the login that you plan to use for creating low-priority compressed backups.
|
-- Configure Resource Governor.
BEGIN TRAN
USE master;
-- Create a resource pool that sets the MAX_CPU_PERCENT to 20%.
CREATE RESOURCE POOL pMAX_CPU_PERCENT_20
WITH
(MAX_CPU_PERCENT = 20);
GO
-- Create a workload group to use this pool.
CREATE WORKLOAD GROUP gMAX_CPU_PERCENT_20
USING pMAX_CPU_PERCENT_20;
GO
-- Create a classification function.
-- Note that any request that does not get classified goes into
-- the 'Default' group.
CREATE FUNCTION dbo.rgclassifier_MAX_CPU() RETURNS sysname
WITH SCHEMABINDING
AS
BEGIN
DECLARE @workload_group_name AS sysname
IF (SUSER_NAME() = 'domain_name\MAX_CPU')
SET @workload_group_name = 'gMAX_CPU_PERCENT_20'
RETURN @workload_group_name
END;
GO
-- Register the classifier function with Resource Governor.
ALTER RESOURCE GOVERNOR WITH (CLASSIFIER_FUNCTION= dbo.rgclassifier_MAX_CPU);
COMMIT TRAN;
GO
-- Start Resource Governor
ALTER RESOURCE GOVERNOR RECONFIGURE;
GO
[Top]