Understanding Parameter Sniffing Benjamin Nevarez Blog: benjaminnevarez.com 1.

Slides:



Advertisements
Similar presentations
SQL Server performance tuning basics
Advertisements

SQL Performance 2011/12 Joe Chang, SolidQ
Sponsored by: Professional Association for SQL Server Bad plan! Sit! Gail Shaw.
Dave Ballantyne Clear Sky SQL. ›Freelance Database Developer/Designer –Specializing in SQL Server for 15+ years ›SQLLunch –Lunchtime usergroup –London.
1 Tuning PL/SQL procedures using DBMS_PROFILER 20-August 2009 Tim Gorman Evergreen Database Technologies, Inc. Northern California Oracle.
IMS 4212: Application Architecture and Intro to Stored Procedures 1 Dr. Lawrence West, Management Dept., University of Central Florida
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 10 Database Performance Tuning and Query Optimization.
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.
Copyright © 2006 Quest Software Best Practices for Stored Procedures By Kevin Kline SQL Server MVP.
Module 9 Designing and Implementing Stored Procedures.
Dive into the Query Optimizer Dive into the Query Optimizer: Undocumented Insight Benjamin Nevarez Blog: benjaminnevarez.com
Module 8: Implementing Stored Procedures. Overview Implementing Stored Procedures Creating Parameterized Stored Procedures Working With Execution Plans.
By Shanna Epstein IS 257 September 16, Cnet.com Provides information, tools, and advice to help customers decide what to buy and how to get the.
10 | Programming with Transact-SQL Graeme Malcolm | Senior Content Developer, Microsoft Geoff Allix | Principal Technologist, Content Master.
How to Use Parameters Like a Pro …and Boost Performance Guy Glantser, CEO, Madeira.
Connect with life Nauzad Kapadia Quartz Systems
Stored Procedure Optimization Preventing SP Time Out Delay Deadlocking More DiskReads By: Nix.
DAT410 SQL Server 2005 Optimizing Procedural Code Kimberly L. Tripp President/Founder, SQLskills.com.
Meta Data Cardinality Explored CSSQLUG User Group - June 2009.
SQL Query Analyzer. Graphical tool that allows you to:  Create queries and other SQL scripts and execute them against SQL Server databases. (Query window)
Virtual techdays INDIA │ august 2010 Filtered Indexes – The unexplored index … Vinod Kumar M │ Microsoft India Technology Evangelist – DB and BI.
Maciej Pilecki Consultant Project Botticelli Ltd. DAT404.
Module 9: Using Advanced Techniques. Considerations for Querying Data Working with Data Types Cursors and Set-Based Queries Dynamic SQL Maintaining Query.
Copyright Sammamish Software Services All rights reserved. 1 Prog 140  SQL Server Performance Monitoring and Tuning.
Dave LinkedIn
Dynamic SQL Writing Efficient Queries on the Fly ED POLLACK AUTOTASK CORPORATION DATABASE OPTIMIZATION ENGINEER.
Improve query performance with the new SQL Server 2016 query store!! Michelle Gutzait Principal Consultant at
SQL Server Performance Tuning
Joe Sack, Principal Program Manager, Microsoft
Parameter Sniffing in SQL Server Stored Procedures
Query Optimization Techniques
Dynamic SQL Writing Efficient Queries on the Fly
Are You There, DBA? It’s Me, The App Developer.
Stored Procedures – Facts and Myths
10 | Programming with Transact-SQL
Parameter Sniffing in SQL Server Stored Procedures
Dynamic SQL Writing Efficient Queries on the Fly
Introduction to Execution Plans
Using Indexed Views & Computed Columns for Performance !
EXEC and sp_executesql An Ad Hoc Rally
Now where does THAT estimate come from?
Query Optimization Techniques
JULIE McLAIN-HARPER LINKEDIN: JM HARPER
Plan cache performance tuning
Steve Hood SimpleSQLServer.com
When query plans go wrong
Hugo Kornelis Now where does THAT estimate come from? The nuts and bolts of cardinality estimation.
How to Use Parameters Like a Pro
Parameter Sniffing: the Good, the Bad, and the Ugly
Parameter Sniffing: the Good,the Bad, and the Ugly
When I Use NOLOCK AND OTHER HINTS
Parameter Sniffing on SQL Server
Parameter Sniffing: the Good, the Bad, and the Ugly
Query Tuning Fundamentals
“Magic numbers”, local variable and performance
Why You Should Consider Implementing Indexed Views
SQL Server Query Design and Optimization Recommendations
Introduction to Execution Plans
Query Optimization Techniques
Introduction to Execution Plans
Why should I care about SQL, if I have ORM?
Performance Tuning for SQL Developers through Execution Plans
Performance Tuning for SQL Developers through Execution Plans
Why You Should Consider Implementing Indexed Views
Performance Tuning for SQL Developers through Execution Plans
Performance Tuning for SQL Developers through Execution Plans
Performance Tuning for SQL Developers through Execution Plans
Performance Tuning for SQL Developers through Execution Plans
Why You Should Consider Implementing Indexed Views
Presentation transcript:

