Off Histogram Join Problems

YouTube player

Chapters

  • 00:00:00 – Introduction
  • 00:06:01 – Joins Don’t Get a Fallback
  • 00:06:53 – Building the New Orders Temp Table
  • 00:07:44 – Legacy Estimator: The One-Row Guess
  • 00:08:40 – Default Estimator Guesses One Row Too
  • 00:08:58 – The Amendment Hint and an Adaptive Join
  • 00:09:36 – Why a One-Row Guess Hurts
  • 00:10:14 – Cold Buffer Pool and No Prefetch
  • 00:12:29 – Fixing It With a Stats Update
  • 00:13:39 – Drop Clean Buffers Again: A Hash Join
  • 00:14:33 – Wrap-Up
Full transcript

Erik Darling here. Darling Data, your number one source for stuff. Like the dark web. You just get what you get. Maybe, I don’t know, it’s kind of like Temu too. One time my wife was, let’s call it late night shopping, and she bought what she thought was a pair of really cheap Doc Martens for one of our kids. from Temu. And, uh, when they, when the Doc Martens finally showed up, uh, it was in a very small padded envelope, and it was a boot on a keychain. Anyway. Uh, we’re gonna talk about off-histogram join problems in this one, cause I, I, I recorded a video recently, uh, about, me and Bats recorded a video recently, rather, the Royal Bats, about, uh, off-histogram cardinality estimate problems. And, um, you know, uh, I, I used, I used a robot friend to formalize my proposal, because it, it occurred to me that I am not good at writing formal proposals. So, I, I, I got a little bit of help and I was, I was pretty pleased with the help, but then there was a comment on the video about, um, this, this sort of thing happening, uh, when you, uh, join tables together sometimes too. And, um, I, I, I found a pretty easy way to demo this using, uh, a temp table.

So, let’s, let’s go and look at that. But, of course, before we do, I have to try to sell you crap, much like Temu and the Dark Web, but, uh, of higher quality and pedigree, I hope, anyway. Uh, down in the video description, there are all sorts of very helpful links, uh, for the both of us to, to benefit immensely in life. Me, monetarily, and you, uh, SQL Server-ly. Uh, you can hire me for a consulting, you can purchase, uh, my training, uh, you can become a supporting member of the channel, uh, ask me questions for free, uh, or office hours, mostly every Tuesday, except when travel is impacted, uh, or, um, you know, whatever other reason might come up. Uh, and, of course, as always, please do like, subscribe, tell, you know, your friends, family, that whole thing. Um, my job these days is, uh, a whole lot of helping people use robots better.

Um, I’ve gotten pretty good at it, building, um, my performance monitoring tool, performance studio, um, having the robots work on my open source stored procedures, and, of course, uh, you know, I have a lot of clients who use a lot of robots, and, um, not a lot of people know how to use a lot of robots very well. Um, there’s a lot of, um, there’s a lot of apprehension, confusion, just not knowing really how to get, get up and started, um, and how to use them effectively when dealing with SQL Server. So, I’ve got a couple things to try and help out the world at large. Um, I’ve got a course that will launch fully on December 1st.

Uh, I’m recording it now. Not, not, not right this moment. Obviously, I’m recording this right now, and, you know, can’t, can’t record two things at once. It’s, what, what am I, it’s impossible. Uh, and I’m, I’m not AI videoing myself, even though, uh, my, an AI video of me, uh, I would, I would never have hair problems, right? Eh, it’s just, man, I’ll start wearing a wig. Uh, someone, well, what’s, this video is gonna end up getting pulled because someone outside is playing the loudest song I’ve ever heard in my life.

Anyway, uh, I wonder what it’s like to sit in that car. You, you’d probably be dead or deaf, one of those things. Um, anyway, uh, if you’re a company and you need that, uh, that’s that one. If you’re a person and you need, it’s that one. Um, pardon the musical interlude there. Uh, if you need free, and, um, I, I, I know, uh, it’s, it’s free, and that should make it very easy to talk people into, but some people need extra talking into when something is free.

Uh, but it’s free, SQL Server Performance Monitoring, and it’s great, and it gets better every day because I’m always working on it, unlike those schlubs who work on other monitoring tools that already sucked and, like, is, like, where are the release notes? That, nothing, right? Like, uh, fixed a comma. Uh, who cares, right? They weren’t good to begin with. Uh, mine, mine’s good now, and it costs you nothing. So, uh, if you don’t have a SQL Server or Postgres Monitoring Tool, and, and you need one, uh, a free one is a very easy way to get one.

