Presentation is loading. Please wait.

Presentation is loading. Please wait.

Parameter Sniffing in SQL Server Stored Procedures

Similar presentations


Presentation on theme: "Parameter Sniffing in SQL Server Stored Procedures"— Presentation transcript:

1 Parameter Sniffing in SQL Server Stored Procedures
Miloš Radivojević Parameter Sniffing in SQL Server Stored Procedures

2 Thanks to our sponsors! Powered by

3 Miloš Radivojević Data Platform MVP
Principal Database Consultant, bwin GVC Vienna, Austria Co-Founder: SQL Pass Austria Conference Speaker, Book Author Contact: Twitter: @MilosSQL

4 We are hiring! Java Developers .net Developers Application Engineers
++ See

5 Introduction

6 Demo – Sample Table 10 M rows Indexes: student_id and exam_date

7 Demo- Requirements Requirements:
Two input parameters: Student Id and Order Date Both parameters are optional The result set should contain up to 10 rows sorted by Exam Note descending A typical example is a web search page with optional search criteria The result set can be very large, but also empty It is challenge to optimize the implementation for all search requests Too many search criteria sometimes – application design problem

8 Demo- Application ALTER PROCEDURE dbo.getExams @student_id INT = NULL,
@exam_date DATETIME = NULL AS BEGIN SELECT TOP (10) student_id, exam_number, exam_date, exam_note FROM dbo.StudentExams WHERE (student_id IS NULL) AND (exam_date IS NULL) ORDER BY exam_note DESC END A typical example is a web search page with optional search criteria The result set can be very large, but also empty It is challenge to optimize the implementation for all search requests Too many search criteria sometimes – application design problem

9 Solution You have to use SSMS!
A typical example is a web search page with optional search criteria The result set can be very large, but also empty It is challenge to optimize the implementation for all search requests Too many search criteria sometimes – application design problem You have to use SSMS!

10 Agenda What is Parameter Sniffing? When and Why It is a Problem? Symptoms of Parameter Sniffing Problem Solutions for The Parameter Sniffing Problem

11 Execution Plan The Query Optimizer considerations:
Table size Data distribution & Statistics Indexes Selectivity of columns in the filter clauses Values of submitted parameters for the first invocation Submitted parameter values often decide about cardinality By default it reuses an exec. plan

12 What is Parameter Sniffing?
During stored procedure compilation, the values from parameters are evaluated (sniffed) and used to create an execution plan Future executions will re-use this plan This is a behaviour, not a bug It is good for invocations with similar parameters It can significantly, sometimes dramatically degrade the performance for non-common parameters

13 When PS is a Problem? When several execution plans are possible
Stored procedures prone to parameter sniffing with parameters participating in range operators with optional parameters Not only limited to stored procedures Static parameterized queries Dynamic queries executed with sp_executesql

14 Example ALTER PROCEDURE INT = DATETIME = NULL AS BEGIN SELECT TOP (10) student_id, exam_number, exam_date, exam_note FROM dbo.StudentExams WHERE (student_id IS NULL) AND (exam_date IS NULL) ORDER BY exam_note DESC END

15 Example Execution time: 202 ms Logical reads: 17.580
EXEC dbo.getExams 2001, NULL; Execution time: 202 ms Logical reads:

16 Example Execution time: 7.335 ms Logical reads: 40.017.504
EXEC dbo.getExams NULL, ' '; Execution time: ms Logical reads:

17 Example Execution time: 180 ms Logical reads: 29.742
EXEC dbo.getExams NULL, ' '; Execution time: 180 ms Logical reads:

18 Example Execution time: 6.445 ms Logical reads: 40.647.499
EXEC dbo.getExams 2001, NULL; Execution time: ms Logical reads:

19 Results Execution time in milliseconds Logical Reads

20 Symptoms of Parameter Sniffing
What? Execution of a stored procedure within an application is suddenly slow Significantly increasing of I/O reads When and How? Yesterday it was OK, problems started one hour ago… For some parameter combinations it works well The same procedure in another application or in the SSMS works perfect!

