Off-histogram Cardinality Estimates in SQL Server

YouTube player

To vote for the feedback item, head over here.

Chapters

  • 00:00:00 – Introduction
  • 00:04:36 – The Feedback Item and a Birthday Wish
  • 00:05:37 – Demo Setup: Stored Procedure and Hints
  • 00:06:12 – Control Set: Values Inside the Histogram
  • 00:07:57 – Rows Past the Histogram and Their Stats
  • 00:10:05 – Default vs Legacy Estimator Off the Histogram
  • 00:12:06 – The Legacy One-Row Guess
  • 00:12:58 – Histogram Amendment for Ascending Keys
  • 00:13:45 – When the Amendment Hint Falls Short
  • 00:15:02 – Updating Stats Fixes It
  • 00:15:28 – None of the Three Options Is Good Enough
  • 00:16:13 – Go Vote for the Proposal
  • 00:16:41 – Wrap-Up
Full transcript

Erik Darling here with Darling Data, and as you can see, I went out and got my PASS haircut, which I’m feeling relatively okay about. It’s a little, maybe not exactly what I was shooting for, but what can you do? At some point in your life, you just gotta be happy that you still have hair. All right. You know, on your head. Anyway. This is perhaps less of a blessing as you get older. Ears and nose and whatnot. I’m not sure what you’re thinking. I have the gray hair that grows directly out of my forehead from time to time. But anyway, today we’re going to talk about off-histogram cardinality because recently I opened a feedback item with my dear and good friends at Microsoft with a humble suggestion to improve to improve our lot in life when dealing with this. And we’re going to talk about that. Down in the video description, you will find so many helpful links. Your mind will spin, boggle. You’ll, I don’t know. I don’t even know if your socks are going to stay on. You know, as a wise man once said, you better hope you have your wood screws in. You know, blow your doors off.

But you can do all of these things by clicking on those. Still to this very day. At this point, maybe you should just have your robots start doing these things because they’re less lazy. Speaking of robots, if you or your company want to learn how to use robots more effectively, efficiently, safely, responsibly, you know, all the happy fuzzy words, that’s, that’s, that’s what I’m, that’s what I’m helping people with. If you’re an individual, or I mean, even if your company just wants to buy a buttload of individual seats, you can do that too. I have a course on presale. I’m going to record it when I get back from PASS. And that will be available December 1st. And after December 1st, the price will go up. Maybe it’ll double, maybe it’ll triple. I don’t know yet. We’re gonna, let’s see how this thing goes.

And then if you are a company, then you can book me for a one day, either remote or in person class where I can help you directly in your environment to set up all the stuff that you need to make your AI data, AI interactions with databases feel nice and kind and cuddly and all that. And if you are still in the market for free SQL Server performance, if you haven’t already bought my free performance monitor, you can do that with that link. And you can monitor SQL Server and you can monitor Postgres, including Aurora Postgres.

Got you covered. All the good stuff that you’d ever want to monitor and look at and whatnot, and you want to get your robots involved in helping you analyze your performance data, I’ve got built-in MCP servers. We’re doing some testing, I guess. Speaking of, well, geez, it’s coming up, isn’t it?

It’s like five days from now. Ah, crap, I gotta pack. Ah, I got so much to do. Why am I recording this? Wasting my time.

PASS Summit West, Seattle, Washington, November 9th through 11th. I have a pre-con. T-SQL that doesn’t suck. Advanced T-SQL.

That’s fun. No editing. No editing here. And I have a regular session about the isolation levels on SQL Server, mostly impugning the character of read-committed, the default isolation level in SQL Server. But anyway, oh wait, it’s November.

I gotta change this. I gotta do a whole new slide. See, I’m falling behind. Like the clocks or something. Anyway, I was gonna put the link to this somewhere, probably in the video, but look at this awful link.

Look at what Microsoft, you just, you can’t. This is, this is, this is, this is what we get. All right.

But I have a short link to it here that I will put in the video description so that you can go vote on this. And, um, I, I, I understand that, uh, yesterday was, uh, the voting day for many, many of you in America. Um, so hopefully you’re not all voted out because, uh, today is my birthday.

And, uh, my birthday wish is for you to click on that link in the video description and vote for, uh, my feedback item. Whether you like it or not, it’s a birthday wish and you must honor it. So, uh, so, uh, just deal with it.

