The two faces of sql parameter sniffing

34
The Two Faces of SQL Parameter Sniffing (or Just the Other Face)

description

MS SQL parameter sniffing (or "Bind Variable Peeking" in Oracle) is designed for good. Following some common best practices developers create procedures, analyze actual execution plan, test performance with various sets of data and it is then that they deploy. How about when the same good procedure that used to run less than 100ms starts to timeout...in production? Then you may have become a victim of bad query plan due to parameter sniffing. This may sound as a sci-fi scenario but is rather a real life use case which anyone may encounter. Join this session to get practical ideas of how to identify the cause, avoid typical mistakes and perform a fix.

Transcript of The two faces of sql parameter sniffing

Page 1: The two faces of sql parameter sniffing

The Two Faces of SQL

Parameter Sniffing(or Just the Other Face)

Page 2: The two faces of sql parameter sniffing

About me

� Project Manager @

� 11 years professional experience

� MCPD .NET Web Development

[email protected]

� http://www.linkedin.com/in/ivelin

� Business Interests

� Web Development (ASP.NET, jQuery, AJAX)

� SOA, Integration

� GIS, Mapping

� SQL optimization

2 |

Page 3: The two faces of sql parameter sniffing

Agenda

� Execution plan cache

� Parameter sniffing

� Performance issues

� Diagnostics

� Solution options

� Other RDBMS

� Demos

Page 4: The two faces of sql parameter sniffing

Not all [animals] are created equal

Image source http://hateandanger.wordpress.com/2012/06/05/fair/

Page 5: The two faces of sql parameter sniffing

The Plan Cache

� Execution plan cache (SQL Server)

� Query plans

� Ad hoc queries

� Stored procedures and User functions

� Triggers

� Execution contexts (per user)

� Retrieve plan

� Retrieve: sys.dm_exec_query_plan (plan_handle)

Page 6: The two faces of sql parameter sniffing

Putting Query Plan in Cache

� Compilation is CPU intensive

� Plan evicted from cache when

� ALTER PROCEDURE

� sp_recompile

� UPDATE STATISTICS (w/o SQL 2012)

� DBCC FREEPROCCACHE

� Cache buffer full

� View cached plans

� sys.dm_exec_query_stats

Page 7: The two faces of sql parameter sniffing

Variable run-time values - unknown for optimizer

Page 8: The two faces of sql parameter sniffing

Query Plan Generation

� CREATE [PROCEDURE]

� Syntactic check

� No plan built at this point

� Execution

� Sniff parameters

� Analyze distribution statistics

� Create plan

Page 9: The two faces of sql parameter sniffing

Plan cache contains only estimated plans

Image source http://123child.com/images/mine/DSCN4763.JPG

Page 10: The two faces of sql parameter sniffing

Parameter Sniffing?

� Def: Optimiser looking at input values

� Estimates based on statistics

� Notes

� Applies to all versions of SQL Server

� Local variables get standard assumption

� Constants are not sniffed

Page 11: The two faces of sql parameter sniffing

Image source http://jtown67.deviantart.com/art/Jekyll-and-Hyde-352442994

Dr. Jekyll and Mr. Parameter Sniffing

Page 12: The two faces of sql parameter sniffing

When Parameter Sniffing Stinks

� Parameter sniffing itself is not a problem

� Until it is!

� Negatively affecting performance?

1. Explain your customers

2. Other options ?

Page 13: The two faces of sql parameter sniffing

Performance problems caused by parameter

sniffing are considered to be by design

Page 14: The two faces of sql parameter sniffing

Typical Cases

1. Sniffing inappropriate for query usage

� One group of calls very different from main bulk

� Plan good for execution not appropriate for next

� Index Seek + Key Lookup vs. Table Scan

2. No perfect index for the query

Not your DB design fault

� Reproducible with AdventureWorks DB

Page 15: The two faces of sql parameter sniffing

DEMO 1: Reproduce parameter sniffing

Image source http://netdna.webdesignerdepot.com/uploads/2012/12/[email protected]

Page 16: The two faces of sql parameter sniffing

� DO NOT schedule DBCC FREEPROCCACHE

� EXEC sp_recompile

If problem reoccurs: (most likely)

� Keep the slow plan to analyse it!

� Which statement is slow?

� Are there different query plans?

� What are the index definitions?

� What parameter values are sniffed

� Are distribution statistics up to date?

Gathering Information

