Parameter Sniffing in SQL Server Stored Procedures

Slides:



Advertisements
Similar presentations
SQL Server performance tuning basics
Advertisements

Big Data Working with Terabytes in SQL Server Andrew Novick
Understanding Parameter Sniffing Benjamin Nevarez Blog: benjaminnevarez.com 1.
Module 8 Improving Performance through Nonclustered Indexes.
2 Avoiding Stored Procedure Recompiles Dr Greg Low Managing Director Solid Q Australia Session Code: DAT454.
Atlanta SQL Server Users Group April 10, 2006 Stored Procedure Best Practices Kevin Kline Director of Technology Quest Software.
Dexterity | CONFIDENTIAL 2009 MRO | Analytics | Insights 1 Stored Procedures.
Module 7 Reading SQL Server® 2008 R2 Execution Plans.
Module 9 Designing and Implementing Stored Procedures.
Ashwani Roy Understanding Graphical Execution Plans Level 200.
Module 5 Planning for SQL Server® 2008 R2 Indexing.
Module 8: Implementing Stored Procedures. Overview Implementing Stored Procedures Creating Parameterized Stored Procedures Working With Execution Plans.
Creating a dynamic search form with database paging Tony Rogerson SQL Server MVP Torver Computer Consultants.
Stored Procedure Optimization Preventing SP Time Out Delay Deadlocking More DiskReads By: Nix.
Meta Data Cardinality Explored CSSQLUG User Group - June 2009.
Virtual techdays INDIA │ august 2010 Filtered Indexes – The unexplored index … Vinod Kumar M │ Microsoft India Technology Evangelist – DB and BI.
Module 9: Using Advanced Techniques. Considerations for Querying Data Working with Data Types Cursors and Set-Based Queries Dynamic SQL Maintaining Query.
How to kill SQL Server Performance Håkan Winther.
SQL Server Statistics DEMO SQL Server Statistics SREENI JULAKANTI,MCTS.MCITP,MCP. SQL SERVER Database Administration.
SQL Server Statistics DEMO SQL Server Statistics SREENI JULAKANTI,MCTS.MCITP SQL SERVER Database Administration.
Dynamic SQL Writing Efficient Queries on the Fly ED POLLACK AUTOTASK CORPORATION DATABASE OPTIMIZATION ENGINEER.
Diving into Query Execution Plans ED POLLACK AUTOTASK CORPORATION DATABASE OPTIMIZATION ENGINEER.
SQL Server Magic Buttons! What are Trace Flags and why should I care? Steinar Andersen, SQL Service Nordic AB Thanks to Thomas Kejser for peer-reviewing.
SQL Server Performance Tuning
Tuning Transact-SQL Queries
Cleveland SQL Saturday Catch-All or Sometimes Queries
Query Optimization Techniques
Dynamic SQL Writing Efficient Queries on the Fly
Chapter 6 - Database Implementation and Use
Stored Procedures – Facts and Myths
Dynamic SQL: Writing Efficient Queries on the Fly
T-SQL Coding Techniques Are you playing with fire?
Parameter Sniffing in SQL Server Stored Procedures
Data Virtualization Demoette… Parameterized Queries
Dynamic SQL Writing Efficient Queries on the Fly
Database Performance Tuning and Query Optimization
Query Execution Expectation-Reality Denis Reznik
Chapter 15 QUERY EXECUTION.
Using Indexed Views & Computed Columns for Performance !
Now where does THAT estimate come from?
TSQL Coding Techniques
Query Optimization Techniques
Execution Plans Demystified
Statistics: What are they and How do I use them
Hidden gems of SQL Server 2016
Steve Hood SimpleSQLServer.com
Dynamic SQL: Writing Efficient Queries on the Fly
Hugo Kornelis Now where does THAT estimate come from? The nuts and bolts of cardinality estimation.
SQL Server Query Plans Journeyman and Beyond
Introduction To Structured Query Language (SQL)
Transact SQL Performance Tips
Ascending Key Problem in SQL Server Large Tables
Hidden Gems of SQL Server 2016
Parameter Sniffing: the Good, the Bad, and the Ugly
Query Processing CSD305 Advanced Databases.
Parameter Sniffing: the Good,the Bad, and the Ugly
Parameter Sniffing on SQL Server
Parameter Sniffing: the Good, the Bad, and the Ugly
Chapter 11 Database Performance Tuning and Query Optimization
Score a (row) goal and beat a query optimizer
Query Tuning Fundamentals
“Magic numbers”, local variable and performance
Dynamic Sql Not so scary?
Diving into Query Execution Plans
SQL Server Query Design and Optimization Recommendations
Query Optimization Techniques
Why should I care about SQL, if I have ORM?
Reinhard Flügel Possiblities and Limitations of System-Versioned Temporal Tables beyond the Basics.
Reinhard Flügel Possiblities and Limitations of System-Versioned Temporal Tables beyond the Basics.
Reinhard Flügel Possiblities and Limitations of System-Versioned Temporal Tables beyond the Basics.
Presentation transcript:

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

Thanks to our sponsors! Powered by

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

We are hiring! Java Developers .net Developers Application Engineers ++ See https://careers.gvc-plc.com/?

Introduction

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

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

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 = @student_id OR @student_id IS NULL) AND (exam_date = @exam_date OR @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

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!

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

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

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

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

Example 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 = @student_id OR @student_id IS NULL) AND (exam_date = @exam_date OR @exam_date IS NULL) ORDER BY exam_note DESC END

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

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

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

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

Results Execution time in milliseconds Logical Reads

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!

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

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

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

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

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

Solutions – Disable Parameter Sniffing 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 = @student_id OR @student_id IS NULL) AND (exam_date = @exam_date OR @exam_date IS NULL) ORDER BY exam_note DESC OPTION (OPTIMIZE FOR UNKNOWN) END

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

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

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

Results Execution time in milliseconds

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

Solutions – Choose Favorite Combinations 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 = @student_id OR @student_id IS NULL) AND (exam_date = @exam_date OR @exam_date IS NULL) ORDER BY exam_note DESC OPTION (OPTIMIZE FOR (@student_id = 1)) END

Results Execution time in milliseconds

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

Solutions – Disable Execution Plan Re-use 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 = @student_id OR @student_id IS NULL) AND (exam_date = @exam_date OR @exam_date IS NULL) ORDER BY exam_note DESC OPTION (RECOMPILE) END

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

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

Results Execution time in milliseconds

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

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

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

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

Results Execution time in milliseconds

Solutions – Decision Tree Dynamic ALTER PROCEDURE dbo.getExams @student_id INT = NULL, @exam_date DATETIME = NULL AS BEGIN DECLARE @sql nvarchar(600) = N'SELECT TOP (10) student_id, exam_number, exam_date, exam_note FROM dbo.StudentExams WHERE 1 = 1 ' IF @student_id IS NOT NULL SET @sql+=' AND student_id = @sid ' IF @exam_date IS NOT NULL SET @sql+=' AND exam_date = @ed ' SET @sql+=' ORDER BY exam_note DESC ' EXEC sp_executesql @sql, N'@sid INT, @ed DATETIME', @sid = @student_id, @ed = @exam_date; END

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

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

Results Execution time in milliseconds

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

Thanks to our sponsors! Powered by