Handling workloads on SQL Server 2008 with Resource Governor

 

Handling workloads on SQL Server 2008 with Resource Governor

Written By: Arshad Ali -- 4/1/2009 -- 2 comments

     Stay informed - get the MSSQLTips.com newsletter and win - click here    

Problem
Handling workloads has been quite difficult until SQL Server 2005. For example consider a scenario where one SQL instance is serving two applications i.e. an OLTP application and a reporting/data warehousing application. Since reporting applications are normally resource intensive, it may consume all the SQL Server available resources and may hamper the OLTP application which ideally should have more preference over the reporting application.

To handle this scenario, in earlier version of SQL Server there was one option to create multiple instances for these application (segregating by running one application on each instance) and setting CPU affinity mask for these instances appropriately. But the problems with this approach are, first it works for CPU only and second the dedicated CPUs cannot be shared by other SQL Server instances. For example, if there are two SQL Server instances and instance one has been assigned CPU 1 and 2 and instance two has been assigned CPU 3 and 4 on a four processor machine, even if instance one is idle and instance two is in need of additional resources, it can only use CPU 3 and 4.  So what does SQL 2008 offer to solve this issue?

Solution
SQL Server 2005 resource allocation policies treat all workloads equally, and allocate shared resources as they are requested. It sometimes causes a disproportionate distribution of resources, which in turn results in uneven performance or unexpected slowdowns whereas the new Resource Governor of SQL Server 2008 allows organizations to define resource limits and priorities for different workloads, which enables concurrent workloads to provide consistent performance to the end users.

Resource Governor is a new technology in SQL Server 2008 that enables you to manage SQL Server workloads and resources by specifying limits on resource consumption by incoming requests. In an environment where multiple distinct workloads are present on the same server, Resource Governor enables us to differentiate these workloads and allocate shared resources as they are requested, based on the limits that you specify. These resources are CPU and memory. In other words, Resource Governor enables you to assign a relative importance to workloads. In other words, one workload can be allowed to proceed faster than another or is guaranteed to complete if there is resource contention. It allows a DBA or ITPros to define resource limits and priorities for different workloads.

Resource Governor Components

There are three new components of Resource Governor which are important to understand : resource pools, workload groups and classification (or classifier user-defined functions).

  • Pool: A resource pool, or pool, is a collection of system resources such as memory or CPU; it represents a portion of the physical resources of the server. Depending on its settings, a pool may have a fixed size (its minimum and maximum resource usage settings are equal to each other) or have a part which is shared between multiple pools (its minimum is less than its effective maximum). “Shared” in this case simply means that resources go to the pool that requests the resources first. In the default configuration all resources are shared, thus maintaining backward compatibility with SQL Server 2005 policies. Two resource pools (internal and default) are created when SQL Server 2008 is installed. Resource Governor also supports 18 user-defined resource pools. You specify MIN and MAX values for resources (CPU or Memory) which represents the minimum guaranteed resource availability of the pool and the maximum size of the pool, respectively. The sum of MIN values across all pools cannot exceed 100 percent of the server resources. MAX value can be set anywhere in the range between MIN and 100 percent inclusive. The internal pool represents the resources consumed by the SQL Server itself. This pool always contains only the internal group, and the pool is not alterable in any way. Resource consumption by the internal pool is not restricted. Any workloads in the pool are considered critical for server function, and Resource Governor allows the internal pool to pressure other pools even if it means the violation of limits set for the other pools. The default pool is the first predefined user pool. Prior to any configuration the default pool only contains the default group. The default pool cannot be created or dropped but it can be altered. The default pool can contain user-defined groups in addition to the default group.

  • Group: A workload group, or group, is a user-specified category of requests that are similar according to the classification rules that are applied to each session request. A group defines the policies for its members. A resource pool is assigned to a Workload Group, which is in turn is assigned to the Resource Governor. Two workload groups (internal and default) are created and mapped to their corresponding resource pools when SQL Server 2008 is installed, apart from that the Resource Governor also supports user-defined workload groups. The internal workload group is populated with requests that are for internal SQL Server use only. You cannot change the criteria used for routing these requests and you cannot classify requests into the internal workload group whereas requests are mapped to default workload group, if there is a classification failure, an attempt to map to a non-existent workload group and there is no criteria to classify. If the Resource Governor is disabled, all new connections are automatically classified into the default group and System-initiated requests are classified into the internal workload group.

  • Classification: Classification is a set of user-written rules that enable Resource Governor to classify session requests into the workload groups as described previously; for example classifying on the basis of user, application etc. It is implemented through a scalar Transact-SQL user-defined function (UDF) which is designated as a “classifier UDF” for the Resource Governor in the master database. Only one user-defined function can be designated as a classifier at a time.

