未加星标

SQL Server 2016 DBCC CHECKDB with MAXDOP

字体大小 | |
[数据库(mssql) 所属分类 数据库(mssql) | 发布者 店小二03 | 时间 2016 | 作者 红领巾 ] 0人收藏点击收藏

By:Ahmad Yaseen || Related Tips:More > Database Consistency Checks DBCCs

Problem

How can we control the number of processors assigned to the SQL Server DBCC CHECKDB command in order not to consume all of the servers resources when CHECKDB is running?

Solution

Degree of parallelism is the number of processors that are assigned to run a single SQL statement. SQL Server manages to detect the best degree of parallelism for each query to generate the best execution plan. Maximum Degree of Parallelism (MAXDOP) is an instance level configuration that is used to restrict the number of processors used for parallel execution plans for performance purposes.

SQL Server 2016 introduces a new option to limit the number of processors assigned for the DBCC CHECKDB statement, overriding the instance level MAXDOP configuration. For this demo I am using a machine with 4 processors as we can see in the Task Manager window as shown below:


SQL Server 2016 DBCC CHECKDB with MAXDOP

The instance level MAXDOP option is configured with the default value 0, which means that SQL Server parallel plans will use all processors available on that server, which is 4 in my case. This configuration can be checked and reconfigured from the Advanced tab of the Server Properties window using SQL Server Management Studio as follows:


SQL Server 2016 DBCC CHECKDB with MAXDOP

The MAXDOP option can be also checked using the sp_configure command as shown below:

sp_configure 'show advanced options', 1;
GO
RECONFIGURE
GO
sp_configure 'max degree of parallelism';

The query result will be like this:


SQL Server 2016 DBCC CHECKDB with MAXDOP

If we execute the below DBCC CHECKDB statement and take into consideration that the DBCC CHECKDB command is a heavy command that will use a parallel execution plan to run (if applicable) using the instance level MAXDOP configuration value:

SET STATISTICS TIME ON
DBCC CHECKDB (AdventureWorks2012)
SET STATISTICS TIME OFF

We can grab the current session ID from the SSMS status bar as shown below:


SQL Server 2016 DBCC CHECKDB with MAXDOP

Then querying the sys.dm_os_tasks system object to check the number of schedulers used by that session during execution:

select Session_id , scheduler_id from sys.dm_os_tasks
where session_id = 54

The result will show us that the DBCC CHECKDB command is using all available processors which is 4 in our situation (scheduler_id 0, 1, 2, 3) :


SQL Server 2016 DBCC CHECKDB with MAXDOP

This took 3800ms to complete its execution:


SQL Server 2016 DBCC CHECKDB with MAXDOP

As mentioned previously, the DBCC CHECKDB can override the instance level MAXDOP option to be restricted to a specific number of processors. The below DBCC CHECKDB command will use only 3 processors to run:

SET STATISTICS TIME ON
DBCC CHECKDB (AdventureWorks2012) WITH MAXDOP = 3;
SET STATISTICS TIME OFF

Checking the number of schedulers again for that query (scheduler_id 0, 1, 3):


SQL Server 2016 DBCC CHECKDB with MAXDOP

You will find that the DBCC CHECKED command is limited with only 3 processors although the SQL Server is configured to use all available 4 processors. This query took 4046ms to complete successfully:


SQL Server 2016 DBCC CHECKDB with MAXDOP

The below query also will restrict the DBCC CHECKDB command to use only 2 processors:

SET STATISTICS TIME ON
DBCC CHECKDB (AdventureWorks2012) WITH MAXDOP = 2;
SET STATISTICS TIME OFF

Which is clear when we check the number of schedulers used in the current session (scheduler_id 0, 1):


SQL Server 2016 DBCC CHECKDB with MAXDOP

And the command took 4293ms to complete successfully.


SQL Server 2016 DBCC CHECKDB with MAXDOP

Lets try it in another way. If the instance level MAXDOP option is configured with a value 1, which means that each query can use only one processor to run. This can be configured using sp_configure as below:

EXEC sys.sp_configure N'max degree of parallelism', N'1'
GO
RECONFIGURE WITH OVERRIDE
GO

The below command will override that value to use 3 processors to run:

DBCC CHECKDB (AdventureWorks2012) WITH MAXDOP = 3;

This can be clearly found by checking the number of schedulers used in that session:


SQL Server 2016 DBCC CHECKDB with MAXDOP

As you can see from the previous results, the DBCC CHECKDB command will run faster if it takes the benefits from more parallel threads. Each time we decrease the number of processors assigned to the command it will take longer to complete, but consume less CPU. Using this option allows us to prevent this command from consuming all server resources and degrading the servers overall performance during its execution. So that is a decision you will need to make, to either run faster or limit the CPU resources used.

Next Steps DBCC CHECKDB WITH MAXDOP option also works with SQL Server 2014 SP2 version. Read more about SQL Server DBCC CHECKDB Overview. Check out What MAXDOP setting should be used for SQL Server.

Last Update: 10/11/2016


SQL Server 2016 DBCC CHECKDB with MAXDOP
SQL Server 2016 DBCC CHECKDB with MAXDOP
About the author
SQL Server 2016 DBCC CHECKDB with MAXDOP
Ahmad Yaseen is a SQL Server DBA with a bachelors degree in computer engineering as well as .NET development experience. View all my tips

Related Resources

More SQL Server DBA Tips...

本文数据库(mssql)相关术语:熊片数据库 mssql数据库 oracle数据库 pubmed数据库 access数据库 万方数据库

主题: SQLSQL ServerTICPU
分页:12
转载请注明
本文标题:SQL Server 2016 DBCC CHECKDB with MAXDOP
本站链接:http://www.codesec.net/view/482100.html
分享请点击:


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 数据库(mssql) | 评论(0) | 阅读(29)