Force Parallelism

Forcing Parallelism – T-SQL Tuesday

T-SQL Tuesday

T-SQL Tuesday is a monthly event where SQL Server bloggers write a post based on a subject chosen by the month’s host. This month’s host is Brent Ozar (b/t) and this months topic is SQL Server Bugs & Enhancement Requests.

My enhancement request for SQL Server is a new query hint to force parallelism.

Parallelism

If the cost of processing a query is above the Cost Threshold for Parallelism (CTP), the optimiser may choose a parallel execution plan. Parallelism splits the work up and distributes it across CPU cores to get the job done quicker.

The optimiser doesn’t always produce a parallel plan when it should. The use of scalar functions, some built-in functions and reading from system tables will inhibit a parallel plan. If these (and other) parallelism inhibiting features are avoided, it is still possible that a parallel plan will not be produced.

Out of date statistics, very complicated queries and table variables can lead the optimiser to make inaccurate estimates on the cost of a query. This is when the optimiser can mistakenly cost a query under the CTP and produce a serial plan.

If changing the query requires multiple sign offs and testing (as it should), it can be difficult to improve the cardinality estimates for the query to get the parallel plan. The same goes if the query is part of a 3rd party app.

Query Hints

At these times, query hints may be seen as an acceptable workaround to influence the optimiser’s plan choice. Query hints can be seen as a smaller change by management as there is no refactoring or logic changes. Hints can also be injected via plan guides or the query store to avoid code changes.

I would like to see a hint that causes the optimiser to consider a parallel plan no matter the cost of the query. It’s possible to get this behaviour with trace flag 8649 but it’s unsupported by Microsoft and therefore unfit for production use.

I only tend to use query hints as a very last resort. It’s almost always better to allow the optimiser to make these decisions and continue to reevaluate these decisions as your data changes but sometimes they can be a get out of jail free card.

I’ve written about this before and so has Paul White. Paul created this connect item for something similar. Please vote it up if you think it could help with your query tuning.

About the author

James Anderson

I'm a freelance SQL Server DBA in Southampton, UK. Performance tuning code and improving development processes are what I love to do for work. Outside of work, I live to travel to as many places as possible with my wife Sarah.

Leave a Reply

Your email address will not be published. Required fields are marked *

Sign Up

James Anderson SQL Server DBA

Hi, I'm James Anderson. When I'm not blogging about SQL Server, I do this.

DBA Stackexchange Profile