Jump to content

Lesson Learned #310: Parameter Sniffing


Recommended Posts

Guest Jose_Manuel_Jurado
Posted

When we are using parameterized queries with object such as stored proecdure and functions, the engine can take the value passed to a parameter and use it to compare with statistics (index or colum). This is known as parameter sniffing

 

 

 

Basically, running a stored procedure the first compilation the optimizer "sniff" the parameter value and compile the query with the distribution of the statistics of this value.

 

 

 

In many cases, parameter sniffing reduces time in compilation and perform much better the execution, but, sometimes a high variance in terms of data distribution will impact in the performance.

 

 

 

For example, if the first execution of a query use a parameter value where will return a small subset of data but in the next execution with a different value the subset of the data is large, we may not have the best execution plan. Because, the query was compiled with a ten of hundres of rows and the new parameter value may has millions.

 

 

 

In the following video you could find an example of this parameter sniffing and how to resolve it. Of course, we have a lot of documentation to fix it SQL Server: How to determine parameter sniffing problem and how to handle it - Microsoft Q&A and OPTIMIZE FOR UNKNOWN – a little known SQL Server 2008 feature - Microsoft Community Hub . In 2022 or Compatibility level for 2022 we have a new option for this - Parameter Sensitive Plan optimization - SQL Server | Microsoft Learn

 

 

 

Video

 

 

 

 

 

 

 

 

Enjoy!

 

Continue reading...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...