The Nolock vs Snapshot debugging trick
Max SQL Performance Blog
by Max Emanuelsson
1y ago
Have you ever tried to reproduce a bug in an application, and in the middle of your debugging process, you reeeeeally would have liked to be able to see the difference between some data from both before and after the bug occurred in order to compare data? Oh boy, have I just stumbled upon a great debugging trick for this... and Imma share it with ya! Now, to do this, you'll need to have RCSI (Read Committed Snapshot Isolation) enabled on your database. What RCSI does is that it enables optimistic locking. Long story short; RCSI causes every single update or delete to store the old data in Temp ..read more
Visit website
Option (recompile) can taint Temp Tables
Max SQL Performance Blog
by Max Emanuelsson
1y ago
Now what does that mean, exactly? Taint? Infect? Spread its disease? Option (recompile) isn't a bad thing. In the right circumstances, it can be a great tool! But I found something today that I absolutely did not know about and it was a devious trap that had caused some major performance issues... Let's get some backstory! Option (recompile) is used to not save a query plan in the query plan cache, forcing us to (re)compile the query each time it is run. This can be effective when we have a parameter in play that is causing a substantial flux in how the execution plan will look, depending on w ..read more
Visit website
Datatype implicit conversions will force index scans
Max SQL Performance Blog
by Max Emanuelsson
1y ago
So you've learned all there is to know about SQL Server and you are tuning queries left and right and you stumble upon a query that is not running very hot, no matter what you try... You add an index that you think should work, but the damn thing just keeps scanning the entire index instead of seeking into a specific row. You are even giving it a very specific value to seek on, but it just keeps telling you "Nope, screw you. I'm going to scan the entire index." What data type are you using? Because if you use a different data type when searching than the actual column is of, you might have a p ..read more
Visit website
Hidden index keys might serve more of a purpose than you know. But they shouldn't.
Max SQL Performance Blog
by Max Emanuelsson
1y ago
When you create a nonclustered index, like this... ...you get a perfectly formed index to retrieve the userid from the comments table. This index does, however, also include a hidden column. This hidden column links us back to the clustered index for key lookups. But if you read from this index, the index itself is now also sorted in the database on first userid, and then on id. So if the query you're trying to run looks like this... ...we'll get an execution plan that looks like this... Notice the lack of a sort operator. Now, further down the line, another developer, named Greg, performe ..read more
Visit website
How to make a query faster, using temp tables
Max SQL Performance Blog
by Max Emanuelsson
1y ago
Okay so temp tables don't actually make queries faster... however, temp tables can be used to let SQL Server maker better decisions when designing its query plans, which in turn can make your queries go faster! When you first execute a new query, SQL Server will run the query in several phases. Phase 1, it will look at the query you're trying to run and attempt to use statistics to build a query plan. It won't actually look into the data of the tables you're trying to access, but instead try to guess how many rows it will find at each operation and make decisions, based on those guesses. Phas ..read more
Visit website
The local variable, the cursor and the floor full of eggshells
Max SQL Performance Blog
by Max Emanuelsson
1y ago
Hi there! Been a while. Like "seven months a while" since my last post. But that's fine, because I've been working on this super complex query below: But then I ran into a bug at my daily work... and it was so weird... so very very weird... I had a process that would pick up a bunch of data and do stuff with it, but there was this one row that was never being picked up! I couldn't figure out why until I tried trouble shooting by eliminating all the other rows in my test database to make stuff as clean and easy to read as possible. So I just straight out flushed all other rows from existence ..read more
Visit website
My performance tuning process and the power of computed columns
Max SQL Performance Blog
by Max Emanuelsson
1y ago
This blog was never meant to go from point A to point Z in regards to difficulty or knowledge requirements. I just wanted to blog about the things I run into that could help people who already attempt to tune their queries. ...but then I figured that I could at least do a post about how MY process goes, when I attempt to performance tune something. So let's say your customers are complaining about this one query, and while you usually ignore them, as they are always complaining about something, this one catches your eye. They want to get the users that have ten thousand times more upvotes on t ..read more
Visit website
Temporal tables and the start time paradox
Max SQL Performance Blog
by Max Emanuelsson
1y ago
Once upon a time, I was sitting in my office, minding my own business, when this guy walked by, looking for someone, anyone who knows SQL Server. In a rush, he popped his head in and went: "You don't know anything about temporal tables, do you?" Me, who automatically heard "temporary tables", made a tired face and blatantly answered yes. It took me a minute of him explaining the issue he was having until I understood that temporal tables was actually a real thing, which I had never even heard about before. Saving face, I just nodded, gave very general responses and told him I could look into i ..read more
Visit website
"Not exists" is faster than "left outer join where is null"
Max SQL Performance Blog
by Max Emanuelsson
1y ago
In case you didn't know, if you want to retrieve rows from a table that doesn't have a connection to another table, you can write a left outer join, and then in the where clause, put "where table2.column is null"? Like this! Neat, right? A lot of people seem to be doing this. I don't know why. Doing this will make SQL Server read every single row from the left outer join table and then proceed to filter out the null values. That's bad. You know what's not bad? Not exists! ...and I guess "Not in" works fine too... Not exists will process the rows in a different way and end up reading a whoooo ..read more
Visit website

Follow Max SQL Performance Blog on FeedSpot

Continue with Google
Continue with Apple
OR