简体   繁体   English

在 SQL Server 中使用 OPTION(MAXDOP 1) 的目的是什么?

[英]What is the purpose for using OPTION(MAXDOP 1) in SQL Server?

I have never clearly understood the usage of MAXDOP .我从来没有清楚地理解MAXDOP的用法。 I do know that it makes the query faster and that it is the last item that I can use for Query Optimization.我知道它使查询更快,并且它是我可以用于查询优化的最后一项。

However, my question is, when and where it is best suited to use in a query?但是,我的问题是,何时何地最适合在查询中使用?

As Kaboing mentioned, MAXDOP(n) actually controls the number of CPU cores that are being used in the query processor.正如 Kaboing 所提到的, MAXDOP(n)实际上控制着查询处理器中正在使用的 CPU 内核的数量。

On a completely idle system, SQL Server will attempt to pull the tables into memory as quickly as possible and join between them in memory.在完全空闲的系统上,SQL Server 将尝试尽快将表拉入内存并在内存中连接它们。 It could be that, in your case, it's best to do this with a single CPU.在您的情况下,可能最好使用单个 CPU 来执行此操作。 This might have the same effect as using OPTION (FORCE ORDER) which forces the query optimizer to use the order of joins that you have specified.这可能与使用OPTION (FORCE ORDER)效果相同,后者强制查询优化器使用您指定的连接顺序。 IN some cases, I have seen OPTION (FORCE PLAN) reduce a query from 26 seconds to 1 second of execution time.在某些情况下,我已经看到OPTION (FORCE PLAN)将查询的执行时间从 26 秒减少到 1 秒。

Books Online goes on to say that possible values for MAXDOP are: Books Online 继续说MAXDOP可能值是:

0 - Uses the actual number of available CPUs depending on the current system workload. 0 - 根据当前系统工作负载使用可用 CPU 的实际数量。 This is the default value and recommended setting.这是默认值和推荐设置。

1 - Suppresses parallel plan generation. 1 - 禁止并行计划生成。 The operation will be executed serially.该操作将串行执行。

2-64 - Limits the number of processors to the specified value. 2-64 - 将处理器数量限制为指定值。 Fewer processors may be used depending on the current workload.根据当前的工作负载,可以使用更少的处理器。 If a value larger than the number of available CPUs is specified, the actual number of available CPUs is used.如果指定的值大于可用 CPU 的数量,则使用实际可用的 CPU 数量。

I'm not sure what the best usage of MAXDOP is, however I would take a guess and say that if you have a table with 8 partitions on it, you would want to specify MAXDOP(8) due to I/O limitations, but I could be wrong.我不确定MAXDOP的最佳用法是什么,但是我会猜测并说如果您有一个有 8 个分区的表,由于 I/O 限制,您会想要指定MAXDOP(8) ,但是我可能是错的。

Here are a few quick links I found about MAXDOP :以下是我找到的一些有关MAXDOP快速链接:

Books Online: Degree of Parallelism在线书籍:并行度

General guidelines to use to configure the MAXDOP option用于配置 MAXDOP 选项的一般准则

This is a general rambling on Parallelism in SQL Server, it might not answer your question directly.这是对 SQL Server 中并行性的一般性漫谈,它可能无法直接回答您的问题。

From Books Online, on MAXDOP:来自 MAXDOP 在线书籍:

Sets the maximum number of processors the query processor can use to execute a single index statement.设置查询处理器可用于执行单个索引语句的最大处理器数。 Fewer processors may be used depending on the current system workload.根据当前系统工作负载,可以使用更少的处理器。

See Rickie Lee's blog on parallelism and CXPACKET wait type.请参阅Rickie Lee关于并行性和 CXPACKET 等待类型 的博客 It's quite interesting.这很有趣。

Generally, in an OLTP database, my opinion is that if a query is so costly it needs to be executed on several processors, the query needs to be re-written into something more efficient.通常,在 OLTP 数据库中,我的观点是,如果查询成本如此之高以至于需要在多个处理器上执行,则需要将查询重新编写为更有效的内容。

