Something that many people overlook about parallel plans is that while they are limited to DOP active CPUs, they can suck up way more threads.
How many more depends on how many branches can be active at the same time.
Plus the coordinator thread.
In order to test these limits out, we need a rather large query.
Since I’m lazy, I’m using a bit of dynamic SQL to make the query I want. Along the way figuring out how many joins I could gin up before things went amok, I learned some fun things.
For example, if I used Hash Joins, I’d get an error that the Query Processor ran out of stack space, and if I used Nested Loops joins I wouldn’t get the requisite parallel exchanges necessary to have multiple parallel zones.
And if I don’t use a force order hint, I’ll end up spending a really long time waiting for a query plan to compile. It wasn’t a good time.
There’s also a tipping point with the number of joins, where if I go over a certain number, my query’s DOP gets downgraded to one.
After finding my sweet spot at 316 joins, I still had to toggle with DOP a little.
With my 316 join query, I was able to reserve 634 worker threads.
But that’s where I topped off. After that, the query would get downgraded to DOP 1 and only ask for 1 thread.
I’m not sure if there’s some built-in cap on how many threads a query can use, or if the limit is global before downgrades start happening.
What I found more interesting was that even though the query reserves 634 workers, those workers weren’t immediately subtracted from available workers on the server.
Technically, reserved threads don’t exist yet. They haven’t been created or attached to a task. All the reservation does is prevent other queries from reserving threads beyond the configured limit.
For example, if I run two copies of the big query at the same time, one is downgraded to DOP 1, likely because it’s hinted to DOP 2 and that’s the next lowest DOP, and it can’t run at DOP 2 and reserve 634 more threads that the first query has already reserved.
Thanks for reading!
If this is the kind of SQL Server stuff you love learning about, you’ll love my training. I’m offering a 75% discount to my blog readers if you click from here. I’m also available for consulting if you just don’t have time for that and need to solve performance problems quickly.