SQL Server Performance Office Hours Episode 96

YouTube player

To ask your questions, head over here.

Chapters

  • 00:00:00 – Introduction
  • 00:06:46 – Replacing EXISTS Semi-Joins With CROSS APPLY
  • 00:07:58 – The Last Time I Used sp_tableoption
  • 00:08:37 – Indexing Strategies That Scale as Data Grows
  • 00:11:34 – Why SQL Server Behaves Irrationally Under Pressure
  • 00:13:04 – The Hardest Performance Problems I’ve Debugged
  • 00:16:14 – Wrap-Up
Full transcript

Erik Darling here with Darling Data, and I apologize for the video publishing. Last week I was, of course, in Seattle at PASS Summit West, and I had planned on bringing my little camera thing that I recorded while I was traveling in Europe over the summer, and I forgot it, and so I was like, you know, it’s cool, you know, I’ll the adoring public, the fans will forgive me for missing a week, maybe, I don’t know. But then, like many Americans, my travel was impacted, and I ended up not getting home until yesterday, and yeah, that was interesting. So, yeah, I’ve had quite a bit of quite a travel away time, and yesterday I was, I was, I was, I was business drunk, and did not, did not choose to record an office hours video. So, we’re gonna do office hours today, Wednesday, because it’s that time, and I have finally gotten dug out from being away for 10, 11 days, rather than the planned number of days. So, anyway, welcome to a belated office hours. Down in the video description, you will still, I have not been impacted by anything further, so you can still, hopefully you are not impacted by anything, you can still hire me for consulting, buy my training, become a supporting member of my YouTube channel, ask me office hours questions, just like the ones that I will answer today. But, I don’t know, maybe, hopefully better, maybe you have better questions out there in the world.

Uh, and of course, if you enjoy this content, uh, to any degree, uh, gosh, you can like, subscribe, tell a friend, um, I don’t know, put me, put me on your favorite playlist, uh, whatever, and, um, any of that. Uh, I, I have begun helping people do better with robots. Um, the robots don’t seem to be going away, uh, the, the various bubbles and other things that have been predicted, uh, have, have, have, have still not happened. And so, uh, I’ve just come to terms of the fact that, um, people need a lot less, uh, uh, you know, uh, help with SQL Server directly. They need help doing their jobs, uh, with SQL Server with the robots. And, uh, since I have apparently a lot of experience built up with that, uh, since the robots have, have come into our lives, um, I have, I have, I have training for you. Uh, for, if you are a nice person out in the world, and you’d want to do better at your job, uh, using, using the robot friends, uh, I’ve got a course that’s available. It will publish on December 1st.

Right now, it’s at a pre-sale price of 250 bucks, uh, starting December 1st. That will go to the regular sale price of 500 bucks. Um, unless, I don’t know, something real Black Friday-ish happens. We’ll see what happens. Anyway, if you’re a company and you need more specific help for a whole bunch of your employees, uh, to, to get, to get them up to, to speed with the, the, the robots, um, I, I have, I have a one-day class. I’ll come to your company. I’ll, I’ll, I’ll get stuff, uh, set up and working for you in a, in a better way for everyone and, uh, teach people, teach a bunch of people how to do their robot jobs. So, um, there’s, you’ve got, you’ve got two of those.

With all that paid stuff out of the way, how about some free stuff like, uh, SQL Server and Postgres Performance Monitoring? Uh, I don’t know how to talk you further into, uh, free SQL Server Monitoring. Uh, it, it feels like it should sell itself cause it’s free, uh, and it replaces all of those god-awful crappy paid tools that people have been suffering with for years and years. So, uh, if you, if you, if you, if you need that, it, it, it’s here for you.

Um, I, I make the same amount of money either way. Um, so there’s that. Uh, I have finally gotten around to updating, uh, the deck images, uh, for November, uh, only 18 days in. I guess it would have been sooner had I not been impacted by the FAA. Um, November being my birthday month, uh, it, it’s well past my birthday, but, um, you know, a little birthday, a little Thanksgiving, uh, we’ve got a pie flying, melting, flying off a table. You’ve got drunk databases. We’ve got a very sickly database up here in the, in the, looking out the window. I’m not sure what’s going on with this, this one. Um, it’s a good thing. It’s wearing a sweater and a muffler because I, I don’t think its health is, uh, I don’t think its health is quite good. Maybe someone ought to, ought to run CheckDB on that one. Take a, take a backup or something. Anyway, I’m just admiring this image. It’s a bit more realistic than the previous images. I kind of, kind of dig the new style. I don’t know. Usual assortment of missing, uh, actually not too many missing arms. I think, I think beer, beer database over there might be missing an arm.