Why you get better results adding MAXDOP(1)?为什么添加 MAXDOP(1) 会获得更好的结果? Hard to tell without the actual execution plans, but it might be so simple as that the execution plan is totally different that without the OPTION, for instance using a different index (or more likely) JOINing differently, using MERGE or HASH joins.没有实际的执行计划很难说,但它可能很简单,因为执行计划与没有 OPTION 的情况完全不同,例如使用不同的索引(或更可能)以不同的方式加入,使用 MERGE 或 HASH 连接。

As something of an aside, MAXDOP can apparently be used as a workaround to a potentially nasty bug:顺便说一句,MAXDOP 显然可以用作解决潜在令人讨厌的错误的方法:

Returned identity values not always correct 返回的身份值并不总是正确的

There are a couple of parallization bugs in SQL server with abnormal input. SQL server 中有几个并行化错误,输入异常。 OPTION(MAXDOP 1) will sidestep them. OPTION(MAXDOP 1) 会回避他们。

EDIT: Old.编辑:旧的。 My testing was done largely on SQL 2005. Most of these seem to not exist anymore, but every once in awhile we question the assumption when SQL 2014 does something dumb and we go back to the old way and it works.我的测试主要是在 SQL 2005 上完成的。其中大多数似乎不再存在,但每隔一段时间我们就会质疑 SQL 2014 做了一些愚蠢的事情时的假设,我们又回到了旧的方式并且它起作用了。 We never managed to demonstrate that it wasn't just a bad plan generation on more recent cases though since SQL server can be relied on to get the old way right in newer versions.我们从未设法证明它不仅仅是在最近的情况下生成错误的计划,因为可以依靠 SQL Server 在较新版本中获得正确的旧方法。 Since all cases were IO bound queries MAXDOP 1 doesn't hurt.由于所有情况都是 IO 绑定查询,因此 MAXDOP 1 不会受到影响。

Adding my two cents, based on a performance issue I observed.根据我观察到的性能问题,加上我的两分钱。

If simple queries are getting parellelized unnecessarily, it can bring more problems than solving one.如果简单的查询被不必要地并行化,它会带来比解决一个更多的问题。 However, before adding MAXDOP into the query as "knee-jerk" fix, there are some server settings to check.但是,在将 MAXDOP 添加到查询中作为“膝跳”修复之前,需要检查一些服务器设置。

In Jeremiah Peschka - Five SQL Server Settings to Change , MAXDOP and "COST THRESHOLD FOR PARALLELISM" ( CTFP ) are mentioned as important settings to check.Jeremiah Peschka - 要更改的五个 SQL Server 设置中,MAXDOP 和“并行成本阈值”( CTFP ) 被提及为要检查的重要设置。

Note: Paul White mentioned max server memory aslo as a setting to check, in a response to Performance problem after migration from SQL Server 2005 to 2012 .注意:Paul White 提到了max server memory作为要检查的设置,以响应从 SQL Server 2005 迁移到 2012 后的性能问题 A good kb article to read is Using large amounts of memory can result in an inefficient plan in SQL Server一篇很好的知识库文章是使用大量内存会导致 SQL Server 中的计划效率低下

Jonathan Kehayias - Tuning 'cost threshold for parallelism' from the Plan Cache helps to find out good value for CTFP . Jonathan Kehayias - 从计划缓存中调整“并行成本阈值”有助于找出CTFP良好价值。

Why is cost threshold for parallelism ignored? 为什么忽略并行性的成本阈值?

Aaron Bertrand - Six reasons you should be nervous about parallelism has a discussion about some scenario where MAXDOP is the solution. Aaron Bertrand - 您应该对并行性感到紧张的六个原因讨论了 MAXDOP 是解决方案的某些场景。

Parallelism-Inhibiting Components are mentioned in Paul White - Forcing a Parallel Query Execution Plan并行抑制组件在Paul White - Forces a Parallel Query Execution Plan中提到

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM