SQL Server Performance Office Hours Episode 93

YouTube player

To ask your questions, head over here.

Chapters

  • 00:00:00 – Introduction
  • 00:02:58 – Filtering Parallelism Out of Blocking Alerts
  • 00:04:45 – Why SQL Server Ignores Indexes That Look Perfect
  • 00:08:21 – When Schema Design Is the Root Cause
  • 00:10:29 – What Silently Causes Plan Cache Bloat
  • 00:13:12 – Why Deletes Sometimes Block Everything
  • 00:14:51 – Wrap-Up
Full transcript

Erik Darling here with Darling Data, and it appears that it has become, come to my attention that it has become Tuesday, and I owe you five answers to five questions. Maybe, I don’t know, we’ll see how those line up, right? Maybe, maybe there’ll be more, I don’t know. But down in the video description, all the stuff you can do to help me keep a roof over my head, hire me for consulting, buy my training, become a channel member. There are coupons right in the links to the training and stuff. I don’t know, maybe, maybe I should put some coupons on the consulting. We’ll see what happens. I don’t know.

My birthday is coming up, and I do like to celebrate by spending it on the phone with people, talking about SQL Server. So, it’s always something. You can also use the links below to continue to ask me office hours questions. I do appreciate it. We’re still, we’re still working through things. Lots of questions in there. And you should like, subscribe, and tell a friend if you haven’t already.

And, I mean, if you only told one friend, make a second friend, tell them. If you out there in this wild, crazy, mixed up world are looking for SQL Server and Postgres Performance Monitoring, I’ve got it all. Or I’ve got both. It’s for free. It’s available on GitHub. You don’t need to give me anything for it. You know, it’d be nice if you did. There are ways to, you know, contribute money to the continued development of this project.

But, for now, I just enjoy using it to torture paid vendors who put out really crappy performance monitoring tools because they deserve it. Past Summit West coming up, Seattle, Washington, November 9th through 11th. Glory days, I’m sure. I have a pre-con there, Advanced T-SQL that doesn’t suck. I should have subtitled it, Arguing with Robots or something.

But, you know, it was too late by then. But there’s always next year. And I also have a regular session called Read Committed is Cruel by Default. And it would just bring so much joy into my life if you showed up to both of them.

Anyway, we are still Octobering here, so let’s answer these questions. All right. Hi, Eric, comma. Hello, you, comma.

Loving the performance monitoring tool. Ah, good. Another happy customer. Free customer. We get a lot of blocking alerts where the query has gone parallel, so the blocking session ID is the same as the blocked session ID. Yeah. Is there a way we can filter this out or is this indicating an actual issue other than just parallelism?

So, no, there isn’t a way to filter that out because that is something you should pay attention to. If you have ECIDs within one spid that are blocking each other, that is something that you should look at because one day that blocking might become a parallel deadlock.

And it might not be fun for you having to debug that one. Well, parallel deadlock graphs are beautiful things. They look like, what are the, like Swarovski things.

They’re like Fabergé eggs. They’re quite intricate and lovely. Figuring them out isn’t a lot of fun. So, pay attention to those queries while they’re just blocking.

And look at the query plan. Well, you can get one because you might not if it, if it dies. Sometimes they’re in query stores, sometimes not.

But, crack them open. Look for stuff like order preserving exchanges. Parallel merge joins, stream aggregate, sorts, stuff like that.

Look for any memory consuming operators spilling. You might even see exchange operators spilling in there. And take care of the problem.

Why would I want you to turn a blind eye to a problem? Being silly in all of your pants. Why does SQL Server sometimes ignore indexes that look perfect for a query?

Well, let’s pretend, let’s not answer the boring version of this question. We’ve talked about why SQL Server might ignore a nonclustered index in favor of a clustered index. That’s not that interesting.

Key lookups, random IO, expensive. Let’s pretend that you have two nonclustered indexes. Both of them would result in key lookups. And both of them can result in a seek.

Let’s say that you have three predicates in your where clause. And each of these two indexes leads with a different one. Sometimes, I mean, it’s all going to come down to costing, but that costing is going to come from cardinality estimation.

And SQL Server might be estimating cardinality for one of your predicates either incorrectly or maybe it has inherited it from a different set of parameter values that things were compiled with. And it made accessing very few rows via that other index look cheap and easy. So let’s say that we’re working with the stack overflow database and the vote type ID or post type ID, very uneven distributions in those.

Some of the type IDs, either post or vote, have very few values. Other than have like 37 million, 11 million values attached to them. And let’s say that you compile the plan looking for some type of type ID with a very low cardinality, like say like a couple thousand rows.

And SQL Server is like, oh, sick. I can find a couple thousand rows here and then knock the rest of that out. And great, right?

Like that looked nice and cheap and easy there. SQL Server is like, yeah, I get the right idea. But then the next time you run it and it goes to one of those crazy big numbers, all of a sudden you found 37 million rows instead. Right?

And now you have to do 37 million key lookups to go filter something else. Right? Not a particularly good time. Right? So a lot of the times it is one of those things, right? You have two competing nonclustered indexes.

For one execution, it looks great. For another execution, like the cardinality, one execution, this index looked like way easier to access the data in. And then, you know, all the other normal stuff.

Maybe you’re using local variables or something. Maybe you’re using table variables and, you know, cardinality estimation isn’t great. And like whatever density vector guess you get from local variables, all of a sudden, you know, it’s not so hot for anyone. Yeah.

So there are all those reasons, but it usually, but it’ll come down to cardinality and costing. You know, from there, it is kind of your job to either, you know, force the plan you want in some way. Right?

You could potentially use query store. If you’re feeling real maniacal, you could use a plan guide. If you have good control over the, or you can even use query store. Well, you can use query store hints.

