Prior to SQL Server 2012, this blog post would be wrong. If you’re still on a version prior to 2012, ignore this blog post.
In fact, ignore every blog post and go upgrade those servers. Go on. Get out of here. Show’s over.
After SQL Server 2012, AKA the only versions that currently matter, this blog post is largely correct, and leans more towards correctness as you get to closer to SQL Server 2019.
Sliding scale correctness. Get on board.
Say It Slow
When windowing functions don’t have a Partition By, the parallel zone ends much earlier on than it does with one.
That doesn’t mean it’s always slower, though. My general experience is the opposite, unless you have a good supporting index.
But “good supporting index” is for tomorrow. You’re just going to have to deal with that.
WITH Comments AS ( SELECT ROW_NUMBER() OVER ( ORDER BY c.CreationDate ) AS n FROM dbo.Comments AS c ) SELECT c.* FROM Comments AS c WHERE c.n = 0 OPTION(USE HINT('QUERY_OPTIMIZER_COMPATIBILITY_LEVEL_140'));
With just an Order By in the windowing function, our query plan looks about like so:
Immediately after sorting data, our parallel streams are gathered. This is the end of our parallel zone, and it will occur regardless of if you’re filtering on the windowing function or now. I’m filtering on it here because I don’t want to spend any time retuning rows to SSMS.
Here’s an example of when a parallel zone is started again later:
WITH Comments AS ( SELECT ROW_NUMBER() OVER ( ORDER BY c.CreationDate ) AS n FROM dbo.Comments AS c ) SELECT c.* FROM Comments AS c WHERE c.n > 100000000 OPTION(USE HINT('QUERY_OPTIMIZER_COMPATIBILITY_LEVEL_140'));
Note that there’s no Top in this plan prior to the Filter.
You’ll see a Top generally when you filter on the windowing function with an equality or less-than predicate. Greater than seems to most often not end up with a Top in the plan.
If we let Batch Mode run free, things turn out a little bit different.
WITH Comments AS ( SELECT ROW_NUMBER() OVER ( ORDER BY c.CreationDate ) AS n FROM dbo.Comments AS c ) SELECT c.* FROM Comments AS c WHERE c.n = 0;
The Window Aggregate operator is within the parallel zone, unlike the Segment and Sequence Project operators in the Row Mode plan.
If we reuse the greater-than query from above while allowing Batch Mode to be used, we get a fully parallel plan.
Of course, Partition By adds work, especially in Row Mode, and especially without a supporting index.
The nearly 11 second Row Mode plan compared to the 1.6 second Batch Mode plan doesn’t leave a lot of room for arguing.
It’s also worth noting here that Batch Mode Sorts (at least currently) will always sort on a single thread, unless it’s the child of a Window Aggregate operator, like in the above plan.
Tomorrow, we’ll look at how indexing can improve things, but not just row store indexes!
Everyone knows about those P(artition By) O(rder By) C(overing) indexes, but does that attention to ordering matter as much with column store indexes?
Tune in to find out!
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 on 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.