Anyway, I got some boring setup stuff here. And down here, I got query plans turned on. We are good, good, good. So, uh, I’ve got, uh, a stored procedure that, um, there’s a lot of dynamic SQL, um, to, uh, supply, uh, different hints to use different, uh, cardinality estimation models.

Uh, there is a histogram amendment use hint that we get involved here. Uh, and so, you know, uh, we got, we got, we got all sorts of variety of things that we, we use to get different cardinality estimates. So, uh, what, what I’m going to show you in this one is, um, sort of a control set of stored procedure executions.

Uh, these are all values that are inside the histogram. So, they are accurately and adequately represented by the existing histogram, uh, to, uh, for the, for the optimizer to use. The problem that you run into is not with, generally not with stuff like this.

I’ve talked about this in a whole bunch of videos where, um, like, usually your, the histogram you have doesn’t change so, so much, uh, throughout the course of, like, maybe a day or a week or maybe even a month. If, if, uh, depending on the, you know, how busy your database is, that, uh, cardinality estimation gets bad for a given set of values that already exist in there. The biggest problem that I see is, is it’s as soon as SQL Server has to deal with values outside of that.

So, um, you know, using the sort of in histogram values, we get a good enough set of cardinality estimates for this, right? Or, I mean, you’re off by, uh, like, 24 rows or something, uh, and all the plans that you get are pretty stable. They’re all merge-joined plans.

They all have roughly, I mean, actually, they all have exactly the same cardinality estimate. Um, and just to give you a little sort of idea of what, uh, hints I’m using in here, uh, I got, uh, for, for this specific execution. I’ve got recompile, and I’ve got forced legacy cardinality estimation, and I’ve got this other one that not a lot of people get into, uh, to enable histogram amendment for ascending keys.

Uh, we don’t, this isn’t hitting ascending keys, so it doesn’t really do much here. But, um, if we, uh, we look down a little bit further, um, we do have rows that live outside the histogram already loaded in the table. And that gives us problems when we start querying that data.

And the stuff that we get from, excuse me, the cardinality estimates that we get from this are, uh, go away SQL prompt, often not awesome. So, uh, right now we’ve got a couple indexes, ordered date and ordered day. Uh, this is how many rows are in the table, this is how many steps are in the histogram, and this is the modification counter for both of those.

So I’ve loaded 99,029 rows in past the histogram. Uh, we do have the, um, the, the more modern, like, uh, uh, auto-update threshold on this table. So if we had that many rows, it’s like another, I don’t know, thousand rows or something.

Um, if we had that many more rows in there than, like, updated, then we would get an auto-update stats. So this is right under that. This is the behavior of, like, trace flag 2371 in the old days and the, the, uh, any database in compat level 130 or higher.

Or in the, the new days, even though compat level 130 is 2016, right? Uh, but for, uh, both of these, the histograms end, uh, just about the same, uh, time in there, right? Uh, so this one is 926 plus almost 24 hours, and this one is just 926 on the nose.

And these are the rude sirens, uh, estimates that you would get from different, um, different predicate types, uh, throughout both of them. So an open range is just greater than equal to, uh, the closed range is greater than equal to with a less than, and then the default is just equality, right? So, uh, so, uh, that’s, that’s what we got there.

Anyway, let’s boogie on down. Um, so if we look at the last hour, uh, and everything, uh, else that, like, the last hour that’s in the statistics histogram and then everything past it, uh, we get different, uh, results, right? So this is the last hour, uh, everything past it, um, so, well, that’s the one without the time on it, uh, and then, you know, the same thing here.

We’re just switching up the, the cardinality estimators for those two. So, uh, the first two use the default, second two use the legacy, and if we run these and we look at the results, we get some execution plans back with some guesses in them, right? Uh, and, well, you know, I mean, you can, you can, you can think what you want about this guess, but, uh, this is, you know, down here, um, uh, 733 of 29709, uh, maybe not the best.

This, this guess is pretty all right, uh, and then coming on down here, uh, sort of the same, maybe not great thing, uh, we, we get, uh, we get a higher guess, but it’s, it’s, it’s way over, right? Um, it’s, so we got, we got, so far we have, uh, way under, uh, and then way over, and then, uh, we come down a little bit further, and, uh, well, we get sort of a different, uh, plan choice altogether, right? Like, these two start with customers on the outside and do a merge join, uh, and then these two down here, uh, they start with our orders table on the outer, outer part of the join.

