1. Resource Governor in action
Now that you have a solid
understanding of the components that make up Resource Governor, let's
walk through an example of its implementation. Let's imagine we have a
database used for both a production point-of-sales application and as a
source for a reporting system. Our goal is to constrain the CPU and
memory resources consumed by the reporting system in order to reduce the
impact on the point-of-sales system when large reports are executed.
Let's walk through the
T-SQL code for creating the necessary components, beginning with the
creation of the workload groups, as shown in listing 1.
Example 1. Creating workload groups
-- Create 2 Workload Groups
USE MASTER
GO
CREATE WORKLOAD GROUP RG_Reporting
GO
CREATE WORKLOAD GROUP RG_Sales
GO
|
Despite the
various options available, we create our workload groups with all of the
default settings. Next up, we'll create the classifier function used to
assign incoming connections to the appropriate group. After the
function is created, we'll configure Resource Governor to use it, as
shown in listing 16.3.
Example 16.3. Classifier function
-- Create a Classifier Function
CREATE FUNCTION ClassifierFn_Basic() RETURNS SYSNAME WITH SCHEMABINDING AS
BEGIN
DECLARE @ResGroup AS SYSNAME
IF (SUSER_NAME() = 'reporting')
SET @ResGroup = 'RG_Reporting'
IF (SUSER_NAME() = 'sales')
SET @ResGroup = 'RG_Sales'
RETURN @ResGroup
END
GO
-- Configure Resource Governor to use the new function
ALTER RESOURCE GOVERNOR WITH (CLASSIFIER_FUNCTION=dbo.ClassifierFn_Basic)
GO
|
In this example, our classifier function uses the login name via the SUSER_NAME()
function as the basis for classification. As we explained earlier, this
could be based on the application, host, or role membership using the
appropriate function.
Our final step is to create the resource pools and assign them to the appropriate workload groups, as per listing 2.
Example 2. Assigning resource pools to workload groups
-- Create a new Resource Pool
CREATE RESOURCE POOL RP_Sales WITH (
MIN_CPU_PERCENT = 40
, MAX_CPU_PERCENT = 60
, MIN_MEMORY_PERCENT = 40
, MAX_MEMORY_PERCENT = 60
)
GO
-- Configure a Workload Group to use the new Resource Pool
ALTER WORKLOAD GROUP RG_Sales USING RP_Sales
GO
-- Create a new Resource Pool
CREATE RESOURCE POOL RP_Reporting WITH (
MIN_CPU_PERCENT = 5
, MAX_CPU_PERCENT = 15
, MIN_MEMORY_PERCENT = 5
, MAX_MEMORY_PERCENT = 15
)
GO
-- Configure a Workload Group to use the new Resource Pool
ALTER WORKLOAD GROUP RG_Reporting USING RP_Reporting
GO
|
In the above example,
we've created resource pools with minimum and maximum CPU and memory
values. At this point, the only step left is to reconfigure the Resource
Governor to enable the new settings. We can do this using the following
command:
-- Reconfigure the Resource Governor to enable the new settings
ALTER RESOURCE GOVERNOR RECONFIGURE
GO
Once the above T-SQL has
been executed, we can confirm our settings using SQL Server Management
Studio by right-clicking Resource Governor and choosing Properties. The
resultant screen, as shown in figure 1, allows us to modify the settings should we wish to do so.
Finally, should we wish to disable Resource Governor, we can do so using the following command:
-- Disable the Resource Governor
ALTER RESOURCE GOVERNOR DISABLE
GO
To complete our coverage of Resource Governor, let's review some of the monitoring options available.