Parameter Sniffing (or Spoofing) in SQL Server

2018-12-31 19:11发布

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

8条回答
呛了眼睛熬了心
2楼-- · 2018-12-31 19:29

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.

查看更多
永恒的永恒
3楼-- · 2018-12-31 19:30

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楼-- · 2018-12-31 19:36

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.

查看更多
路过你的时光
5楼-- · 2018-12-31 19:43

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/

查看更多
旧人旧事旧时光
6楼-- · 2018-12-31 19:48

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

查看更多
看淡一切
7楼-- · 2018-12-31 19:49

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.

查看更多
登录 后发表回答