resource pool, this setting determines whether requests within one workload group run at a higher or lower priority than other workload groups within the same resource pool.. Memory Gran
Trang 1resource pool, this setting determines whether requests within one workload group
run at a higher or lower priority than other workload groups within the same
resource pool MEDIUMis the default setting Currently, the weighting factors for each
setting is LOW=1,MEDIUM=3, and HIGH=9 This means that the scheduler will attempt to
execute sessions in workgroups with importance of HIGHthree times more often than
workgroups with MEDIUMimportance, and nine times more often workgroups with
LOWimportance
NOTE
Try to avoid having too many sessions in groups with high importance or assigning high
importance to too many groups because the sessions will likely end up getting only
equal time on the scheduler as your medium and low priority sessions
Maximum Requests—Specifies the maximum number of simultaneous requests
allowed to execute in the workload group The default setting, 0, allows unlimited
requests
CPU Time—Specifies the maximum amount of CPU time, in seconds, that a request
within the workload group can use The default setting is 0, which means unlimited
Memory Grant %—Specifies, as a percentage, the maximum amount of execution
grant memory that a single request can take from the resource pool This percentage
is relative to the amount of memory allocated to the resource pool The allowed
range of values is from 0through100 The default setting is 25 Execution grant
memory is the amount of memory used for query execution, not for data buffers or
cached plans, which can be shared by many sessions, regardless of resource pool or
workload group Note that setting this value to 0prevents queries with SORTand
HASH JOINoperations in user-defined workload groups from running It is also not
recommended that this value be set greater than 70because the server may be
unable to set aside enough free memory if other concurrent queries are running
Grant Time-out—Specifies the maximum time, in seconds, that a query waits for a
resource to become available If the resource does not become available, the process
may fail with a time-out error Note that a query does not always fail when the grant
time-out is reached A query fails only if there are too many concurrent queries
running Otherwise, the query may run with reduced resources, resulting in reduced
query performance The default setting is 0, which means the server calculates the
time-out using an internal calculation based on query cost to determine the
maximum time
Degree of Parallelism—Specifies the maximum degree of parallelism (DOP) for
parallel queries This values takes precedence over the global max degree of
paral-lelism configuration setting, as well as any query hints The allowed range of values
is from 0through64 The default setting is 0, which means that processes use the
global setting Be aware that MAX_DOPspecifies an upper limit only The actual degree
Trang 2of parallelism is determined by the server based on the actual number of schedulers
and available number of parallel threads, which may be less than the specified
MAX_DOP To better understand how the MAX_DOPsetting is handled, consider the
following:
MAX_DOPas a query hint is considered only if it does not exceed the workload
groupMAX_DOPsetting
MAX_DOPas a query hint always overrides the max degree of parallelism server
configuration option
Workload group MAX_DOPalways overrides the max degree of parallelism server
configuration option
If a query is marked as serial at compile time, it cannot be changed back to
paral-lel at runtime regardless of the workload group or server configuration setting
When the degree of parallelism is decided, it can be lowered only when
memory pressure occurs Workload group reconfiguration is not seen for tasks
already waiting in the grant memory queue
To verify that the new workload group was created, in SSMS Object Explorer, expand the
Resource Governornode, expand the Resource Poolsfolder, expand the ReportPool
node, and finally, expand the Workload Groupsfolder You should then see a folder
namedReportWG1
Creating Workload Groups in T-SQL
Now that you’ve set up the ReportWG1workload group in SSMS, you are able to set up a
second workload group, OLTPWG1, using T-SQL The command to create a resource pool,
CREATE RESOURCE POOL, takes five optional arguments:
REQUEST_MAX_MEMORY_GRANT_PERCENT,REQUEST_MAX_CPU_TIME_SEC,GROUP_MAX_REQUESTS,
REQUEST_MEMORY_GRANT_TIMEOUT_SEC, and MAX_DOP, which were described in the preceding
section
CREATE WORKLOAD GROUP OLTPWG1
WITH ( IMPORTANCE = HIGH )
USING OLTPPool
ALTER RESOURCE GOVERNOR RECONFIGURE
GO
To view the workload groups in T-SQL, you can run a query against the
sys.resource_governor_workload_groupssystem catalog view, similar to the following,
which also displays the workload group settings:
select wg.name,
p.name as ‘pool’,
group_max_requests as max_req,
request_max_cpu_time_sec as max_cpu,
request_max_memory_grant_percent as max_mem,
Trang 3request_memory_grant_timeout_sec as grant_timeout,
max_dop
from sys.resource_governor_workload_groups wg
inner join
sys.resource_governor_resource_pools p
on wg.pool_id = p.pool_id
go
name pool max_req max_cpu max_mem grant_timeout max_dop
- -
-internal -internal 0 0 25 0 0
default default 0 0 25 0 0
ReportWG1 ReportPool 0 0 25 0 0
OLTPWG1 OLTPPool 0 0 25 0 0
Creating a Classification Function
After you define your resource pools and workload groups, you need to create a
classifica-tion funcclassifica-tion that contains the logic to evaluate the connecclassifica-tions and assign them to the
appropriate workload group The classification function applies to each new session
connection to SQL Server Each session stays in the assigned workload group until it
termi-nates, unless is it reassigned explicitly to a different group There can be only one
classifi-cation function active at any given time If no classifier function is defined or active, all
connections are assigned to the default workload group
The classification function is a scalar function created with the CREATE FUNCTION
state-ment, which must return a workgroup name as value of type SYSNAME(SYSNAMEis a data
type alias for nvarchar(128)) If the user-defined function returns NULL,’default’, or the
name of nonexistent group, the session is assigned to the default workload group The
session is also assigned to the default context if the function fails for any reason
The logic of the classification function is typically based on connection properties and
often determines the workload_groupthe connection should be assigned to based on
values returned by system functions such as SUSER_NAME(),SUSER_SNAME(),
IS_SRVROLEMEMBER(),IS_MEMBER(),HOST_NAME(), or APP_NAME().In addition to these
func-tions, you can use other available property functions when making classification
deci-sions The LOGINPROPERTY()function now includes two properties (DefaultDatabaseand
DefaultLanguage) that can be used in classification functions In addition, the
CONNECTIONPROPERTY()function provides access to the network transport and protocol
being used for the connection, as well as details of the authentication scheme, the local IP
address and TCP port, and the client’s IP address For example, you could assign a
connec-tion to a workload group based on which subnet a connecconnec-tion is coming in from
Trang 4TIP
If you decide to use eitherHOST_NAME()orAPP_NAME()in your classifier function, be
aware that it’s possible for the values returned by these functions to be altered by users
In general, however, theAPP_NAME()function tends to work very well for classifying
connections
TIP
A client session may time out if the classification function does not complete within
the specified time-out for the login Login time-out is a client property, and as such, the
server is unaware of a time-out A long-running classifier function can leave the server
with orphaned connections for long periods It is important that you create efficient
classifier functions that finish execution before a connection time-out
If you are using the Resource Governor, it is recommended that you enable the
dedi-cated administrator connection (DAC) on the server The DAC is not subject to
Resource Governor classification and can be used to monitor and troubleshoot a
classification function
For simplicity, the example presented in this chapter uses the SUSER_NAME()function
Listing 40.1 first creates a couple of SQL Server logins (report_userandoltp_user),
which will be used within the classification function to identify which workload group
session connections should be assigned to After adding the logins as users in the
AdventureWorks2008R2database, it then creates the classification function in the master
database
LISTING 40.1 Classification Function Example
use master;
create login report_user with password=’Rep0rter1’
create login oltp_user with password=’01tPus3r1’
go
use AdventureWorks2008R2;
create user report_user
create user oltp_user
EXEC sp_addrolemember N’db_datawriter’, N’report_user’
EXEC sp_addrolemember N’db_datareader’, N’report_user’
EXEC sp_addrolemember N’db_datawriter’, N’oltp_user’
EXEC sp_addrolemember N’db_datareader’, N’oltp_user’
go
Trang 5use master
go
CREATE FUNCTION dbo.WorkgroupClassifier ()
RETURNS SYSNAME WITH SCHEMABINDING
AS
BEGIN
DECLARE @WorkloadGroup SYSNAME = N’Unidentified’;
SET @WorkloadGroup = CASE suser_name()
WHEN N’report_user’ THEN
N’ReportWG1’
WHEN N’oltp_user’ THEN
N’OLTPWG1’
ELSE N’Unidentified’
END;
RETURN @WorkloadGroup;
END;
Go
GRANT EXECUTE on dbo.WorkgroupClassifier to public
go
Before you put the classification function into use, it’s a good idea to test it A poorly
written classification function could cause your system to become unresponsive For
example, you can test the WorkgroupClassifier()function in SSMS by executing the
following commands under different login IDs:
Executed logged in as report_user
select dbo.WorkgroupClassifier()
go
-ReportWG1
Executed logged in as report_user
select dbo.WorkgroupClassifier()
go
-OLTPWG1
Executed Logged in as another user
select dbo.WorkgroupClassifier()
go
-Unidentified
After you verify the classification function works as expected, you can then configure it as
the classification function using the ALTER RESOURCE GOVERNORcommand:
Trang 6ALTER RESOURCE GOVERNOR
WITH (CLASSIFIER_FUNCTION = dbo.WorkgroupClassifier);
ALTER RESOURCE GOVERNOR RECONFIGURE;
After you create the function and apply the configuration changes, the Resource Governor
classifier will use the workload group name returned by the function to send new requests
to the appropriate workload group
NOTE
You can also set the classification function for Resource Governor on the Resource
Governor Properties page, as shown in Figure 40.4 Click the Classifier Function Name
drop-down list and choose from the list of available functions presented Click OK to
save the changes and reconfigure Resource Governor
You can verify which classification function Resource Governor is currently using by
running the following query against the sys.resource_governor_configurationsystem
catalog view:
select object_name(classifier_function_id) AS ‘Classifier UDF name’,
is_enabled
from sys.resource_governor_configuration
go
Classifier UDF name is_enabled
-WorkgroupClassifier 1
At this point, your Resource Governor configuration is complete You then should
monitor the system to make sure it’s working as it should
TIP
To help make setting up and configuring Resource Governor easy and make sure you
get all the pieces together in the right sequence, you can configure Resource Governor
by using a template provided in SQL Server Management Studio From the View menu
in SSMS, select Template Explorer to display the Template Explorer In the Template
Explorer, expand Resource Governor and then double-click Configure Resource
Governor Provide the connection information, and the template Configure Resource
Governor.sqlopens in a query editor window This template contains template code
to create and configure a resource pool, workload group, and classifier function
Monitoring Resource Usage
SQL Server provides three dynamic management views you can use to view and monitor
your Resource Governor configuration:
sys.dm_resource_governor_workload_groups—Returns workload group
statistics along with the current in-memory configuration of the workload groups
Trang 7sys.dm_resource_governor_resource_pools—Returns information about
current state of your resource pools and resource pool statistics
sys.dm_resource_governor_configuration—Returns the in-memory
configu-ration state of the Resource Governor Output is the same as the
sys.resource_governor_configurationsystem catalog view
For example, the following query against the sys.dm_resource_governor_resource_pools
DMV returns the configuration settings for each of the pools along with the actual
memory allocated:
select name,
min_cpu_percent as MinCPU,
max_cpu_percent as MaxCPU,
min_memory_percent as ‘MinMEM%’ ,
max_memory_percent as ‘MaxMEM%’,
max_memory_kb as ‘MaxMemKB’,
used_memory_kb as ‘UsedMemKB’,
target_memory_kb as ‘TgtMemKB’
from sys.dm_resource_governor_resource_pools
GO
name MinCPU MaxCPU MinMEM% MaxMEM% MaxMemKB UsedMemKB TgtMemKB
- - - - -
-internal 0 100 0 100 1556232 8296 1556232
default 0 100 0 100 389064 8336 389064
ReportPool 0 20 0 30 389064 280 389064
OLTPPool 80 100 75 100 1556232 40 1556232
The following example displays statistics on the requests received within the defined
workgroups:
select
cast(g.name as nvarchar(10)) as wg_name,
cast(p.name as nvarchar(10)) as pool_name,
total_request_count as totreqcnt,
active_request_count as actreqcnt,
g.total_cpu_usage_ms as tot_cpu_use,
total_cpu_limit_violation_count as tot_clvc,
g.request_max_cpu_time_sec as req_mcts,
g.total_reduced_memgrant_count as tot_rmc
from sys.dm_resource_governor_workload_groups g
inner join
sys.dm_resource_governor_resource_pools p
on p.pool_id = g.pool_id
go
Trang 8wg_name pool_name totreqcnt actreqcnt tot_cpu_use tot_clvc req_mcts tot_rmc
- - -
-internal -internal 0 0 37314 0 0 0
default default 784 2 97938 0 0 0
ReportWG1 ReportPool 170 1 476016 0 0 0
OLTPWG1 OLTPPool 161 0 1834 0 0 0
Six other DMVs in SQL Server 2008 contain information related to Resource Governor:
sys.dm_exec_query_memory_grants—Returns information about the queries
that have acquired a memory grant or that still require a memory grant to execute
Resource Governor–related columns in this table are the group_id,pool_id,
is_small, and ideal_memory_kbcolumns
sys.dm_exec_query_resource_semaphores—Returns information about the
currentquery_resourcesemaphore status, providing general query-execution
memory status information The pool_idcolumn provides a link to Resource
Governor information
sys.dm_exec_session—Returns one row per session on SQL Server The group_id
column relates the information to Resource Governor workload groups
sys.dm_exec_requests—Returns information about each request currently
executing within SQL Server The group_idcolumn relates the information to
Resource Governor workload groups
sys.dm_exec_cached_plans—Returns a row for each query plan cached by SQL
Server in the plan cache The pool_idcolumn relates the information to Resource
Governor resource pools
sys.dm_os_memory_brokers—Returns information about internal allocations
within SQL Server that use the Memory Manager This information includes the
fol-lowing columns for the Resource Governor: pool_id,allocations_db_per_sec,
predicted_allocations_kb, and overall_limit_kb
The following query joins between sys.dm_exec_sessionand
sys.dm_resource_governor_workload_groupsto display which sessions are in which
workload group:
SELECT
CAST(g.name as nvarchar(10)) as poolname,
s.session_id as ‘session’,
s.login_time,
CAST(s.host_name as nvarchar(15)) as host_name,
CAST(s.program_name AS nvarchar(20)) as program_name
FROM sys.dm_exec_sessions s
INNER JOIN sys.dm_resource_governor_workload_groups g
ON g.group_id = s.group_id
where g.name in (‘default’, ‘ReportWG1’, ‘OLTPWG1’)
Trang 9go
poolname session login_time host_name program_name
- - - -
-default 51 2010-05-02 14:31:18.530 LATITUDED830-W7 Microsoft SQL Server
default 52 2010-05-02 14:31:21.990 LATITUDED830-W7 SQLAgent - Generic R
default 53 2010-05-02 14:31:23.533 LATITUDED830-W7 SQLAgent - TSQL JobS
default 55 2010-05-02 14:47:27.250 LATITUDED830-W7 Microsoft SQL Server
ReportWG1 60 2010-05-02 19:06:21.100 LATITUDED830-W7 Microsoft SQL Server
OLTPWG1 54 2010-05-02 21:03:03.020 LATITUDED830-W7 Microsoft SQL Server
You can also monitor CPU and memory resources allocated by the Resource Governor
through the Windows Performance Monitor via a couple of new performance counters:
SQLServer: Resource Pool Stats
SQLServer: Workload Stats
An instance of the SQLServer: Resource Pool Statscounter is available for each of the
configured resource pools Likewise, an instance of the SQLServer: Workload Stats
counter is available for each of the configured workload groups (see Figure 40.5) These
performance counters return the same information as that returned by the
sys.dm_resource_governor_workload_groupsand
sys.dm_resource_governor_resource_poolsDMVs but enable you to monitor these
statistics over time
FIGURE 40.5 Monitoring resource pool and workload group statistics in Performance Monitor
Trang 10Modifying Your Resource Governor Configuration
You can modify settings for resource pools or workload groups in SQL Server Management
Studio via the Resource Governor Properties page, as shown previously in Figure 40.4 You
simply make the changes desired (for example, a Resource Pool Maximum CPU% or
Workload Group Importance) and click OK to save the changes
Alternatively, you can modify the resource pool using the ALTER RESOURCE POOL
command With this command, you can modify the minimum and maximum CPU and
memory percentages for a resource pool The syntax is as follows:
ALTER RESOURCE POOL { pool_name | “default” }
[WITH
( [ MIN_CPU_PERCENT = value ]
[ [ , ] MAX_CPU_PERCENT = value ]
[ [ , ] MIN_MEMORY_PERCENT = value ]
[ [ , ] MAX_MEMORY_PERCENT = value ] )
]
You can modify workload group settings using the ALTER WORKLOAD GROUPcommand You
can change the workload group settings as well as move the workload group to another
resource pool The syntax is as follows:
ALTER WORKLOAD GROUP { group_name | ”default” }
[ WITH
([ IMPORTANCE = { LOW | MEDIUM | HIGH } ]
[ [ , ] REQUEST_MAX_MEMORY_GRANT_PERCENT = value ]
[ [ , ] REQUEST_MAX_CPU_TIME_SEC = value ]
[ [ , ] REQUEST_MEMORY_GRANT_TIMEOUT_SEC = value ]
[ [ , ] MAX_DOP = value ]
[ [ , ] GROUP_MAX_REQUESTS = value ] )
]
[ USING { pool_name | ”default” } ]
NOTE
After executing yourALTER WORKLOAD GROUPorALTER RESOURCE POOLcommands,
you need to run theALTER RESOURCE GOVERNOR RECONFIGUREcommand to apply
the changes
The following example moves the ReportWG1workload group from the ReportPool
resource pool to the default resource pool:
ALTER WORKLOAD GROUP ReportWG1
USING [default];
GO