Page 17: The two faces of sql parameter sniffing

Typical symptom is lots of logical reads

Page 18: The two faces of sql parameter sniffing

Cause and Symptoms

Cause: Cardinality Error

� Huge difference - estimated/actual rows

� Huge difference - resource cost for the same plan

Symptom: Logical Reads

� Monitor deviation from average

� Monitor high Min – Max fluctuations

Page 19: The two faces of sql parameter sniffing

DEMO 2: Gather information

Image source http://netdna.webdesignerdepot.com/uploads/2012/12/[email protected]

Page 20: The two faces of sql parameter sniffing

Solution 1 - Recompilation

Recompile SP on every execution

� OPTION (RECOMPILE)

� WITH RECOMPILE (Create/Execute)

� Pros

� The best query plan every time

� Cons

� CPU-intensive

� Not suitable for SP used frequently

� Plans not stored in cache (for debug)

Page 21: The two faces of sql parameter sniffing

Solution 2 – OPTIMIZE Query Hint

Use specified value when compiling the plan

� You know a value that produces good plan

� OPTION (OPTIMIZE FOR (@par=[Value]))

� OPTIMIZE FOR UNKNOWN (SQL 2008 +)

� Pros

� “Good Enough” plan

� Cons

� Not for frequent data distribution changes

Page 22: The two faces of sql parameter sniffing

Solution 3 – Local Variables

Copy SP parameter to local variable

� Pros

� Same as “OPTIMIZE FOR UNKNOWN”

� Works on every version of SQL server

� Cons

� Same as “OPTIMIZE FOR UNKNOWN”

Page 23: The two faces of sql parameter sniffing

Solution 4 – Trace Flag 4136

Average estimate instead of histogram stats

� DBCC TRACEON (4136[, -1])

� Pros

� Evens “highs” and “lows”

� Cons

� Disables sniffing at SQL instance level

� Affects all DBs

� No benefit if data is evenly distributed

Page 24: The two faces of sql parameter sniffing

Solution 5 – Plan Guides

Attach fixed query plan to queries

� Pros

� Optimize query w/o changes in SQL text

� 3rd party vendor SQL optimization

� Cons

� Only one guide enabled for object

� Modify object referred by plan guide causes error

Page 25: The two faces of sql parameter sniffing

DEMO 3: Fix parameter sniffing

Image source http://netdna.webdesignerdepot.com/uploads/2012/12/[email protected]

Page 26: The two faces of sql parameter sniffing

When it may not be Sniffing

SET Options

� Many are cache keys

� Causes different entries for one SP

ARITHABORT

� Terminates query on overflow / divide-by-zero

� SET ARITHABORT ON/OFF

� Defaults (OFF – ADO.NET, ON - SSMS)

� Always match ARITHABORT on troubleshoot

Page 27: The two faces of sql parameter sniffing

Poorly written SQL is always slow

Page 28: The two faces of sql parameter sniffing

Parameter Sniffing and Other RDMS

� Bind Variable Peeking

� Adaptive Cursor Sharing (11g) 2007

� Enables statement to use multiple plans

� Identify “bind-sensitive” cursor

� Promote to “bind-aware” cursor� Track expected/actual operator cardinalities

� Cache and use new plan

� Notes

� Enabled by default

� Does not apply for >14 bind variables

Page 29: The two faces of sql parameter sniffing

Microsoft will fix that… When pigs fly

Page 30: The two faces of sql parameter sniffing

http://connect.microsoft.com/SQLServer/feedback/details/377959/

Page 31: The two faces of sql parameter sniffing

The Two Faces (Summary)

Dr. Jekyll

� Parameter sniffing is not bad

� Useful for performance tuning

� Integral part of SQL server

� Learn how to diagnose

Mr. Hyde

� Until it is

� At great cost for server

� Oracle fixed it

� Apply non-ideal solutions

Page 32: The two faces of sql parameter sniffing

Uneven data distribution and cost cannot be

derived. It is the optimiser that failed

Image source http://blogs.urbancode.com/agile/maslows-hammer-the-curse-of-tool-blindness

Page 33: The two faces of sql parameter sniffing

Credits

� Milos Radivojevic

� DB developer, Consultant, Speaker

� http://sqlbits.com/Speakers/Milos_Radivojevic

� Erland Sommarskog

� SQL Server MVP

� Consultant

� http://www.sommarskog.se/

Page 34: The two faces of sql parameter sniffing

Sponsors