Um, the, the row guesses are still maybe not great across, right, which is the point, right? I’m saying we need something new, uh, but, uh, I mean, at least this plan does a little bit better, but 733 rows and a nested loops join supported by an index, eh, you’re not gonna do too terribly on that, even, even though the guess is not great. Uh, down here, though, this one, this is where, you know, things get a bit more alarming, is the legacy cardinality estimator just gives you that one row guess, right?

Like, the default cardinality estimator, uh, it, it, you know, uh, at least doesn’t do that. It makes the same sort of dinky guess, uh, that is much higher than 733, and, uh, of course, you’re gonna jump around on me, and much lower than 99,000. So, like, I don’t think neither one of those is all that great, this sort spills a little bit, uh, you know, these two take 400, 500 milliseconds, uh, the, the, the, the easy nested loops join takes 6 milliseconds, but the, the, the not great nested loops join, uh, that takes 567 milliseconds.

This one spills a little bit, too. But, uh, like, neither one of these really does, um, sort of an awesome job here. And just to compare, um, sort of, like, the, the last hour thing, uh, with, the, the default estimator versus the, um, using that amend, the histogram amendment for ascending keys hint, uh, you’re on these two.

And, like, you know, uh, sort of, like, the same thing above, right? We start with the customers table, choose a merge join, uh, this goes over in here. This doesn’t spill because 733 is way under 29709, so, like, maybe the memory grant is a little too big.

I’m not going to fuss too much about that, but, you know, you might run into stuff where that gets way worse. Uh, but with the amendment thingy, thingy doodad on here, uh, go away. Uh, we do, we do a bit, a bit better, right?

733 or 422, like, that’s at least getting in the neighborhood. But, it doesn’t always work out well. So, like, if you have, um, if you have tables that go really fast, right, and, you know, you have to wait a while before, uh, gosh darn it, um, you know, you get, uh, a stats update, uh, because they’re big tables still, um, you know, uh, you still get some pretty bad guesses, right, on stuff, right?

And these, these all have recompile hints on this, not like this is a parameter sensitivity thing. Um, you know, like, like, like, this was, like, this was an okay plan shape when we found 733 rows, but now we’re like, uh, 3,400, but we got 30, almost 30,000. Uh, maybe less of an awesome choice.

And then, uh, down here, you know, uh, kind of the same deal. Uh, just a way high guess over, right? It’s, like, we’re just not really on the nose with any of these.

So, uh, and then, you know, uh, obviously the Legacy Cardinality estimator, you get that one row guess, and, well, you just can’t trust these things anymore. Uh, all this stuff gets worse under, uh, if you, you know, have a cold cache, too. Uh, these are not worth going into, because you kind of just see the same things.

But, um, obviously, uh, you know, doing a stats update on your own helps, right? If we update stats and then rerun this stuff, uh, then, you know, it doesn’t, like, no matter what Cardinality estimator we use, these rows are in the histogram now, so everything’s fine. Um, I obviously don’t always suggest a full scan of everything, but, you know, you’ll, you can make up your own mind on that.

You know, you have, you have, you have your own servers. So, uh, you know, like, none of these are rather, none of the three sort of options that we have. Um, for this, really do a good job of sort of covering, um, a good Cardinality estimate for off rows.

Um, you know, the Legacy Cardinality estimator gives you one row estimate, no matter how many there are. Um, the Default Cardinality estimator gives you slightly different estimates depending on the, uh, type of, uh, predicate you’re using. Um, so, like, you know, open range, closed range, equality, like I talked about.

And, uh, then the, uh, amend, uh, Cardinality, amend histogram for ascending keys, it, it, it gets you a little bit closer, but still not quite there. So, um, what I would like to, uh, ask you to do is, uh, click on this link, uh, check out the proposal I made to improve things, and, uh, vote for it. Because, um, you know, I can’t show you what that would look like because it doesn’t exist.

But, uh, mathematically, it did make these guesses a lot faster. Anyway, it’s my birthday, and I’m gonna go birthday something. Thank you for watching.

Hope you enjoyed yourselves. I hope you learned something. If it’s your birthday, I hope you have a happy birthday, too. Uh, but otherwise, uh, if it’s not your birthday, you, you’re required to go, uh, grant my birthday wish and, and vote for that. All right.

Thank you for watching.

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 *