SQL SERVER – Simple Example to Configure Resource Governor – Introduction to Resource Governor

What is Resource Governor?

Resource Governor is a feature which can manage SQL Server Workload and System Resource Consumption. We can limit the amount of CPU and memory consumption by limiting /governing /throttling on the SQL Server.

Why is resource governor required?

If there are different workloads running on SQL Server and each of the workload needs different resources or when workloads are competing for resources with each other and affecting the performance of the whole server resource governor is a very important task.

What will be the real world example of need of resource governor?

Here are two simple scenarios where the resource governor can be very useful.

Scenario 1: A server which is running OLTP workload and various resource intensive reports on the same server. The ideal situation is where there are two servers which are data synced with each other and one server runs OLTP transactions and the second server runs all the resource intensive reports. However, not everybody has the luxury to set up this kind of environment. In case of the situation where reports and OLTP transactions are running on the same server, limiting the resources to the reporting workload it can be ensured that OTLP’s critical transaction is not throttled.

Scenario 2: There are two DBAs in one organization. One DBA A runs critical queries for business and another DBA B is doing maintenance of the database. At any point in time the DBA A’s work should not be affected but at the same time DBA B should be allowed to work as well. The ideal situation is that when DBA B starts working he get some resources but he can’t get more than defined resources.

Does SQL Server have any default resource governor component?

Solarwinds

Yes, SQL Server have two by default created resource governor component.

1) Internal –This is used by database engine exclusives and user have no control.

2) Default – This is used by all the workloads which are not assigned to any other group.

What are the major components of the resource governor?

  • Resource Pools
  • Workload Groups
  • Classification

In simple words here is what the process of resource governor is.

  • Create resource pool
  • Create a workload group
  • Create classification function based on the criteria specified
  • Enable Resource Governor with classification function

Let me further explain you the same with graphical image.

SQL SERVER - Simple Example to Configure Resource Governor - Introduction to Resource Governor ResourceGovernor

Is it possible to configure resource governor with T-SQL?

Yes, here is the code for it with explanation in between.

Step 0: Here we are assuming that there are separate login accounts for Reporting server and OLTP server.

/*-----------------------------------------------
Step 0: (Optional and for Demo Purpose)
Create Two User Logins
1) ReportUser, 2) PrimaryUser
Use ReportUser login for Reports workload
Use PrimaryUser login for OLTP workload
-----------------------------------------------*/

Step 1: Creating Resource Pool

We are creating two resource pools. 1) Report Server and 2) Primary OLTP Server. We are giving only a few resources to the Report Server Pool as described in the scenario 1 the other server is mission critical and not the report server.

-----------------------------------------------
-- Step 1: Create Resource Pool
-----------------------------------------------
-- Creating Resource Pool for Report Server
CREATE RESOURCE POOL ReportServerPool
WITH
( MIN_CPU_PERCENT=0,
MAX_CPU_PERCENT=30,
MIN_MEMORY_PERCENT=0,
MAX_MEMORY_PERCENT=30)
GO
-- Creating Resource Pool for OLTP Primary Server
CREATE RESOURCE POOL PrimaryServerPool
WITH
( MIN_CPU_PERCENT=50,
MAX_CPU_PERCENT=100,
MIN_MEMORY_PERCENT=50,
MAX_MEMORY_PERCENT=100)
GO

Step 2: Creating Workload Group

We are creating two workloads each mapping to each of the resource pool which we have just created.

-----------------------------------------------
-- Step 2: Create Workload Group
-----------------------------------------------
-- Creating Workload Group for Report Server
CREATE WORKLOAD GROUP ReportServerGroup
USING ReportServerPool ;
GO
-- Creating Workload Group for OLTP Primary Server
CREATE WORKLOAD GROUP PrimaryServerGroup
USING PrimaryServerPool ;
GO

Step 3: Creating user defined function which routes the workload to the appropriate workload group.

In this example we are checking SUSER_NAME() and making the decision of Workgroup selection. We can use other functions such as HOST_NAME(), APP_NAME(), IS_MEMBER() etc.

-----------------------------------------------
-- Step 3: Create UDF to Route Workload Group
-----------------------------------------------
CREATE FUNCTION dbo.UDFClassifier()
RETURNS SYSNAME
WITH SCHEMABINDING
AS
BEGIN
DECLARE @WorkloadGroup AS SYSNAME
IF(SUSER_NAME() = 'ReportUser')
SET @WorkloadGroup = 'ReportServerGroup'
ELSE IF (SUSER_NAME() = 'PrimaryUser')
SET @WorkloadGroup = 'PrimaryServerGroup'
ELSE
SET @WorkloadGroup = 'default'
RETURN @WorkloadGroup
END
GO

Step 4: In this final step we enable the resource governor with the classifier function created in earlier step 3.

