Parameter Sniffing in SQL Server Stored Procedures

Slides:



Advertisements
Similar presentations
DB glossary (focus on typical SQL RDBMS, not XQuery or SPARQL)
Advertisements

Big Data Working with Terabytes in SQL Server Andrew Novick
SQL Performance 2011/12 Joe Chang, SolidQ
Understanding Parameter Sniffing Benjamin Nevarez Blog: benjaminnevarez.com 1.
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 7-1 David M. Kroenke’s Chapter Seven: SQL for Database Construction and.
Module 17 Storing XML Data in SQL Server® 2008 R2.
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 7-1 David M. Kroenke’s Chapter Seven: SQL for Database Construction and.
Database Design for DNN Developers Sebastian Leupold.
Introduction to Databases Chapter 6: Understanding the SQL Language.
Sofia, Bulgaria | 9-10 October Using XQuery to Query and Manipulate XML Data Stephen Forte CTO, Corzen Inc Microsoft Regional Director NY/NJ (USA) Stephen.
Physical Database Design Chapter 6. Physical Design and implementation 1.Translate global logical data model for target DBMS  1.1Design base relations.
Ashwani Roy Understanding Graphical Execution Plans Level 200.
Dive into the Query Optimizer Dive into the Query Optimizer: Undocumented Insight Benjamin Nevarez Blog: benjaminnevarez.com
Indexes and Views Unit 7.
Stored Procedure Optimization Preventing SP Time Out Delay Deadlocking More DiskReads By: Nix.
DB Tuning : Chapter 10. Optimizer Center for E-Business Technology Seoul National University Seoul, Korea 이상근 Intelligent Database Systems Lab School of.
Meta Data Cardinality Explored CSSQLUG User Group - June 2009.
Oracle9i Developer: PL/SQL Programming Chapter 5 Functions.
TSQL Worst Practices Jacob Sebastian, SQL Server MVP
How to kill SQL Server Performance Håkan Winther.
Dynamic SQL Writing Efficient Queries on the Fly ED POLLACK AUTOTASK CORPORATION DATABASE OPTIMIZATION ENGINEER.
Data Integrity & Indexes / Session 1/ 1 of 37 Session 1 Module 1: Introduction to Data Integrity Module 2: Introduction to Indexes.
SQL Server Performance Tuning
Creating Database Objects
SQL Server Statistics and its relationship with Query Optimizer
Practical Database Design and Tuning
Parameter Sniffing in SQL Server Stored Procedures
Query Optimization Techniques
Dynamic SQL Writing Efficient Queries on the Fly
Indexes By Adrienne Watt.
Chapter 6 - Database Implementation and Use
Stored Procedures – Facts and Myths
UFC #1433 In-Memory tables 2014 vs 2016
T-SQL Coding Techniques Are you playing with fire?
Dynamic SQL Writing Efficient Queries on the Fly
Using local variable without initialization is an error.
Query Execution Expectation-Reality Denis Reznik
Chapter 15 QUERY EXECUTION.
Marcos Freccia Stop everything! Top T-SQL tricks to a developer
TSQL Coding Techniques
Query Optimization Techniques
Teaching slides Chapter 8.
JULIE McLAIN-HARPER LINKEDIN: JM HARPER
Execution Plans Demystified
Statistics: What are they and How do I use them
Transactions, Locking and Query Optimisation
SQL Server Query Plans Journeyman and Beyond
SQL Server Performance Tuning Nowadays
Transact SQL Performance Tips
Ascending Key Problem in SQL Server Large Tables
Parameter Sniffing: the Good, the Bad, and the Ugly
Introduction to Execution Plans
Parameter Sniffing: the Good,the Bad, and the Ugly
Chapter 7 Using SQL in Applications
Parameter Sniffing on SQL Server
Parameter Sniffing: the Good, the Bad, and the Ugly
Score a (row) goal and beat a query optimizer
Query Tuning Fundamentals
Database Management System
Are you following SQL Server Development Best Practices?
SQL Server Query Design and Optimization Recommendations
Introduction to Execution Plans
Sourav Mukherjee Are you following SQL Server Development Best Practices? March 30, 2019 Cincinnati.
Query Optimization Techniques
The Five Mistakes You are Probably Making with SQL Server
Creating Database Objects
T-SQL Basics: Coding for performance
Introduction to Execution Plans
Why should I care about SQL, if I have ORM?
The Ins and Outs of Indexes
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/?

Agenda Query Execution Functions in the WHERE Clause Data Type Conversion Local Variables APPLY vs. JOIN Database Constraints and Performance Other Tips 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

Query Execution Transact SQL is a declarative language SQL Server generates an execution plan (cost-based optimization) What can affect the query execution? Which tables are involved in the query Are there indexes available on the tables Cardinality estimation by reading table statistics How the query is written? 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

Functions in the WHERE Clause Tip: Avoid functions in the WHERE clause with table columns as arguments! An index will not be used at all or it will be used inefficiently 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

Data Type Conversion Tip: Choose the right data type! Otherwise: Conversion overhead String Conversion costs can be significant (a “non-Unicode” value will be converted to a “Unicode” value) Estimation problem (for instance with the LIKE Operator) 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

Local Variables Tip: Understand how local variables affect query execution When local variables are used the SQL Server optimizer cannot generate the optimal execution plan because the variable value is not known at the compile time Tip: Use OPTION (RECOMPILE) to get a good 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

Local Variables Tip: Do not underestimate discrepancy in the cardinality estimation! Otherwise you could see something like this: 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

Database Constraints Tip: Database Constrains help SQL Server to make a better execution plan. The main purpose of constraints is data integrity, but Unique Constraints, Check Constraints and Foreign Keys help SQL Server optimizer to make the right decision about query execution 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

Other Tips Tip: Use UNION ALL when you know that sets are not overlapped and when duplicates are allowed! Tip: IN vs. EXISTS! perform the same. Use what you are more comfortable with! The same tip for EXISTS vs. COUNT(*) 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