Putting it all together

The incoming connection request for a session is classified by a classifier UDF and is routed to an appropriate workload group. This workload group in turn uses the resource pool associated with it and finally the resource pool provides and limits on the resources required by the session. Let's see this with an example, I will consider the same problem discussed in the problem section where we have one SQL Server instance serving an OLTP application and a reporting application, though it can be used in variety of different circumstances where you have to manage workloads:

  • First I will create two resource pools to be used by OLTP and Reporting application, then I will create two workload groups which will categorize the request coming from these applications.

Working with Resource Governor in SQL Server 2008 - Part 1

--Resource pool to be used by OLTP Application
CREATE RESOURCE POOL OLTPPool
WITH
(
 MIN_CPU_PERCENT=50, MAX_CPU_PERCENT=100,
 MIN_MEMORY_PERCENT=50, MAX_MEMORY_PERCENT=100
)
GO
--Resource pool to be used by Report Application
CREATE RESOURCE POOL ReportPool
WITH
(
 MIN_CPU_PERCENT=50, MAX_CPU_PERCENT=100,
 MIN_MEMORY_PERCENT=50, MAX_MEMORY_PERCENT=100
)
GO
--Workload Group to be used by OLTP Application
CREATE WORKLOAD GROUP OLTPGroup
    USING OLTPPool ;
GO
--Workload Group to be used by Report Application
CREATE WORKLOAD GROUP ReportGroup
    USING ReportPool ;
GO    
  • Next I will create the classifier UDF to route incoming request to different workload groups and finally I will enable Resource Governor with ALTER RESOURCE GOVERNOR RECONFIGURE statement. Assumption here is, the OLTP application uses "OLTPUser" login whereas reporting application uses "ReportUser" login.

Working with Resource Governor in SQL Server 2008 - Part 2

USE master;
GO
CREATE FUNCTION dbo.ResourceClassifier() 
RETURNS SYSNAME 
WITH SCHEMABINDING
AS
BEGIN
 --Declare the variable to hold the value returned in sysname.
 DECLARE @WorkloadGroup AS SYSNAME
 --If the user login is 'OLTPUser', map the connection to the 
 --OLTPGroup workload group. 
 IF (SUSER_NAME() = 'OLTPUser')
  SET @WorkloadGroup = 'OLTPGroup'
 --If the user login is 'ReportUser', map the connection to 
 --the ReportGroup workload group. 
 ELSE IF (SUSER_NAME() = 'ReportUser')
  SET @WorkloadGroup = 'ReportGroup'
 ELSE 
  SET @WorkloadGroup = 'default'
 RETURN @WorkloadGroup