-----------------------------------------------
-- Step 4: Enable Resource Governer
-- with UDFClassifier
-----------------------------------------------
ALTER RESOURCE GOVERNOR
WITH (CLASSIFIER_FUNCTION=dbo.UDFClassifier);
GO
ALTER RESOURCE GOVERNOR RECONFIGURE
GO

Step 5: If you are following this demo and want to clean up your example, you should run following script. Running them will disable your resource governor as well delete all the objects created so far.

-----------------------------------------------
-- Step 5: Clean Up
-- Run only if you want to clean up everything
-----------------------------------------------
ALTER RESOURCE GOVERNOR WITH (CLASSIFIER_FUNCTION = NULL)
GO
ALTER RESOURCE GOVERNOR DISABLE
GO
DROP FUNCTION dbo.UDFClassifier
GO
DROP WORKLOAD GROUP ReportServerGroup
GO
DROP WORKLOAD GROUP PrimaryServerGroup
GO
DROP RESOURCE POOL ReportServerPool
GO
DROP RESOURCE POOL PrimaryServerPool
GO
ALTER RESOURCE GOVERNOR RECONFIGURE
GO

I hope this introductory example give enough light on the subject of Resource Governor. In future posts we will take this same example and learn a few more details.

Reference: Pinal Dave (https://blog.sqlauthority.com)

Solarwinds
, ,
Previous Post
SQL SERVER – Fix: Error 147 An aggregate may not appear in the WHERE clause
Next Post
SQL SERVER – Fix: Error: 10920 Cannot drop user-defined function. It is being used as a resource governor classifier

Related Posts

40 Comments. Leave new

  • Sathish (@iamsathis)
    June 4, 2012 9:59 am

    Super!!!!!

    Reply
  • Robin Sasson
    June 4, 2012 11:34 am

    Great intro as always.

    Reply
  • Very nice!!

    Reply
  • Thank you Pinal this was a great article! It was the best introduction to resource governor ever!

    Reply
  • juliancastiblancop
    June 6, 2012 3:10 am

    ELSE IF (SUSER_NAME() = ‘PrimaryUser’)
    not
    ELSE IF (SUSER_NAME() = ‘PrimaryServerPool’)

    Reply
  • useful info as always!!

    Reply
  • Great information. You guys with Enterprise edition get to have all the fun.

    Reply
  • How much overhead does the Resource Governor add to the system?

    Reply
  • This was one beautiful example to understand the working and usage of a resource governor. Thanks for the lovely article.

    Reply
  • good article
    i have a use case where in my app disregards the TSQL resultset if it is returned after say 5 seconds, and i cant set timout at app level as the driver is not compatible with sql server so isthere any way with wich i can timeout/fail a tsql query which is taking longer than few seconds ?

    your help will be appreciated, thanks in advance

    Reply
  • Good article!

    I can’t use that feature because I have SQL Server Standard Edition and SQL Governor is supported only by SQL Enterprise and higher. Do you have in mind a third party tool that can do the same thing?

    Reply
  • @pinaldave can we classify the resource governer incoming requests based on stored procedure names? example like i want the SP ‘FullEmpDetails_SP’ to be routed to seperate workgroup… is it possible to classify based on name?

    Reply
  • Informative and very useful as usual

    Reply
  • mehdi paryabi
    August 7, 2013 4:46 pm

    I translate this article to farsi (persian) Language if you want i can present it to you

    Reply
  • mehdi paryabi
    August 7, 2013 4:48 pm

    if you permit i want use it in farsi in iranian web sites

    Reply
  • What can you do in the case where this was done on standard edition and it says it needs to be reconfigured but you can’t since resource governor is not available in standard edition?

    Reply
  • wow ! what a neat & clean explanation of Resource Governor Setting :) thank u soooo much

    Reply
  • Binny Mathew
    June 12, 2014 2:09 pm

    I executed a stored procedure which inserts 100000 records into a table as a user from the default group at it took 28 seconds. I executed the stored procedure logging in as a primary user which is in the primary group having max_cpu_percent = 100 and max_mem_percent = 100 but it took 46 second. Why there was a degradation in the performance of execution?

    Reply
  • arunagiri gopal
    October 1, 2014 4:25 pm

    It is again best one & awesome for new resource governor users :)

    Reply
  • i have used the function
    “create function sysgoverner4()
    returns sysname
    with schemabinding
    as
    begin
    declare @wlgrp as sysname
    declare @db1 varchar(20)=’pruthviraj_test_new’,@db2 varchar(20)=’TestDB_new1′
    if(ORIGINAL_DB_NAME()=@db1)
    set @wlgrp=’pruthviraj’
    else if(ORIGINAL_DB_NAME()=@db2)
    set @wlgrp=’test’
    else
    set @wlgrp=’default’
    return @wlgrp
    end”

    but still the database is using the default when i run a query from “pruthviraj_test_new”

    any suggestions?

    Reply

Leave a Reply

Menu