And, uh, on top of that, if, if, if your paid crappy monitoring tool is coming up for a renewal, and you’re looking at the price quote saying, I don’t want to pay this, uh, there’s a free one you can get to replace that hunk of junk. So, uh, you should do that. Anyway, uh, we are still Novembering, we are having birthdays, we are having Thanksgiving things, at least if you’re in the continental United States.

Um, and, yeah, look at all this stuff going on. Every time I look at this, I see something new. Like, the gravy is spilling one way, and the pie is flying away another way, and, uh, looks like a, probably a couple pretty good bottles of Bordeaux there. Ah, look at that. Anyway, I wonder what that, is that supposed to be mashed potatoes, or, I don’t know. It’s very odd looking.

Anyway, uh, I don’t know if that’s a legitimate number of birthday candles for me. It very well may be. Anyway, let’s talk about this whole thing. So, uh, joins, uh, don’t get a fallback. And, uh, it would, it would seem, uh, whoever is, uh, driving that car is now getting chased by the police.

Because there’s a lot of sirens. Anyway, who knows what they did? Probably, anyway, hard to know. But, uh, joins don’t really get any fallback. Um, none of the, the guesses that we saw from clip two applied to, uh, to apply to, uh, a little rearranging on the fly here. Uh, none of the guesses or cardinality estimates that we saw, uh, from the other video that I did, uh, will apply to a join.

And I’m going to talk through that today. First, you must turn on actual execution plans. Man, we got to do this whole thing. All right. Cool. So we’re going to, uh, do that. And we’re, we’re going to, we’re going to create this temp table called New Orders.

And, uh, it’s, you know, man, it’s a lot busy outside. Uh, if, if you don’t currently listen to a band called New Order, you should. Because they’re, they’re a good band. Um, so it isn’t a side, uh, but they’re not, it’s New Order singular, not New Orders plural, like, like, like the temp table. I’m dating myself a little bit here. Anyway, let us insert some rows into a temp table.

Uh, and remember this, this, this temp table does have a, a clustered primary key on it. So, uh, things should, things should be okay there, but we’ll quickly see, uh, that they are not. So, um, the, the stored procedure that I have here, uh, executes queries with different hints and whatnot applied.

So, we’re going to look at how this all turns out. First, with the legacy cardinality estimator here, right? And, uh, of course, this thing, when we, when we run the query, uh, we get an estimate of one, right? Uh, over here, right? Uh, it’s a bit of a, a strange thing.

Uh, but, you know, SQL Server was like, uh, well, I think I’m going to get this many rows from here. And, uh, I think, uh, this many rows are, are going to come out of here, uh, which, which, which makes sense a bit. But then, uh, right here, it’s just like, uh, no, I think, I think that’s one row.

And, and, and over here, uh, it’s still guessing one. And it’s, it’s, it’s still giving us one here. And it’s not, maybe, maybe not the smartest guess in the world, right?

Um, with the default cardinality estimator, right? Or the thing that the SQL Server is like, ah, we got this new cardinality estimator. We’re going to call it the default one, because that’s the one you should default to.

Uh, it does, does the same dumb thing, right? Guess, guess is one row after that. And so that’s, that’s not very helpful. And then if we run, uh, the, the default with, with, uh, that amendment hint that I was talking about before, ah, we, uh, we bum out there.

And then if we run it, if we run the, the legacy cardinality estimator with our amendment hint, then all of a sudden SQL Server is like, oh yeah, by the way, I think, I think we’ll get a lot more rows out of here. So many rows that I think we will use an adaptive join.

And, uh, if we, we hover over the adaptive join, we will see that SQL Server has chosen a hash match, a hash join in batch mode, a hash batch, as I will call it lovingly from now on. So, um, when you run into problems with this stuff, that can happen for many reasons, right?

Like the query plan that we saw early, like first, anyway, uh, where am I scrolling to here? Uh, is that the right way? There we go.

Uh, I, I use many different mice and some of them have different scroll behaviors than others. We run it like this, right? Uh, it’s, it’s, it’s probably not like too big of a deal, but just imagine that, that, that guess of, of one, uh, led to a lot more stuff happening down here. Right?

