Parameter Sensitivity and Plan Reuse

Parameter sniffing, parameterization, recompilation, and cached-plan reuse.

62 associated posts60 primary posts

Performance Tuning

Inline Table Valued Functions: Parameter Snorting

You've probably heard about parameter sniffing
But there's an even more insidious menace out there: Parameter Snorting.

It goes beyond ordinary parameter sniffing, where SQL at least tried to come up with a good plan for something once upon a compile. In these cases, it just plain gives up and throws a garbage number at you. You've seen it happen countless times with Table Variables, Local Variables, non-SARGable queries, catch-all queries, and many more poorly thunked query patterns.

Read more about Inline Table Valued Functions: Parameter Snorting 21 comments — Join the discussion
Performance Tuning

Filtered Statistics Follow-up

During our pre-con in Seattle
A really sharp lady brought up using filtered statistics, and for a good reason. She has some big tables, and with just 200 histogram steps, you can miss out on a lot of information about data distribution when you have millions or billions of rows in a table. There's simply not enough room to describe it all accurately, even with a full scan of the stats.

Read more about Filtered Statistics Follow-up 5 comments — Join the discussion
Performance Tuning

Using Plan Guides to Remove OPTIMIZE FOR UNKNOWN Hints

Say you've got an application that has tons of OPTIMIZE FOR UNKNOWN hints in the T-SQL, and you're getting bad query plans.

We're going to use the same StackOverflow query (and the same index on Reputation) that I demoed in the post Why Is This Query Sometimes Fast and Sometimes Slow? This technique produces a query that will produce two different execution plans depending on the Reputation parameter.

Read more about Using Plan Guides to Remove OPTIMIZE FOR UNKNOWN Hints 7 comments — Join the discussion
Performance Tuning

Why Is This Query Sometimes Fast and Sometimes Slow?

You swear you didn't change anything, but all of a sudden the SQL Server is going doggone slow. What happened? Parameter sniffing might be the problem, and to explain it, let's see how it works. I'm going to use the StackOverflow database - particularly, the Users table that I demo in How to Think Like the…

Read more about Why Is This Query Sometimes Fast and Sometimes Slow? 11 comments — Join the discussion
Performance Tuning

How to Start Troubleshooting Parameter Sniffing Issues

When a query is sometimes fast and sometimes slow, for the same input parameters, and you swear nothing else in the environment is changing, that's often a case of parameter sniffing.

After SQL Server starts up and you run a stored procedure, SQL Server builds an execution plan for that proc based on the first set of parameters that are passed in.

Read more about How to Start Troubleshooting Parameter Sniffing Issues 17 comments — Join the discussion
Performance Tuning

Query Tuning Week: How to Start Troubleshooting a Slow Stored Procedure

When you need to find out why a stored procedure is running slow, here's the information to start gathering: Check to see if the plan is in the cache. Run sp_BlitzCache® and use several different @sort_order parameters - try cpu, reads, duration, executions. If you find it in your top 10 plans, you can view…

Read more about Query Tuning Week: How to Start Troubleshooting a Slow Stored Procedure 7 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

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
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