It’s hard to tell. There is something going on over there. Maybe a leg too. Uh, anyway, uh, I believe you can see that database’s butt, which I’m not sure about. And that, that database standing with the crown up on the table, you can see it’s, it’s crotch a bit there. That’s, ah, well, you know, life is life. Anyway, um, over here, I’m, I’m trying to, uh, get, I said, I tried to update SQL Server Management Studio 2022 and, um, it, it, it broke a bit and I had to roll it back. I’m not sure if it’s gonna open. Uh, we can, we can try and see if anything happens here. Uh, uh, uh, restarting, uh, yeah, I guess. Okay. Well, um, thanks. Thanks, Management Studio.

Really ruining office hours here. Anyway, let’s answer some questions. I’ll figure this out later. Uh, anyway, where were we? Uh, is there anything theoretically deep about replacing where exists semi-joins with cross-apply? I am finding it unreasonably effective for query tuning and I do not know why. Well, whoever you may be, uh, the, the, the, you, you’ve not been very specific about, uh, what form your cross-apply is taking, um, but of course where exists being a semi-join, um, will behave somewhat differently, uh, with the optimizer than, uh, cross-apply, which is, uh, more like an inner join unless you put a top of some form in there. If you’re using a top one, then, uh, you would get the same row goalish behavior. So it’s a little bit unclear, uh, what, what format these queries are taking where you are seeing better performance. But, um, my, my initial guess would be that perhaps the row goal that is, uh, often introduced, uh, when you use exists and a semi-join is present in your plan is not present when you use cross-apply. That would be my first, my, my first guess there.

All right. Uh, I forgot to circle that one. I apologize, whoever you are. Tell me about the last time you used sp_tableoption. Well, last time I used sp_tableoption was when I wrote a blog post about moving lob data off row with sp_tableoption. And that’s about it. I do not find a very, uh, frequent use for sp_tableoption. And I, um, have not used it since then for anything. So I’m sorry to disappoint, but sp_tableoption, not a frequent flyer here at Darling Data. How do you design indexing strategies that scale as data grows? Well, uh, I’m going to assume that, uh, your, your concern, uh, is maybe, well, I can think of a number of concerns here. Uh, one of them is, you know, data gets bigger.

How do you get SQL Server to keep using those indexes, right? Because a SQL Server might start making strange estimates about, uh, maybe, uh, the number of rows that will be emitted by your query and start, start thinking to itself, uh, boy, golly and gosh, I just don’t think I want to do that many key lookups. I think I’m just going to go scan this clustered index instead. Or, um, maybe, uh, you know, there was a whole thing, um, that, uh, video that I recorded, uh, the other week about parameter sensitivity, uh, and, and, and different nonclustered indexes being used, which, which, which also might be a concern for you. Uh, but, but, but generally, um, you know, it’s, it’s, it’s a twofold thing, right? Uh, you, you don’t want to create the overly wide indexes because, you know, those will eventually, uh, gang up on you and start, start clogging up all those modifications you make. It takes a bunch of them, uh, more than five in my experience, but, uh, uh, you know, uh, making sure that, uh, that queries, uh, at minimum have, uh, the, the columns that you care about, uh, indexed properly. Um, you know, like if you, uh, introduce a new query into your ecosystem, uh, and, and it doesn’t have a good index, that, that’s usually not a good thing, especially on those, those growing tables. Um, that, that, that would be the first one, but you all, you also have to consider, um, you know, a querying strategy that, that works as data grows, um, making sure that, uh, you know, you, you write your queries in a way where, where the indexes can be used well and where, um, you know, uh, you, you, you making sure that SQL Server, it doesn’t, you know, like the overly complex queries where, where cardinality estimation becomes, uh, downright impossible. Uh, you know, you, you gotta make sure that your queries are written in a way where SQL Server favors the indexes that you’ve created. That’s a, that’s another big part of it is because it’s not just an indexing strategy. Um, growing data, you know, I’m always going to tell people page compression is, is a, is a, will be a good friend of yours. Um, you know, uh, it’s been, been pointed out many times that that is, uh, only compression at the page level, not at the full column level, but that’s a columnstore index. And you might want to start thinking about those too, uh, as data grows, uh, perhaps a non-clustered columnstore index would be beneficial, uh, depending on your querying strategy. I don’t know there. This is a bit of a, a bit of a big question, but, um, you know, uh, that’s, that’s, that’s, that’s probably good enough for free anyway.

