I know, I know
Here we are in 2017, which means we’re about two years away from the next Ska revival effort, if my watch is correct.
I hope it isn’t, but since Brent sent it to me with a note that says “please stop blogging” and it starts shocking me every time I open a new blog post, I’m pretty sure it’s accurate.
You’re probably sick of hearing about Adaptive Joins by now. “Dead horse!” you’re screaming, about a feature in a product that hasn’t been released yet.
God these shocks hurt.
If you’re not aware of the performance problems scalar valued functions can (and often do) cause, well, uh… click here. We’ll talk in a few days.
If you are, and you’re worried about them crapping on Adaptive Joins, follow along.
The big question I had is if various uses of scalar valued functions would inhibit Adaptive Joins, and it turns out they’re a lot like non-SARGable queries.
Starting with a simple function that doesn’t touch anything.
CREATE FUNCTION [dbo].[ScalarID] ( @Id INT ) RETURNS INT AS BEGIN RETURN 2147483647 END;
We can all agree that it doesn’t access any data and just returns the INT max. Now some queries that call it!
SELECT u.Id, p.Score, dbo.ScalarID(u.Id) --In the SELECT on the Users table FROM dbo.Users_cx AS u JOIN dbo.Posts AS p ON p.OwnerUserId = u.Id WHERE u.LastAccessDate >= '2016-12-01' SELECT u.Id, p.Score FROM dbo.Users_cx AS u JOIN dbo.Posts AS p ON p.OwnerUserId = u.Id WHERE dbo.ScalarID(u.Id) = u.Id --In the WHERE clause on the Users table SELECT u.Id, p.Score, dbo.ScalarID(p.Id) --In the SELECT on the Posts table FROM dbo.Users_cx AS u JOIN dbo.Posts AS p ON p.OwnerUserId = u.Id WHERE u.LastAccessDate >= '2016-12-01' SELECT u.Id, p.Score FROM dbo.Users_cx AS u JOIN dbo.Posts AS p ON p.OwnerUserId = u.Id WHERE dbo.ScalarID(p.Id) = p.Id --In the WHERE clause on the Posts table
If you’re the kind of monster who puts scalar functions in WHERE clauses, you deserve whatever you get. That’s like squatting in high heels.
Not that I’ve ever squatted in high heels.
Alright look, what’s that Brent says? I was young and I needed the money?
Let’s forget about last week.
By this point, you’ve seen enough pictures of Adaptive Join plans. I’ll skip right to the plan that doesn’t use one.
It’s for the last query we ran, with the scalar function in the WHERE clause with a predicate on the Posts table.
See, this isn’t SARGable either (and no, SCHEMABINDING doesn’t change this). When a predicate isn’t SARGable, you take away an index seek as an access choice. You don’t see too many Nested Loops with an index scan on the other end, do you?
So there you go. It’s not the function itself that bops our Adaptive Join on the head, but the lack of SARGability.
Thanks for reading!