Microsoft SQL Server 2008 R2 Unleashed- P157 pdf

10 310 0
Microsoft SQL Server 2008 R2 Unleashed- P157 pdf

Đang tải... (xem toàn văn)

Thông tin tài liệu

ptg 1504 CHAPTER 40 Managing Workloads with the Resource Governor 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. MEDIUM is 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 HIGH three times more often than workgroups with MEDIUM importance, and nine times more often workgroups with LOW importance. 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 0 through 100. 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 0 prevents queries with SORT and HASH JOIN operations in user-defined workload groups from running. It is also not recommended that this value be set greater than 70 because 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 0 through 64. The default setting is 0, which means that processes use the global setting. Be aware that MAX_DOP specifies an upper limit only. The actual degree ptg 1505 Configuring Resource Governor 40 of 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_DOP setting is handled, consider the following: . MAX_DOP as a query hint is considered only if it does not exceed the workload group MAX_DOP setting. . MAX_DOP as a query hint always overrides the max degree of parallelism server configuration option. . Workload group MAX_DOP always 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 Governor node, expand the Resource Pools folder, expand the ReportPool node, and finally, expand the Workload Groups folder. You should then see a folder named ReportWG1. Creating Workload Groups in T-SQL Now that you’ve set up the ReportWG1 workload 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_groups system 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, ptg 1506 CHAPTER 40 Managing Workloads with the Resource Governor request_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 function that contains the logic to evaluate the connections 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 (SYSNAME is 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_group the 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 (DefaultDatabase and 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 connection is coming in from. ptg 1507 Configuring Resource Governor 40 TIP If you decide to use either HOST_NAME() or APP_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, the APP_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_user and oltp_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 AdventureWorks2008R2 database, 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 ptg 1508 CHAPTER 40 Managing Workloads with the Resource Governor use 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 GOVERNOR command: ptg 1509 Monitoring Resource Usage 40 ALTER 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_configuration system 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.sql opens 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. ptg 1510 CHAPTER 40 Managing Workloads with the Resource Governor . sys.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_configuration system 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 ptg 1511 Monitoring Resource Usage 40 wg_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_kb columns. . sys.dm_exec_query_resource_semaphores—Returns information about the current query_resource semaphore status, providing general query-execution memory status information. The pool_id column 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_id column 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_id column 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_session and sys.dm_resource_governor_workload_groups to 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’) ptg 1512 CHAPTER 40 Managing Workloads with the Resource Governor go 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 Stats counter 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_groups and sys.dm_resource_governor_resource_pools DMVs but enable you to monitor these statistics over time. FIGURE 40.5 Monitoring resource pool and workload group statistics in Performance Monitor. ptg 1513 Modifying Your Resource Governor Configuration 40 Modifying 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 GROUP command. 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 your ALTER WORKLOAD GROUP or ALTER RESOURCE POOL commands, you need to run the ALTER RESOURCE GOVERNOR RECONFIGURE command to apply the changes. The following example moves the ReportWG1 workload group from the ReportPool resource pool to the default resource pool: ALTER WORKLOAD GROUP ReportWG1 USING [default]; GO . 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. 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. Performance Monitor via a couple of new performance counters: . SQLServer: Resource Pool Stats . SQLServer: Workload Stats An instance of the SQLServer: Resource Pool Stats counter is available for each

Ngày đăng: 05/07/2014, 02:20

Từ khóa liên quan

Mục lục

  • Table of Contents

  • Introduction

  • Part I: Welcome to Microsoft SQL Server

    • 1 SQL Server 2008 Overview

      • SQL Server Components and Features

      • SQL Server 2008 R2 Editions

      • SQL Server Licensing Models

      • Summary

      • 2 What’s New in SQL Server 2008

        • New SQL Server 2008 Features

        • SQL Server 2008 Enhancements

        • Summary

        • 3 Examples of SQL Server Implementations

          • Application Terms

          • OLTP Application Examples

          • DSS Application Examples

          • Summary

          • Part II: SQL Server Tools and Utilities

            • 4 SQL Server Management Studio

              • What’s New in SSMS

              • The Integrated Environment

              • Administration Tools

              • Development Tools

              • Summary

              • 5 SQL Server Command-Line Utilities

                • What’s New in SQL Server Command-Line Utilities

                • The sqlcmd Command-Line Utility

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan