What Is the Purpose for Using Option(Maxdop 1) in SQL Server

Can I use OPTION (MAXDOP 1) for a query within an IF statement?

Interesting. It seems like you cannot use query hints on a query enclosed in parenthesis that is not a derived table - that is, one that expects a single value. Actually, I do not know how this clause is called to search for it through the documentation, so more enlightening answers are welcome.

Still, you can use a variable to do what you want:

SELECT TOP 1 @identifier=Identifier 
FROM SomeTable
WHERE <some conditions>
option (maxdop 1)

IF @identifier IS NOT NULL......

OPTION (MAXDOP) with sub query?

The subquery might not be in one piece at runtime. The optimizer can pick it apart and reorder everything. This query will certainly not execute as written. Therefore a maxdop for a subquery does not apply to SQL Servers execution model.

There can only be one maxdop for the whole query. There is a logical reason for this restriction.

Why does Parallelism affect if my query is successful?

A query language, which SQL is, has nothing to see with a procedural language.
In procedural language, you control the order of each instruction by sequencing the pieces of the code in ordering each parts. This is known as the "how" code...
In a query language, you only specify the "what" code, not the how, and the query is translated into differents sequences of instructions (how code) to do your request, then each different solution to solve your demand are evaluated in terms of cost and finally the optimizer choose the best one that will be executed.

Sometime, the how code is changed because of subtile differences in the query, the session execution parameters, the distribution of the data values...

So you must never presume that a query will be executed the same way ever. You need to secure your query by a logic that eliminate misinterpretation...



Related Topics



Leave a reply



Submit