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 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

6 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

7 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

8 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

9 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

10 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

11 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

12 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

13 Thanks to our sponsors! Powered by

14


Download ppt "Parameter Sniffing in SQL Server Stored Procedures"

Similar presentations


Ads by Google