21 SSMS Mistery It works instantly in the SSMS, but it takes 5 seconds in the application A new execution plan is created for the stored procedure invocation within SSMS! Factors that affect plan-reuse ANSI_NULLS ANSI_PADDING ANSI_WARNINGS ARITHABORT QUOTED_IDENTIFIER CONCAT_NULL_YIELDS_NULL DATEFORMAT A typical example is a web search page with optional search criteria The result set can be very large, but also empty It is challenge to optimize the implementation for all search requests Too many search criteria sometimes – application design problem

22 SSMS Mistery A typical example is a web search page with optional search criteria The result set can be very large, but also empty It is challenge to optimize the implementation for all search requests Too many search criteria sometimes – application design problem

23 PS Challenge – What To Do?
SQL Server Optimizer: Considers input parameters Reuses execution plans We can suggest to the Optimizer: Do not consider input parameters! Do not “blindly” reuse an execution plan! A typical example is a web search page with optional search criteria The result set can be very large, but also empty It is challenge to optimize the implementation for all search requests Too many search criteria sometimes – application design problem

24 Solutions – Disable Parameter Sniffing Effect
SQL Server uses average distribution statistics to choose an execution plan Neutrlaize parameter values – an average solution It does not work always! How to implement? Using the OPTIMIZE FOR UKNOWN query hint Wrap parameters in local variables Using TF 4136 A typical example is a web search page with optional search criteria The result set can be very large, but also empty It is challenge to optimize the implementation for all search requests Too many search criteria sometimes – application design problem

25 Solutions – Disable Parameter Sniffing Effect
SQL Server uses average distribution statistics to choose an execution plan Neutralize parameter values – an average solution It does not work always! How to implement? Using the OPTIMIZE FOR UKNOWN query hint Wrap parameters in local variables Using TF 4136 A typical example is a web search page with optional search criteria The result set can be very large, but also empty It is challenge to optimize the implementation for all search requests Too many search criteria sometimes – application design problem

26 Solutions – Disable Parameter Sniffing
ALTER PROCEDURE INT = DATETIME = NULL AS BEGIN SELECT TOP (10) student_id, exam_number, exam_date, exam_note FROM dbo.StudentExams WHERE (student_id IS NULL) AND (exam_date IS NULL) ORDER BY exam_note DESC OPTION (OPTIMIZE FOR UNKNOWN) END

27 Solutions – Disable Parameter Sniffing
ALTER PROCEDURE INT = DATETIME = NULL AS BEGIN DATETIME SELECT TOP (10) student_id, exam_number, exam_date, exam_note FROM dbo.StudentExams WHERE (student_id IS NULL) AND (exam_date IS NULL) ORDER BY exam_note DESC END

28 Example Execution time: 420 ms Logical reads: 681.550
EXEC dbo.getExams 2001, NULL; Execution time: 420 ms Logical reads:

29 Example Execution time: 505 ms Logical reads: 681.550
EXEC dbo.getExams NULL, ' '; Execution time: 505 ms Logical reads:

30 Results Execution time in milliseconds

31 Solutions – Choose Favorite Combination(s)
Goal: To work perfect for the most common or important combination(s) SQL Server generates an optimal execution plan for it and reuses it Need to contact business people How to implement? Using the OPTIMIZE FOR query hint Query Decomposition (IF) A typical example is a web search page with optional search criteria The result set can be very large, but also empty It is challenge to optimize the implementation for all search requests Too many search criteria sometimes – application design problem

32 Solutions – Choose Favorite Combinations
ALTER PROCEDURE INT = DATETIME = NULL AS BEGIN SELECT TOP (10) student_id, exam_number, exam_date, exam_note FROM dbo.StudentExams WHERE (student_id IS NULL) AND (exam_date IS NULL) ORDER BY exam_note DESC OPTION (OPTIMIZE FOR = 1)) END