END
GO
--Register the classifier user-defined function and update the 
--the in-memory configuration.
ALTER RESOURCE GOVERNOR 
WITH (CLASSIFIER_FUNCTION=dbo.ResourceClassifier);
GO
--Enabling Resource Governor(By default when you install 
--SQL Server, Resource Governor is disabled)
--It loads the stored configuration metadata into memory 
ALTER RESOURCE GOVERNOR RECONFIGURE
GO
--Disabling Resource Governor
ALTER RESOURCE GOVERNOR DISABLE
GO
--It resets statistics on all workload groups and resource pools. 
ALTER RESOURCE GOVERNOR RESET STATISTICS
GO
  • Resource Governor can also be managed using SQL Server Management Studio (SSMS), you can CREATE, ALTER, DROP resource pools, workload groups, change classifier UDF and ENABLE/DISABLE Resource Governor as shown below.

Resource Governor’s Catalog Views and Dynamic Management Views

There are three new Catalog Views and three new Dynamic Management Views introduced for Resource Governor.

  • sys.resource_governor_configuration - used to display the Resource Governor configuration as stored in metadata.

  • sys.resource_governor_resource_pools - used to display resource pool configuration as stored in metadata.

  • sys.resource_governor_workload_groups - used to display workload group configuration as stored in metadata.

  • sys.dm_resource_governor_configuration - used to get the current in-memory configuration state of Resource Governor

  • sys.dm_resource_governor_resource_pools - used to get the current resource pool state, the current configuration of resource pools, and resource pool statistics.

  • sys.dm_resource_governor_workload_groups - used to get the workload group statistics and the current in-memory configuration of the workload group.

In addition to new views that are specific to Resource Governor, existing system views have been modified to include information about Resource Governor as well.

Note

  • Only one resource pool can be assigned to a workload, though a single resource pool can serve multiple workload groups. If there are multiple workload groups in a given resource pool, you can set relative importance of each workload group to either LOW, MEDIUM or HIGH.

  • The resource governor limits can easily be reconfigured in real time with minimal impact on the workloads that are executing; for that purpose you use ALTER RESOURCE GOVERNOR statement with the RECONFIGURE parameter.

  • To monitor utilization of Resource governor you can monitor different performance counters under SQLServer:Resource Pool Stats and SQLServer:Workload Group Stats performance counter categories.

Limitations

There are also some limitations to the Resource Governor. They are as follows:

  • Resource management is limited to the SQL Server Database Engine. Resource Governor cannot be used for Analysis Services, Integration Services, and Reporting Services.

  • Only a single instance can be managed through this. An organization may have more than a single instance, but must manage each separately.

  • Limited to only two resources i.e. CPU bandwidth and memory management.

  • You are allowed to create only 18 resource pools apart from Default and Internal pool. Creating more resource pool than this throws an error, “The resource pool cannot be created. The maximum number of resource pools cannot exceed current limit of 20 including predefined resource pools.” Though in one sense it is good to have fewer resource pools and assign multiple workloads to it if required.

  • Resource Governor is available only on the Enterprise, Developer, and Evaluation editions of SQL Server.

Next Steps

