【Weekly SQLpassion Newsletter】Setting a Processor Affinity in SQL Server – the (unwanted) Side-Effects

Today I want to talk about a very important topic in SQL Server – setting a Processor Affinity(n. 密切关系;吸引力;姻亲关系;类同) – and the unwanted side effects that you introduce with this approach. First of all I want to talk a little bit about what a Processor Affinity is, and how it relates to SQL Server.

What is a Processor Affinity

If you run SQL Server with a default configuration, SQL Server will schedule your queries on all available CPU cores. For each CPU core you get a so-called Scheduler, which implements a Query State Machine with the states RUNNINGSUSPENDEDRUNNABLE.

This Query State Machine is very important, because it is also tracked by the Wait Statistics, which give you a great overview why SQL Server is slow. The most important point is now that a Scheduler is NOT bound to a specific CPU core. Each Scheduler can run on every CPU core. The following picture illustrates this very important concept.

Therefore, when an incoming query is assigned to a Scheduler, the Scheduler can be run on each CPU core. There is no 1:1 mapping between a Scheduler and a CPU core. The following picture shows you the CPU utilization(n. 利用,使用,效用) during the execution of a Single-Threaded query which is mostly CPU bound.

 

As you can see all 4 cores have some CPU utilization, but there is no CPU core that runs continuously at 100%. With the following query you can also check on which CPU cores a specific query (based on the SPID) can be executed.

SELECT r.session_id, t.affinity FROM sys.dm_exec_requests r
JOIN sys.dm_os_workers w ON w.task_address = r.task_address
JOIN sys.dm_os_threads t ON t.worker_address = w.worker_address
WHERE r.session_id = 53

With the default configuration of SQL Server, and a 4 core system, you will get back the value 15 – which is in binary 1111. The binary value 1111 just means that the Scheduler of this query can run on each core of our 4 core system. With this example you can see quite easily how a Scheduler moves around between the individual CPU cores.

Setting a Processor Affinity

Imagine now you want to restrict(vt. 限制,限定;约束,束缚) which specific CPU cores your SQL Server instance should use. In that case you can reconfigure your SQL Server Instance and tell SQL Server to use only a subset of CPU cores.

Setting a Processor Affinity in the correct way

We know now that taking some CPU cores offline for SQL Server has bad side effects. But there is some help available for you – Trace Flag 8002. When you enable that Trace Flag on your SQL Server instance (and you restart SQL Server), SQL Server will again take your CPU cores offline, but the remaining cores have NO affinity with their Schedulers.

 

Therefore, when you now run again a query, the scheduler can itself decide on which core to be executed. You can again verify this behaviour through Task Manager.

This behaviour is now much better, because the Scheduler can again decide on which remaining cores to execute your query. If you run the diagnostic query from previous you will get now back the value 12 – binary 1100. The Scheduler can use core 2 and 3 for query execution.

 

Summary

By default SQL Server doesn’t have a Processor Affinity, which is a good thing. But as soon as you restrict SQL Server to use a subset of CPU cores, you will also have internally(adv. 内地;内心地;) a Processor Affinity set. And therefore it’s very important that you know these unwanted side effects when you working on your SQL Server instance configuration, and how to avoid it with Trace Flag 8002.

Thanks for your time,

-Klaus

 

posted @ 2018-02-02 11:07  FH1004322  阅读(164)  评论(0)    收藏  举报