33 Results Execution time in milliseconds

34 Solutions – Disable Execution Plan Re-use
Goal: Always get the optimal execution plan Pros: Optimal plan for each execution The plan can be better than the best plan in the initial solution!!! Cons: Compiled by each execution How to implement? Define SP with the option WITH RECOMPILE (not recommended!) OPTION (RECOMPILE) at the statement level A typical example is a web search page with optional search criteria The result set can be very large, but also empty It is challenge to optimize the implementation for all search requests Too many search criteria sometimes – application design problem

35 Solutions – Disable Execution Plan Re-use
ALTER PROCEDURE INT = DATETIME = NULL AS BEGIN SELECT TOP (10) student_id, exam_number, exam_date, exam_note FROM dbo.StudentExams WHERE (student_id IS NULL) AND (exam_date IS NULL) ORDER BY exam_note DESC OPTION (RECOMPILE) END

36 Example Execution time: 36 ms Logical reads: 79
EXEC dbo.getExams 2001, NULL; Execution time: 36 ms Logical reads: 79

37 Example Execution time: 37 ms Logical reads: 7.156
EXEC dbo.getExams NULL, ' '; Execution time: 37 ms Logical reads: 7.156

38 Results Execution time in milliseconds

39 Solutions – Static or Dynamic Query Decomposition
Goal: Always get the optimal execution plan and avoid recompilation Pros: Optimal plan for each execution Reuse The plan can be better than the best plan in the initial solution!!! Cons: Maintenance problems, SQL Injection... How to implement? Static SQL (Decission Tree Implementation) Dynamic SQL A typical example is a web search page with optional search criteria The result set can be very large, but also empty It is challenge to optimize the implementation for all search requests Too many search criteria sometimes – application design problem

40 Solutions – Decision Tree
ALTER PROCEDURE dbo.getExams @student_id INT = NULL, @exam_date DATETIME = NULL AS BEGIN IS NOT NULL SELECT TOP (10) student_id, exam_number, exam_date, exam_note FROM dbo.StudentExams WHERE student_id ORDER BY exam_note DESC ELSE IS NOT NULL WHERE exam_date END

41 Example Execution time: 36 ms Logical reads: 79
EXEC dbo.getExams 2001, NULL; Execution time: 36 ms Logical reads: 79

42 Example Execution time: 43 ms Logical reads: 7.036
EXEC dbo.getExams NULL, ' '; Execution time: 43 ms Logical reads: 7.036

43 Results Execution time in milliseconds

44 Solutions – Decision Tree Dynamic
ALTER PROCEDURE INT = DATETIME = NULL AS BEGIN nvarchar(600) = N'SELECT TOP (10) student_id, exam_number, exam_date, exam_note FROM dbo.StudentExams WHERE 1 = 1 ' IS NOT NULL AND student_id ' IS NOT NULL AND exam_date ' ORDER BY exam_note DESC ' EXEC = END

45 Example Execution time: 36 ms Logical reads: 79
EXEC dbo.getExams 2001, NULL; Execution time: 36 ms Logical reads: 79

46 Example Execution time: 37 ms Logical reads: 7.156
EXEC dbo.getExams NULL, ' '; Execution time: 37 ms Logical reads: 7.156

47 Results Execution time in milliseconds

48 Solutions – The Combined Solution
Goal: Always get the optimal execution plan and reuse it for most common parameters Pros: Optimal plan for most important executions Reuse The plan can be better than the best plan in the initial solution!!! How to implement? Static SQL Decission Tree Implementation combined with OPTION (RECOMPILE) A typical example is a web search page with optional search criteria The result set can be very large, but also empty It is challenge to optimize the implementation for all search requests Too many search criteria sometimes – application design problem

49 Thanks to our sponsors! Powered by

50


Download ppt "Parameter Sniffing in SQL Server Stored Procedures"

Similar presentations


Ads by Google