And if, you know, if you, if you’ve got good control over the code, you can add in whatever set of, you know, index join hints for seek and stuff that they get you the plan that you want that works better, more generally across all those. All right. How do you know when schema design is the real root cause of performance issues?

So I’m going to say very generally when your tables are all, how do I do this? No, that’s the wrong one. This wide.

And your indexes are all this wide. That’s usually a pretty good sign. You know, like lack of normalization. That’s a pretty good one.

And, you know, just, I mean, I guess it’s easier to pin it on like, ah, they created a single column index on every column in the table. I mean, that’s, that’s one of the patterns that you, that you sometimes see with those. But, um, another one is, um, you know, just like, you know, very, very narrow indexes that don’t help, they don’t help queries beyond like some limited capacity.

And so they barely ever get used. That’s another good one. Uh, lots of max data types are just, you know, inappropriately sized strings, stuff like that.

Um, you know, um, mismatched columns that should match, uh, the number of databases I’ve seen were like, and I, like, I don’t know. Like, and I, like an ID column in one table is an integer and it’s a VARCAR of some variety in another one. Or like, even like GUIDs, like in one table, it’s a unique identifier and another one, it’s a VARCAR 36.

And you’re like, can we just make up for mine? Can we please just do something, do something useful with our lives? Uh, so there, you know, like there, there are a lot more tells in that, but you know, um, a lot of it, I mean, I’m like, you know, you could, you could try to put it on stuff like, Oh, there’s no foreign keys, but you know, foreign keys don’t really do anything.

Like they, they obviously referential integrity is the thing they do, but like, it’s not like they help SQL Server, like, like, estimate join cardinality better. That’s all, that’s all other stuff anyway, right? Don’t really play in there.

Um, but I, I think, yeah, like, like really wide tables is usually, um, usually the biggest one that I see. Um, do what actually, uh, are you a robot? You send in robots out here.

Actually, what actually silently causes the load bearing crux of plan cache bloat? When does it matter? Um, I mean, tons of things, uh, you know, passing in.

Well, okay. So like the obvious one is when your queries aren’t parameterized, right? Uh, the less obvious one is if you’re using an ORM or, you know, dapper entity framework.

If anyone, I don’t know if anyone’s still using hibernate, uh, JDBC or something, and you are not strongly typing your parameters there. And what happens is the parameter lengths all get inferred, right? So, um, let’s say, you know, the, you run it and you’re like, uh, this query is parameterized.

Everything’s fine. But like the first time you pass it in, it’s like, I’m going to go look for Eric. And it’s like, cool.

And VARCAR four. And then the next time you search for someone, you’re like, I’m going to go look for Bob. And then the print is like, my query is parameterized. This is great.

Uh, but Bob turns out as an VARCAR three. And so you get different copies of all the parameter lengths, uh, in the plan cache. That’s another one. Um, optional spids and temp tables is another one that I see. But, uh, you know, some, sometimes those are, those are a bit harder, harder to alleviate yourself of.

But, uh, when does it matter? Uh, you know, usually the plan cache is just such a jumble anyway. Um, hardly anyone notices.

Right. Uh, and you know, if, if plan cache builds, bloat is like your, your real, like your worst problem. Well, you got relatively easy fixes.

Um, unless you have, you know, you know, like 50 databases on the same server, all having to share the same plan cache. And it’s not an easy problem to solve. But, um, yeah, like it’s not, you know, it’s not like we get a plan cache per database.

I mean, technically we do with query store, but that’s not where SQL Server, that’s not what SQL Server uses for a plan cache, unfortunately. Um, so, you know, uh, when does it matter? Uh, hardly, hardly ever.

Um, you know, I think probably the, the, the person who it matters most to is the person who has to run analysis queries on the plan cache. It’s just like, it’s the same query over and over again. Ah, there’s 10 billion of them.

We can’t get rid of them. Right. Like they’re more hell on analysis scripts than peep. Uh, anyway, uh, why do delete sometimes block everything? Even when removing only a few rows.

Um, this is usually going to come down to, uh, one of three things, but generally I, well, yeah, I’d say generally that stems from a lack of indexing. Uh, you may, may not have a good index to support whatever the where clause of your delete is looking for. Uh, assuming that, you know, well, I mean, if you delete a few rows, assume there’s a where clause there that narrows things down.

Um, uh, that’s, you know, usually one. Um, if you have a foreign key on the table, um, then, you know, uh, if you don’t have good indexes to support the foreign keys, that might, uh, lead to some issues, especially big tables trying to latch onto each other. Um, let’s see what else we get.

Uh, cascading foreign keys would be another one. Those take serializable locks under the hood. Um, perhaps you have some index views going on. Um, if you have created columnstore indexes on your, uh, on your OLTP tables, perhaps.

A non clustered columnstore index to support some reporting queries. Uh, deleting from those and not a lot of fun. Mm.

Yeah. Don’t, don’t enjoy that. Uh, uh, yeah, that that’s, that’s, that’s pretty much the, the stuff that, um, I would, I would say that, that is a good enough. Oh, triggers, triggers, triggers, triggers, triggers.

Yeah. Delete a few rows. Who knows what all those triggers do, right? I have 15 audit triggers on this table. What are you auditing? Yeah.

I don’t know. Who’s looking at it? Got me. Why is it there? Hey. Oh, last, last guy did it. Like, okay. Well, yes, we’re here then. All right.

All right. That’s good enough. Thank you for, thank you for watching. I hope you enjoyed yourselves. I hope you learned something. I will see you in tomorrow’s video where, uh, I’m gonna say some unpleasant things. All right.

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 *