<think>好的,我现在要帮用户解决关于SQL Server参数使用方法和配置教程的问题。首先,我需要回忆一下用户提供的引用内容,看看有没有相关的信息可以利用。引用1提到使用Maven集成SQL Server驱动的过程和配置步骤,引用2是关于SQL Server的使用教程,包括安装配置、数据库操作、备份还原以及优化维护,引用3则包含安装时的踩坑经验和一些Python连接SQL Server的代码示例。 用户的问题集中在参数的使用和配置上,所以我需要从这些引用中提取相关信息,并结合我的知识来组织回答。首先,参数在SQL Server中通常指的是配置选项,比如服务器级参数、数据库参数、连接参数等。需要分门别类地讲解。 然后,我要考虑用户可能的实际需求。他们可能是开发人员或DBA,需要调整服务器配置、优化查询性能,或者在应用程序中正确配置连接参数。因此,我应该涵盖常见的参数类型,比如最大内存、并行度,数据库的恢复模式,以及连接字符串中的参数,比如超时设置和加密选项。 接下来,配置方法部分需要分步骤说明,比如如何通过SSMS图形界面修改服务器参数,或者使用T-SQL命令。同时,引用3中提到了Python连接时的参数设置,这部分可以作为连接参数配置的示例,比如host、user、password等,可能还需要提到其他常用参数如database名称、连接超时等。 注意事项和常见问题部分,应该包括参数修改后需要重启服务的情况,权限问题,以及参数设置不当可能导致的性能问题。引用1中提到对方团队不熟悉技术,可能用户也会遇到类似问题,所以需要强调参数配置的谨慎和测试的重要性。 最后,生成相关问题时要围绕参数配置、性能优化、连接问题、安全设置等方面提问,帮助用户进一步探索。同时,确保引用标记正确,比如引用1提到配置步骤,引用2涉及优化维护,引用3有连接示例,这些都应在回答中适当标注。 检查是否有遗漏,比如备份相关的参数可能在引用2的备份还原部分提到,但用户的问题更关注配置,所以可能需要补充备份压缩选项等参数。同时,确保所有步骤清晰,语言简洁,符合用户要求的格式,如行内公式用$...$,但这里可能用不上数学公式,但要注意其他格式要求,如代码块用反引号,独立公式用$$...$$,但本回答中可能不需要公式。 最后,确保回答结构符合用户给出的示例,先介绍参数类型,再配置方法,注意事项,示例代码,然后相关问题。引用标注在相关段落末尾,如配置步骤引用1,备份参数引用2,连接参数引用3。这样用户能清楚看到每个部分的参考来源。</think>### SQL Server参数配置与使用方法 #### 一、SQL Server参数类型 1. **服务器级参数** - 控制整个SQL Server实例的行为,例如: - `max server memory`:设置SQL Server可使用的最大内存[^2] - `max degree of parallelism`:控制查询并行度 - 查看方式: ```sql SELECT name, value_in_use FROM sys.configurations WHERE name LIKE '%max%' ``` 2. **数据库级参数** - 控制单个数据库行为,例如: - `RECOVERY`模式:`FULL/SIMPLE/BULK_LOGGED`[^2] - `AUTO_SHRINK`:是否自动收缩数据库文件 3. **连接参数** - 应用程序连接时使用的参数,例如: - `Encrypt`:是否启用SSL加密 - `Connection Timeout`:连接超时时间[^3] #### 二、核心参数配置方法 1. **通过SQL Server Management Studio (SSMS)** - 右键实例 → 属性 → 服务器属性页 → 内存/处理器等选项卡修改参数 - 数据库参数:右键数据库 → 属性 → 选项页 2. **通过T-SQL命令** ```sql -- 修改最大内存为8GB EXEC sp_configure 'max server memory', 8192 RECONFIGURE ``` 3. **连接字符串配置(Python示例)** 引用中的连接代码可扩展更多参数: ```python conn = pymssql.connect( host='localhost', port=1433, # 指定端口 database='mydb', # 指定数据库 login_timeout=30, # 登录超时 as_dict=True # 返回字典格式结果 ) ``` #### 三、关键注意事项 1. **参数修改风险** - 修改`max server memory`需保留足够内存给操作系统 - `max degree of parallelism`设置过高可能导致资源争用 2. **生效条件** - 部分参数需重启服务(如`cost threshold for parallelism`) - 连接参数修改需重新建立连接 3. **性能优化参数** - `optimize for ad hoc workloads`:优化临时查询 - `query wait`:设置查询等待资源超时时间[^2] #### 四、常见问题解决 1. **连接超时问题** - 检查防火墙设置 - 在连接字符串增加`timeout=60`参数 2. **内存不足报错** - 通过以下命令调整内存: ```sql EXEC sp_configure 'max server memory', 16384 -- 16GB RECONFIGURE ``` 3. **备份参数配置** - 使用`COMPRESSION`参数减少备份文件大小: ```sql BACKUP DATABASE [mydb] TO DISK='mydb.bak' WITH COMPRESSION ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值