Understanding Parameter Sniffing Benjamin Nevarez Blog: benjaminnevarez.com 1

About the Speaker Benjamin Nevarez Author of “Inside the SQL Server Query Optimizer” and “SQL Server 2014 Query Tuning & Optimization” Working with SQL Server for 15 years 2

Query Optimizer - purpose It analyzes a number of candidate execution plans for a given query, estimates the cost of each of these plans, and selects the plan with the lowest cost of the choices considered. Requires a lot of resources (mostly CPU, optimization time) 3

Procedure Cache - purpose Cache query plans and allow for their reuse Minimize compile/optimization time 4

Parameterization Parameterized queries: query plan can be reused many times even if the parameter value changes Query not explicitly parameterized: in most cases plan can only be reused with the exact parameter value 5

Parameterization 1.Explicit Parameterization: application is written to separate parameters from the query text sp_executesql, stored procedures ADO, OLE DB, and ODBC Implicit Parameterization: Application do not explicitly uses parameters Simple Parameterization Forced Parameterization 6

Explicit Parameterization Application is written to separate parameters from the query text CREATE PROCEDURE test int) AS SELECT * FROM Sales.SalesOrderDetail WHERE ProductID 7

Implicit Parameterization Application do not explicitly uses parameters Forced Parameterization Requires ALTER DATABASE … PARAMETERIZATION FORCED Simple Parameterization (autoparameterization) Very conservative policy 8

Parameterization Demo 9

Parameter Sniffing It is a very good thing: getting an execution plan tailored to the current parameters of a query naturally improves the performance of your applications. However, some performance problems can occasionally appear 10

Parameter Sniffing Given that the Query Optimizer can produce different execution plans for syntactically identical queries, depending on their parameters, caching and reusing only one of these plans may create a performance issue for alternative instances of this query which would benefit from a better plan 11

Parameter Sniffing Demo Using the statistics histogram Producing two distinct plans for the same query 12

Optimize for a typical parameter Most of the executions of a query use the same plan Avoid an ongoing optimization cost ALTER PROCEDURE test int) AS SELECT * FROM Sales.SalesOrderDetail WHERE ProductID OPTION (OPTIMIZE FOR = 897)) 13

Optimize for a typical parameter Demo 14

Optimize on every execution Best execution plan for every query You end up paying for the optimization cost ALTER PROCEDURE test int) AS SELECT * FROM Sales.SalesOrderDetail WHERE ProductID OPTION (RECOMPILE) 15

Optimize on every execution Demo 16

OPTIMIZE FOR UNKNOWN and Local Variables Disables parameter sniffing Query Optimizer uses the density information of the statistics object (instead of the histogram) Ignore parameters, uses the same plan 17

OPTIMIZE FOR UNKNOWN and Local Variables ALTER PROCEDURE test int) AS SELECT * FROM Sales.SalesOrderDetail WHERE ProductID OPTION (OPTIMIZE FOR UNKNOWN) 18

OPTIMIZE FOR UNKNOWN and Local Variables Demo OPTIMIZE FOR UNKNOWN Local Variables Using the statistics density 19

Query plan caching and various SET options Some SET options are plan-reuse-affecting ANSI_NULL_DFLT_OFF ANSI_NULL_DFLT_ON ANSI_NULLS ANSI_PADDING ANSI_WARNINGS ARITHABORT CONCAT_NULL_YIELDS_NULL DATEFIRST DATEFORMAT FORCEPLAN LANGUAGE NO_BROWSETABLE NUMERIC_ROUNDABORT QUOTED_IDENTIFIER 20

Query plan caching and various SET options Finding the bad plan is more complicated Some SET options are plan-reuse-affecting 21

Query plan caching and various SET options Demo Finding Plans with different SET options Finding plans using Profiler/SQL trace 22