Why does SQL Server sometimes behave irrationally under pressure? I don’t know. Why do people behave irrationally under pressure? You’re under pressure. Things are going wrong. Things are bad. All right. Things are not going as planned. Uh, and sometimes people freak out, like say, um, maybe, uh, you know, you, you go to the airport and you’re like, I’m going to get on this airplane. And then you find out that like 4,000 airplanes got canceled and there’s, there’s no, uh, there’s no airplanes for a few days, uh, because they’re all, they’re all canceled or, or, or rebooked. Um, sometimes in those, in those, in those cases, uh, a person may also behave irrationally. Um, they, they may, you know, decide to, uh, stay at a resort in the Phoenix area for a few days rather than sleep in the airport. You know, uh, there are all sorts of things that someone might do under, under duress that, uh, would, would not maybe be their normal behavior. You know, the important thing for you to understand is that, uh, SQL Server is basically a person at this point. Um, it, it, it, it, it, it, it, it, it, it’s, it, it’s subject to all of the, the harms and fouls under pressure that, uh, that a person is because, um, uh, software is people now, right? That’s, that’s what I keep hearing anyway. Uh, but I don’t know.

Uh, leave me alone. I’m tired. What are the hardest SQL Server performance problems you’ve ever debugged? Um, I’m not going to punt on this one, but, um, I am, but, uh, I am going to, uh, be, be brief with this one. Uh, I think generally the hardest ones to debug are probably around parallelism.

And when parallelism, uh, sometimes does not act very parallel, uh, or sometimes, uh, is, is not uh, uh, maybe it’s behaving rationally under pressure. Um, you know, sometimes a query runs and everything is perfectly fine and all your rows are distributed wonderfully. And then other times that query runs and all your rows end up on a single thread and it’s, it’s very unhappy. And the whole thing takes way longer. Um, other times a parallel query may, uh, deadlock itself sometimes and not other times. There are all sorts of things in that area that, uh, uh, uh, were at least in my experience, the hardest ones to debug because they were often the hardest ones to, uh, to reproduce. Um, because, uh, you know, it’s, it’s like, it’s, it’s like parameter sensitivity, but harder because you, the, the getting, getting the query to, um, you know, sometimes deadlock or getting the query, uh, to sometimes, uh, have, have, have rows misaligned across threads is, is, is a, is a difficult task. Um, because like, you know, with, with parameter sensitivity, right?

It’s like, you just like, oh, well, this is a little value and this is a big value. Uh, I, I can just mess with these and see what happens. Uh, with the parallelism thing, uh, you might run into other, other stuff going on, right? Like, uh, like you might run into circumstances where a single thread, uh, in your parallel query gets blocked by something, right? And holds up the whole rest of the query because this one thread can’t make progress. Uh, you might run into situations where, um, because SQL Server, uh, schedules tasks cooperatively, right? SOS_SCHEDULER_YIELD and all that, that, uh, sometimes it just, it takes a particular load on the server, uh, in order for like one of the CPUs to be very, like, let’s say you have eight CPUs, right?

You have MAXDOP set to eight or zero. I wouldn’t care either way in that point, right? But, um, let’s just say you have a single parallel query running and let’s say that, uh, maybe you have a couple long running tasks that are single threaded running on other CPUs, right? You’re gonna have two of your eight, uh, DOP threads, uh, just get like, like kind of like just gummed up by these other queries constantly asking for attention from two of the CPUs. Things can, that’s another thing that can really, uh, get in the way of, uh, things going well. I promise I’m not business drunk anymore.

Anyway, I think parallel problems like that are some of the most difficult to, uh, debug and figure out because they offer, they often require much more orchestration than, uh, than normal performance problems to, to figure out. Anyway, that’s good here. Thank you for watching. I hope you enjoyed yourselves. I hope you learned something. And, uh, as long as I can get management studio working, I will see you in tomorrow’s video, which will be on Thursday and then, uh, one on Friday, which I usually have off. But, um, again, uh, I, I, I have been impacted friends. I’ve been impacted.

Anyway, see you tomorrow.

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 *