Cost Threshold and Max Degree of Parallelism

SQL Server has many options for configuring a database system. Most do not become apparent until some part of the system does not function “properly”. Parallelism is one of these settings. You will see Waits for Parallelism and blocking if this setting is not effective. The SQL Server instance defaults for Max Degree of Parallelism is 0 and Cost Threshold is 5. What does this mean?

clip_image001

Advanced SQL Server Instance Properties

Maybe it is best to explain with an example. The machine used to write this blog has 4 CPUs in one Core. So, it is possible for a query or process in SQL Server to run on all 4 CPUs (Threads) at the same time. Because the default for Max Degree of Parallelism is 0, all CPUs are used when a process or query runs in parallel. I can change this to 2 and all processes or queries can only use a maximum of 2 CPUs unless a MAXDOP is specified in the query or process to use more or less.

Let’s look at a query that runs in parallel.

USE AdventureWorks2014
GO
SET STATISTICS XML ON
GO

SELECT sod.SalesOrderID, sod.SalesOrderDetailID, sod.LineTotal
  , p.Class, p.ListPrice, p.Name
FROM Sales.SalesOrderDetail sod
INNER JOIN Production.Product p ON sod.ProductID = p.ProductID
ORDER BY Style
GO

The SET STATISTICS XML ON will produce a second result from running the query that is the XML of the execution plan.

clip_image002

Adventure Works query running in parallel

By clicking on the XML output, you will get the Execution Plan for the query.

clip_image003

Parallel Query

The operators (Hash Match is one) with the yellow double arrows indicate a parallel process during the execution of the plan. If we add OPTION (MAXDOP 1) to the query, it will not run in parallel and we will be able to see the Cost of the query when not running in parallel. This option forces the query to run as with one CPU (Thread) without any parallelism. We could also change the Max Degree of Parallelism on the instance but this would be system wide.

 

SELECT sod.SalesOrderID, sod.SalesOrderDetailID, sod.LineTotal
  , p.Class, p.ListPrice, p.Name
FROM Sales.SalesOrderDetail sod
INNER JOIN Production.Product p ON sod.ProductID = p.ProductID
ORDER BY Style
OPTION (MAXDOP 1)
GO

The following is the details of the query plan and cost. The cost of this query will be approximately 6.8.

clip_image004

No Parallelism with MADOP 1

clip_image005

Cost of Query

Now, if we change the Cost Threshold of Parallelism on the instance from 5 to 10, the cost of the query (6.8) is no longer above the threshold (10) and the query will not run in parallel even without the MAXDOP 1 option.

clip_image006

Change Cost Threshold and Max Degree of Parallelism

We have changed the Cost Threshold of Parallelism to 10 for the instance as well as the Max Degree of Parallelism to 2. The 2 will be an instance wide setting to not use more than 2 CPUs for a query or process unless the OPTION MAXDOP is used in the query or process. We will see this in the properties of the parallel operator in the screen below.

SELECT sod.SalesOrderID, sod.SalesOrderDetailID, sod.LineTotal
  , p.Class, p.ListPrice, p.Name
FROM Sales.SalesOrderDetail sod
INNER JOIN Production.Product p ON sod.ProductID = p.ProductID
ORDER BY Style
GO

clip_image007

No Parallelism with New Cost Threshold

If I change the Cost Threshold back to 5, I will get parallelism with 2 CPUs.

clip_image009

MAXDOP2 Shown In Hash Match Operator Properties

You can now see in the properties of the Hash Match the Actual Number of Rows has 3 threads. The 0 zero thread controls the other 2 threads – 1 and 2. 1 and 2 are the 2 CPUs used to run the query in parallel based on the instance setting of 2 for Max Degree of Parallelism.

So, in conclusion, the MAXDOP feature is for the number of CPUs available for queries or processes (like Backups) to run in parallelism. The Cost Threshold is the value of the Cost of a query that triggers the Query Processer to see if parallelism will help a query or process.