Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can MAXDOP be set for just a stored procedure?

Tags:

sql-server

I have a stored procedure that contains many SELECT commands.

Instead of putting OPTION (MAXDOP 8) at the end of every select command, is there a way I can set it at the start of the stored procedure and remove it at the end, without actually setting it on the server?

ex.

sp_configure 'max degree of parallelism', 4
go
reconfigure with override
like image 924
Ryan Pope Avatar asked Sep 30 '10 19:09

Ryan Pope


People also ask

What should I set Maxdop to?

Setting MAXDOP to 2, 4, or 8 generally provides the best results in most use cases. We recommend that you test your workload and monitor for any parallelism-related wait types such as CXPACKET .

Where can I use Maxdop in SQL query?

The MAXDOP option is utilized to powers the maximum level of parallelism value at the individual T-SQL statement. MAXDOP value cannot be the same as the SQL Server Degree of Parallelism parameter value. Actually, it is more helpful when additional CPU resources are required to execute the specific SQL statement.

How do you set Maxdop at database level?

Simply open the database properties dialog in the UI from Object Explorer and navigate to the Options tab. Once on the Options tab you'll see a new section that holds configuration settings for MAXDOP, Legacy Cardinality Estimation, Parameter Sniffing, and Query Optimizer Fixes.

How is Maxdop calculated?

There is no way to "calculate" the best MAXDOP setting for your environment. This is a configuration setting which is unique to every usage. Unless you are actually having an issue, I do not recommend changing it from the default value of 0.


2 Answers

I wish there was a way to set MAXDOP for the scope of the proc but I agree there is no way to do this.

To counter some of the advice above, there are great reasons for using the MAXDOP hint, such as for after hours index builds, updates or data builds in an multi processor environment, but where MAXDOP is set lower than the processor count to help balance activity during busy "peak" hours, such as normal business hours. Unlike some hints, such as index hints, this will not force your processes to use more threads, but it will enable it to do so if the plan so chooses.

Some index builds will execute nearly twice as fast if you double the threads, so there is a genuine benefit.

like image 135
Crew Avatar answered Sep 25 '22 15:09

Crew


Sadly, no.

Your options are either set it at the server level using sp_configure 'max degree of parallelism', or update each SELECT statement in your stored procedure to use OPTION (MAXDOP 8).

That said, query options should be a last resort and if your queries are performing poorly, there may be an underlying problem.

like image 24
LittleBobbyTables - Au Revoir Avatar answered Sep 22 '22 15:09

LittleBobbyTables - Au Revoir