Off-histogram Join Problems Redux

YouTube player

Chapters

  • 00:00:00 – Introduction
  • 00:03:08 – The Orders Histogram Falls Short
  • 00:04:08 – Fixing Yesterday’s Data Type Mistake
  • 00:05:04 – Every Procedure Guesses One Row
  • 00:05:35 – Why the Histograms Can’t Line Up
  • 00:06:14 – Unordered Prefetch on the First Join
  • 00:07:00 – No Prefetch Below 25 Rows
  • 00:07:29 – Legacy Estimator Plus the Amendment Hint
  • 00:08:05 – Cold Buffer Pool: Every Seek Goes to Disk
  • 00:09:08 – Running With the Legacy Amendment Hint
  • 00:10:06 – Fixing It With a Stats Update
  • 00:10:26 – Wrap-Up
Full transcript

Erik Darling here with Darling Data, and we are going to have a Darling Data do-over, or I guess the fancy word for that would be a redux, and we’ll be celebrating Forgiveness Friday, because yesterday when I recorded the video about this, it was a demo that I smushed into the larger batch of cardinality estimation demos that I realized didn’t make sense the way some of it was placed, and so I figured this would be a better way to show you in isolation the stuff that I wanted to show you yesterday that was a bit of a bit of a mashed potato. It’s too late to take it back. It’s out there in the world, but we’re going to do better today. Down in the video description, you will find all sorts of helpful links where you can hire me for consulting of many varieties. You can buy my training of many varieties.

You can become a supporting member of also many varieties, but as few as $4 a month, you could contribute to me getting a better haircut, or give me a lot of money. I could shave my head and get it tattooed, and that’d be cool too. There’s lots of stuff that could happen here, but it all depends on you. Also, depending on you is liking, subscribing, and telling a friend, because if you don’t do that, I won’t make friends either. I won’t make new friends, so that would be depressing. A lot of what I do with clients these days is not just directly help them with SQL Server, but teach people how to do stuff better with the robots talking to SQL Server.

A lot of people have robot trust issues, which I understand. They’re kind of silly, though, because, you know, there’ll be people like, well, what if it drops a database? Like, but what if you didn’t give it permission to do that? Like, what if you hired some other person who is an idiot, and they came in, and they did all the things you’re worried about robots doing? And I guess you’d have someone to yell at and fire, but, you know, you’d still have all the same problems. It would still have the same outcome, right?

So, both for individuals and for companies, there are ways to have me help you get robo-better with that stuff. Always, my free SQL Server and Postgres performance monitoring remains free, remains available for acquisition to the highest bidder. I just don’t accept tokens as payment, because that’s your problem after that.

But it’s awesome, and it replaces the crappy paid monitoring tool that you have today that tells you nothing, helps you with nothing, and probably hasn’t actually seen a real update in years. Anyway, still marveling at this image here. I can’t wait to see. I can’t wait to see what we get for 2027. I don’t know what it’s going to be. It’s going to be a big year. I sense good things for 2027. Huge things. Huge.

All right. So, to start things off, we’ve got this query, which is going to show us what is happening in our orders table. All right. So, much, much more easy to explain with numbers. We’ve got statistics for rows that go up to 10,6105.

That is the number of rows in the histogram, and that is, well, it’s not the number of rows in the histogram. It is. We only get 200 step histograms, but the statistics rows go up to that number. And the end of the histogram is, coincidentally, that number.

We would auto-update stats if we hit 100,030 modifications, but we are only at 99,029 bottles of beer on the wall. So, we do not get an auto-update stat. So, SQL Server’s histogram currently remains at this number.

All right. So, just like yesterday, and another thing I need to apologize for in yesterday’s video was I did not have this string correctly converted to the right data type. And we were getting a lousy-looking query plan for that. So, I have corrected that here.

And upon correcting that, now we have a much better query plan. We do not have that constant scan and all that weird dynamic seek stuff that SQL Server does when data types maybe don’t exactly match the way they should. But that means that the rows in this temp table, also very coincidentally, my friends, are the 99,029 rows that we do not have represented in our histogram.

Ain’t that crazy? Isn’t that nuts? All right. So, we have precisely that. So, just like yesterday, every one of these stored procedures will estimate, excamate, a single row for cardinality estimation at the nested loops join, right?