You ended up in this nested loops join for a lot longer of a time. That, that, that, that wouldn’t be a good, good, good feeling, I don’t think. And, and that, that can really sort of get exacerbated by, um, uh, cases where you are dealing with a cold buffer pool rather than a warm one, right? A warm buffer pool is, uh, much nicer than a cold, cold pool.

Uh, all sorts of strange things happen in here, right? Like, uh, SQL Server is like, Hey, I think we’re going to get one row out of this thing. Uh, and, and when we, we, we don’t, we get nine, 99,029 rows and we go into a nested loops join maintaining that one row guess and, and keeping that.

And like, that, that, that’s not a good time in there, right? We spill a little bit, right? That’s maybe like a less than a hundred milliseconds of spill.

The spill isn’t like the biggest deal in this thing. Um, what, what, what happens in here that is, uh, rather annoying is that, um, when SQL Server estimates fewer than 25 rows are going to, uh, come out of a thing for a nested loops join, uh, it, it, it, it doesn’t, uh, it doesn’t really use the, the optimized nested loops, but more importantly, and it’s, it’s hard to see because it’s, it’s not here. It would be here if it happened, but sometimes when you look at the properties of a nested loops join, um, you should always be looking at the properties of your query plan operators.

If you’re not, you’re missing out on so much good stuff, but down here, sometimes you will see, uh, an attribute, uh, where SQL Server has chosen to prefetch either in an ordered or unordered fashion. And that prefetch can really speed things up, but you don’t get that with, with, uh, uh, an estimate of fewer than 25 rows and SQL Server guessing one row here, but actually encountering 99,029 rows. Uh, we had a bad time, right?

Uh, we did not prefetch anything. And so each one of those nested loops went and grabbed stuff, right? Uh, from, from the buffer rather than like reading ahead a whole bunch and then getting the pages that we would probably need in the future. to speculate, speculatively have needed in the future.

We didn’t do that. And we just kept going, getting over and over and over. Right. So let’s, um, talk about what some fixing stuff looks like.

Uh, if, if we update, uh, these tables and we, uh, I’m using full scan here just to, just to stay safe. Um, you know, there’s a lot of times when I’ve tried to write demos where I find like that, that, that perfect, uh, uh, statistics update scan level.

And, uh, sometimes I’m like, ah, I found it. And then the next time I run, I’m like, ah, I didn’t find it like, man, it’s disappointing. Uh, so I, I, I just use full scan here to, to keep things, um, keep things fresh and fresh and clean. Run this look what, uh, now SQL Server is like, ah, cool.

I got, I got a very precise number of rows here. And then, uh, it, it, it, it almost doesn’t matter all that much, uh, because we get, we get correct estimates, uh, everywhere we look, everywhere we go. Going back to the, uh, the orders surge table though.

Uh, what do we get here? All right. Correct estimates. All right. Every, everyone’s happy. And, uh, you know, we don’t, we don’t have all the problems that we were encountering before. But, uh, we, of course need to do that drop clean buffers test one more time and, and rather than, uh, getting a nested loops join, uh, we get the, the, the hash join to begin with.

Uh, there, I, I could force a loop join to show you what it would look like with the prefetch in there, but that, that wouldn’t, that maybe would be a little bit unfair. Uh, it’s typically nicer to let the optimizer make some choices, right? Not, not, not too many because, uh, those, uh, that optimizer, man, it’s, sometimes I feel like its head has been boiled a little bit too much.

There’s been a little few too many, uh, acupuncture needles stuck in there over the years and it, uh, it’s got too many choices, right? It’s too many choices. Anyway.

Thank you for watching. I hope you enjoyed yourselves. I hope you learned something and, uh, I will see you in tomorrow’s video, uh, where we are going to talk about some insert crap, uh, before I, uh, really start pounding you with marketing material for my, my new, uh, uh, course to work with databases the way I do, uh, where I teach you about the robots. Anyway, you got that to look forward to.

Thank you for watching. Hope you enjoyed yourselves. I hope you learned something. Um, and I will see you in tomorrow’s video where, again, we will talk about, uh, insert stuff. All right.

Goodbye. Bye.

Going Further

Someone still has to know enough SQL Server to keep the robots in check, and that’s what my training is for.



Leave a Reply

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