Parameter Sniffing (or Spoofing) in SQL Server

2018-12-31 19:37发布

问题:

A while ago I had a query that I ran quite a lot for one of my users. It was still being evolved and tweaked but eventually it stablised and ran quite quickly, so we created a stored procedure from it.

So far, so normal.

The stored procedure, though, was dog slow. No material difference between the query and the proc, but the speed change was massive.

[Background, we\'re running SQL Server 2005.]

A friendly local DBA (who no longer works here) took one look at the stored procedure and said \"parameter spoofing!\" (Edit: although it seems that it is possibly also known as \'parameter sniffing\', which might explain the paucity of Google hits when I tried to search it out.)

We abstracted some of the stored procedure to a second one, wrapped the call to this new inner proc into the pre-existing outer one, called the outer one and, hey presto, it was as quick as the original query.

So, what gives? Can someone explain parameter spoofing?

Bonus credit for

  • highlighting how to avoid it
  • suggesting how to recognise possible cause
  • discuss alternative strategies, e.g. stats, indices, keys, for mitigating the situation

回答1:

FYI - you need to be aware of something else when you\'re working with SQL 2005 and stored procs with parameters.

SQL Server will compile the stored proc\'s execution plan with the first parameter that\'s used. So if you run this:

usp_QueryMyDataByState \'Rhode Island\'

The execution plan will work best with a small state\'s data. But if someone turns around and runs:

usp_QueryMyDataByState \'Texas\'

The execution plan designed for Rhode-Island-sized data may not be as efficient with Texas-sized data. This can produce surprising results when the server is restarted, because the newly generated execution plan will be targeted at whatever parameter is used first - not necessarily the best one. The plan won\'t be recompiled until there\'s a big reason to do it, like if statistics are rebuilt.

This is where query plans come in, and SQL Server 2008 offers a lot of new features that help DBAs pin a particular query plan in place long-term no matter what parameters get called first.

My concern is that when you rebuilt your stored proc, you forced the execution plan to recompile. You called it with your favorite parameter, and then of course it was fast - but the problem may not have been the stored proc. It might have been that the stored proc was recompiled at some point with an unusual set of parameters and thus, an inefficient query plan. You might not have fixed anything, and you might face the same problem the next time the server restarts or the query plan gets recompiled.



回答2:

Yes, I think you mean parameter sniffing, which is a technique the SQL Server optimizer uses to try to figure out parameter values/ranges so it can choose the best execution plan for your query. In some instances SQL Server does a poor job at parameter sniffing & doesn\'t pick the best execution plan for the query.

I believe this blog article http://blogs.msdn.com/queryoptteam/archive/2006/03/31/565991.aspx has a good explanation.

It seems that the DBA in your example chose option #4 to move the query to another sproc to a separate procedural context.

You could have also used the with recompile on the original sproc or used the optimize for option on the parameter.



回答3:

A simple way to speed that up is to reassign the input parameters to local parameters in the very beginning of the sproc, e.g.

CREATE PROCEDURE uspParameterSniffingAvoidance
    @SniffedFormalParameter int
AS
BEGIN

    DECLARE @SniffAvoidingLocalParameter int
    SET @SniffAvoidingLocalParameter = @SniffedFormalParameter

    --Work w/ @SniffAvoidingLocalParameter in sproc body 
    -- ...


回答4:

In my experience, the best solution for parameter sniffing is \'Dynamic SQL\'. Two important things to note is that 1. you should use parameters in your dynamic sql query 2. you should use sp_executesql (and not sp_execute), which saves the execution plan for each parameter values



回答5:

Parameter sniffing is a technique SQL Server uses to optimize the query execution plan for a stored procedure. When you first call the stored procedure, SQL Server looks at the given parameter values of your call and decides which indices to use based on the parameter values.

So when the first call contains not very typical parameters, SQL Server might select and store a sub-optimal execution plan in regard to the following calls of the stored procedure.

You can work around this by either

  • using WITH RECOMPILE
  • copying the parameter values to local variables inside the stored procedure and using the locals in your queries.

I even heard that it\'s better to not use stored procedures at all but to send your queries directly to the server. I recently came across the same problem where I have no real solution yet. For some queries the copy to local vars helps getting back to the right execution plan, for some queries performance degrades with local vars.

I still have to do more research on how SQL Server caches and reuses (sub-optimal) execution plans.



回答6:

I had similar problem. My stored procedure\'s execution plan took 30-40 seconds. I tried using the SP Statements in query window and it took few ms to execute the same. Then I worked out declaring local variables within stored procedure and transferring the values of parameters to local variables. This made the SP execution very fast and now the same SP executes within few milliseconds instead of 30-40 seconds.



回答7:

Very simple and sort, Query optimizer use old query plan for frequently running queries. but actually the size of data is also increasing so at that time new optimized plan is require and still query optimizer using old plan of query. This is called Parameter Sniffing. I have also created detailed post on this. Please visit this url: http://www.dbrnd.com/2015/05/sql-server-parameter-sniffing/



回答8:

Changing your store procedure to execute as a batch should increase the speed.

Batch file select i.e.:

exec (\'select * from order where  order id =\'\'\'+ @ordersID\')

Instead of the normal stored procedure select:

select * from order where  order id = @ordersID

Just pass in the parameter as nvarchar and you should get quicker results.