Query Optimization and Execution Plans

How SQL Server compiles, optimizes, and executes individual queries.

573 associated posts292 primary posts

Performance Tuning

Are SQL Server Functions Dragging Your Query Down?

In most coding languages, functions are often-used blocks of code that can be reused from multiple locations, leading to less code – and cleaner code. SQL Server also lets us create functions that can be used the same way. They are reusable blocks of code that can be called from multiple locations. So, if you need to format phone numbers a certain way, or parse for specific characters, you can do so using a function.

Read more about Are SQL Server Functions Dragging Your Query Down? 20 comments — Join the discussion

Is it Cheating to Solve A Performance Problem By Adding Memory?

We had a comment recently on Five Things That Fix Bad SQL Server Performance that got me thinking. The comment came from a frustrated system administrator, who wrote: Chucking resources at a problem is not solving that problem, it’s just temporarily masking the symptoms of that problem. Funnily enough, I've heard the exact same thing from…

Read more about Is it Cheating to Solve A Performance Problem By Adding Memory? 23 comments — Join the discussion
Performance Tuning

Comparing Estimated and Actual Execution Plans in SQL Server

Let’s say you want to have an addition built on your house. You contact a contractor, who comes to your house, looks at your lot, looks at what exists, and asks you questions about exactly what you want. He then gives you an estimate of the work – approximately how long he thinks it will take his crew, what supplies will be needed, and the cost of those supplies.

Read more about Comparing Estimated and Actual Execution Plans in SQL Server 17 comments — Join the discussion
Performance Tuning

Stabilizing Execution Plans: Plan Guides and NORECOMPUTE

Sometimes you end up in a good plan / bad plan situation: an important query runs just fine most of the time. The query is parameterized, a good execution plan gets re-used, everything is cool. But sometimes, a "bad plan" gets compiled and starts to be reused. This is "bad" parameter sniffing. "Bad plans" can come in a few…

Read more about Stabilizing Execution Plans: Plan Guides and NORECOMPUTE 16 comments — Join the discussion
Performance Tuning

Temp Tables vs Table Variables vs Memory Optimized Table Variables [Video]

Should you use temp tables or table variables in your code? Join Microsoft Certified Master Kendra Little to learn the pros and cons of each structure, and take a sneak peek at new Memory Optimized Table Variables in SQL Server 2014.

This video is a recording of a live webcast, so please excuse any audio and video imperfections. Want the scripts from the webcast? Scroll on down.

Read more about Temp Tables vs Table Variables vs Memory Optimized Table Variables [Video] 7 comments — Join the discussion
Performance Tuning

Why You’re Tuning Stored Procedures Wrong (the Problem with Local Variables)

There's an important rule for tuning stored procedures that's easy to forget: when you're testing queries from procedures in SQL Server Management Studio, execute it as a stored procedure, a temporary stored procedure, or using literal values.

Don't re-write the statemet you're tuning as an individual TSQL statement using local variables!
Where it goes wrong
Stored procedures usually have multiple queries in them. When you're tuning, you usually pick out the most problematic statement, maybe from the query cache), and tune that.

Read more about Why You’re Tuning Stored Procedures Wrong (the Problem with Local Variables) 20 comments — Join the discussion

Curious About the SQL Server 2014 Cardinality Estimator?

If you've been following the blogosphere, or heading to SQL Saturdays, you'll know that many folks are excited about the brand new SQL Server cardinality estimator (CE for short). As Kendra showed in her article The SQL 2014 Cardinality Estimator Eats Bad TSQL for Breakfast, the new CE provides dramatically improved query plans. What if…

Read more about Curious About the SQL Server 2014 Cardinality Estimator? 6 comments — Join the discussion

Collecting Detailed Performance Measurements with Extended Events

Analyzing a workload can be difficult. There are a number of tools on the market (both free and commercial). These tools universally reduce workload analysis to totals and averages - details and outliers are smeared together. I’m against using just averages to analyze workloads; averages and totals aren’t good enough, especially with the tools we…

Read more about Collecting Detailed Performance Measurements with Extended Events 22 comments — Join the discussion

Why Index Fragmentation and Bad Statistics Aren’t Always the Problem (Video)

Do you rely on index rebuilds to make queries run faster? Or do you always feel like statistics are "bad" and are the cause of your query problems? You're not alone-- it's easy to fall into the trap of always blaming fragmentation or statistics. Learn why these two tools aren't the answer to every problem…

Read more about Why Index Fragmentation and Bad Statistics Aren’t Always the Problem (Video) 1 comment — Join the discussion
Performance Tuning

Are Table Variables as Good as Temporary Tables in SQL 2014?

There's a couple of new features in SQL Server 2014 that provide options for how you work with temporary objects. Will inline index creation or memory optimized temporary tables forever change the way you code? Let's take a look! Inline Index Creation SQL Server 2014 brings us a TSQL improvement called "inline specification of CLUSTERED…

Read more about Are Table Variables as Good as Temporary Tables in SQL 2014? 31 comments — Join the discussion

Q: Can High MaxDOP make a query SLOWER?

Answer: Yep, sometimes it can.I used to think that higher degrees of parallelism followed a law of diminishing returns-- you could add more threads, but the benefits would taper off. But it's a bit more complicated than that. Microsoft's recommendation to be careful when setting maxdop to values over 8 is a warning worth heeding.Lowering maxdop…

Read more about Q: Can High MaxDOP make a query SLOWER? 15 comments — Join the discussion
Performance Tuning

RECOMPILE Hints and Execution Plan Caching

When you identify that parameter sniffing is a problem, you need to test whether implementing 'recompile' hints will help plan quality. (Not sure what parameter sniffing is? Learn from this blog post or this 50 minute free video.) You must decide: what hint or command do you use, and where do you put it? What trade-offs are you making when it comes to being able to performance tune your SQL Server in the future?

Read more about RECOMPILE Hints and Execution Plan Caching 35 comments — Join the discussion