Database Research & Development

  • Home
  • NoSQL
    • NoSQL
    • Cassandra
  • Databases
    • Database Theory
    • Database Designing
    • SQL Server Coding Standards
    • SQL Server
    • PostgreSQL
    • MySQL
    • Greenplum
    • Linux
  • Interviews
    • SQL Server Interviews
    • MySQL Interviews
    • SQL Puzzles
  • DBA Scripts
    • SQL Server DBA Scripts
    • PostgreSQL DBA Scripts
    • MySQL DBA Scripts
    • Greenplum DBA Scripts
  • Home
  • Blog Archives !
  • (: Laugh@dbrnd :)
  • Contact Me !
sqlserverinterviews
Home 2015 May SQL Server Parameter Sniffing

SQL Server Parameter Sniffing

This article is half-done without your Comment! *** Please share your thoughts via Comment ***

In this post, I am sharing about the Parameter Sniffing of SQL Server. I will discuss, why the query optimizer sometimes fails to choose a proper query plan.

One day I experienced that one of my production stored procedure was not returning expected performance, and before two days, same stored procedure was running fine.

At that time, I didn’t know about the Parameter Sniffing and started to look into this problem.

I found that before two days there were total 25000 records and later there were total 10,00,00 records.

I concluded that records increased quickly, but not increased ten times, and it increased just three times, and even I had already tested this stored procedure for 10,00,00,00 records.

Now time to know about the Parameter Sniffing:

Many times query optimizer chooses old query plan for execution because it stores this plan into the cache for frequently running queries.

Now, what happened when old query generated table scan for the first set of records and later the same parameter is required to change for index scanning because records increased.

I check the query execution plan and found that in my situation query optimizer use old query plan instead of to create a new query plan.

The query optimizer was using an old query plan from the query cache.

Now, this is known as a Parameter Sniffing issue. 

For you guys, this is very important to know why this is happening.

The Query optimizer does not always choose the old query plan, but many times due to traffic or load, query optimizer chooses a plan from the query cache.

As a DBA, you should monitor all indexes and its usages to identify this kind of problem.

SQL Server Solution for Parameter Sniffing :

Three options for this solution:

  • OPTION 1: RECOMPILING
  • OPTION 2: OPTIMIZE FOR VALUE
  • OPTION 3: OPTIMIZE FOR UNKNOWN

OPTION 1 : RECOMPILING

RECOMPILING,  means forcing SQL Server to recompile stored procedure or statement at every execution. You can set recompile hint on stored procedure or statement.

Sample code for Stored procedure and statements.

1
2
3
4
5
6
7
8
/*Stored Procedure with Recomplie option*/
CREATE PROCEDURE dbo.usp_gettestdata @testid INT
WITH RECOMPILE
AS
 
/*Select statement with Recomplie option*/
SELECT * FROM dbo.tbl_testdata
OPTION (RECOMPILE);

OPTION 2 : OPTIMIZE FOR VALUE

As a DBA, you can find a particular set of values which require executing under the best execution plan so you can set OPTIMIZE for a hint for this value. But values and data rapidly change in the database system so this only for those values or data which are not changing frequently.

Sample statement with OPTIMIZE for Hint:

1
2
3
4
5
6
7
CREATE PROCEDURE dbo.usp_EmployeeData
@EmpID INT
AS
SELECT EmployeName, EmployeeDOB, EmployeeClass
FROM dbo.tbl_EmployeeDetails
WHERE EmpID= @EmpID
OPTION (OPTIMIZE FOR (@EmpID=26));

OPTION 3 : OPTIMIZE FOR VALUE

Basically, you can set UNKNOWN hint for the query optimizer to create a generic execution plan base on statistical data. This may be again generated parameter sniffing issue, but still, you can use this for infrequently running queries.

1
2
3
4
5
6
7
CREATE PROCEDURE dbo.usp_EmployeeData
@EmpID INT
AS
SELECT EmployeName, EmployeeDOB, EmployeeClass
FROM dbo.tbl_EmployeeDetails
WHERE EmpID= @EmpID
OPTION (OPTIMIZE FOR UNKNOWN);
May 31, 2015Anvesh Patel
Script to Enable and Disable the Default Trace in SQL ServerBest way to store IP Address into Database System
Comments: 5
  1. Yogesh Shind
    February 4, 2016 at 4:24 am

    I think there is one more way to avoid it. You can declare variable inside and assign the variable of SP to these variables

    • Anvesh Patel
      Anvesh Patel
      February 4, 2016 at 4:27 am

      Oky thanks Yogesh, I will check.

  2. amit bansal
    April 8, 2016 at 10:29 am

    Just happen to come across the post and gave a quick read. You are missing an important point that the optimizer sniffs the parameter value nd creates a plan optimized for that value, which gets reused subsequently. Good effort though…

    • Anvesh Patel
      Anvesh Patel
      April 8, 2016 at 2:15 pm

      Thanks Amit, for your input.

  3. Chris
    February 15, 2018 at 3:48 pm

    I recently had a stored procedure where ‘with recompile’ and ‘Optimize For’ did not work but using local (dummy) parameters did. Sort of SQL Server 2005 methodology that worked best in my situation on a SQL Server 2016 instance.

    Example:

    CREATE PROCEDURE dbo.usp_EmployeeData
    (
    @EmpID INT
    )
    AS

    DECLARE @localEmpID INT = @EmpID;

    SELECT EmployeName, EmployeeDOB, EmployeeClass
    FROM dbo.tbl_EmployeeDetails
    WHERE EmpID = @localEmpID

    GO

Anvesh Patel
Anvesh Patel

Database Engineer

May 31, 2015 SQL ServerAnvesh Patel, database research and development, OPTION FOR Hint, OPTION Recompile, Parameter Sniffing, SQL Server, SQL Server Administrator, SQL Server Performance Tunning, SQL Server Tips and Tricks
About Me!

I'm Anvesh Patel, a Database Engineer certified by Oracle and IBM. I'm working as a Database Architect, Database Optimizer, Database Administrator, Database Developer. Providing the best articles and solutions for different problems in the best manner through my blogs is my passion. I have more than six years of experience with various RDBMS products like MSSQL Server, PostgreSQL, MySQL, Greenplum and currently learning and doing research on BIGData and NoSQL technology. -- Hyderabad, India.

About DBRND !

dbrnd

This is a personal blog (www.dbrnd.com).

Any views or opinions represented in this blog are personal and belong solely to the blog owner and do not represent those of people, institutions or organizations that the owner may or may not be associated with in professional or personal capacity, unless explicitly stated.

Feel free to challenge me, disagree with me, or tell me I’m completely nuts in the comments section of each blog entry, but I reserve the right to delete any comment for any reason whatsoever (abusive, profane, rude, or anonymous comments) - so keep it polite.

The content of this website is protected by copyright. No portion of this website may be copied or replicated in any form without the written consent of the website owner.

Recent Comments !
  • Anvesh Patel { Sure will do... } – May 27, 12:43 PM
  • Anvesh Patel { Great... } – May 27, 12:41 PM
  • Anvesh Patel { Great... } – May 27, 12:39 PM
  • Anvesh Patel { Great... } – May 27, 12:36 PM
  • Anvesh Patel { Great... } – May 27, 12:28 PM
  • Anvesh Patel { Great... } – May 27, 12:27 PM
  • Anvesh Patel { Great... } – May 27, 12:16 PM
  • Older »
Follow Me !
  • facebook
  • linkedin
  • twitter
  • youtube
  • google
  • flickr
© 2015 – 2019 All rights reserved. Database Research & Development (dbrnd.com)
Posting....