You see one there, you see one there, and if we scroll down to this third one, we also see one here. Now, just to spend a little bit of time on this, and just sort of maybe explain myself a little bit, let’s, I’m probably doing this backwards and moving the bottom stuff up to the top, but that’s fine. So, SQL Server knows that 99,029 rows will leave this temporary table.

It has figured that out, right? And this temporary table has a histogram on it, but when SQL Server goes to align the histogram from the temp table with the histogram from the orders table, it can’t do it, right? Because the histogram for the temp table has all those rows that aren’t represented in the orders table.

And so, SQL Server keeps guessing that one row is going to match at the join. Even though it knows 99,029 rows leave here, it’s going to have to do this nested loops join 99,029 times. Now, something I can show you here that it’s not, that helps show the lack of it in the next join, is if we look at the properties of this nested loops join, it gets an unordered prefetch.

Okay? That means that when SQL Server looked at, did the cardinalities, it said, well, how many, how many times am I going to have to do this, do that nested loops join? It said, oh, a lot.

I’m going to go fetch a whole bunch of pages at once, bring them into the buffer pool, and then they’ll be there when I need them for the next loop. So, I don’t have, every loop doesn’t represent a physical read from disk. That wouldn’t be cool either, right?

This is not an optimized nested loop, but that is not the important part here. Now, remember where this thing was here, right? Because when we click on this nested loops join, it disappears, right?

There’s unordered prefetch. There’s no unordered prefetch, right? So, when SQL Server narrows the cardinality estimate at, ah, go away, tooltip, you stinker. When SQL Server narrows the cardinality estimate at this nested loops join to one, this nested loops join is no longer eligible for a prefetching optimization because you need 25 or more rows for that.

So, if we use legacy and amendment here, so that is using the legacy cardinality alligator estimator plus the histogram amendment hint, which I’ll show you the full version of in a moment. And we look at this, now SQL Server correctly guesses 99,000 rows here, and it chooses, remember, the old hash batch join for this one, right? It’s the hash join and batch mode, the old hash batch, soft-baked cookie.

Now, if we run this one, and we look at how long it took, this is with a cold buffer cache. Remember, we spend, well, I mean, the whole query still takes about 1.9 seconds, but 1.5 seconds are spent doing this seek over and over again because every time we do this, we go to disk, right? Every time we do this, we go and we read data from disk into memory.

If we look at the wait stats for this, maybe we’ll see the PAGEIOLATCH, wait, yep, there it is. There she is, right, there is nearly 1.3 seconds of PAGEIOLATCH_SH going to disk, right, rather than the pages being already in memory because we didn’t prefetch them, right? So that’s a sad state of affairs, right?

So let’s, this is sort of the way that query looked, and these are the hints that got applied in there, right? Force legacy cardinality estimation, enable histogram amendment for ascending keys. Now, let’s do this, and let’s use the legacy amendment hint, right?

Now, when we run it, when we run it, remember, legacy cardinality estimation, when we run it like this, we don’t get that one row guessing. We don’t get a loop join hint anymore, right? Rather, we don’t get a loop join here anymore because SQL Server chooses the hash batch join, but that’s not really the important thing, right?

The important thing is that the bad estimate led to, I mean, it is technically a query plan choice, but it’s one that’s deeper down than most people will ever look, right? Always be looking at the properties of things because they can tell you one hell of a story.

But if we, of course, update statistics on our orders table, and then when that statistics update finishes, we run this, and then we run this, we get the query plan that we were kind of after there, right? So, this video shows a little bit more geared towards what I wanted to do yesterday.

It was just chopped up, and there were too many other things in between, but this is a nice, concise, consolidated set of demos for you to hopefully see what I was talking about better yesterday. Anyway, thank you for watching.

Hope you enjoyed yourselves. I hope you learned something. I hope you all have a very nice Forgiveness Friday and find the forgiveness in your hearts necessary to forgive me for Thursday’s somewhat confused video. Anyway, I’m going to go Friday myself. I’m going to go forgive myself.

All right. See you next Tuesday with Office Hours. Yeah. I’m so good at excitement. All right. Anyway, goodbye.

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 *