Aaron Bertrand SQL Sentry, Senior Kevin Kline SQL Sentry, Dir of Engineering
Check for links to the video, slides, and demo code starting August 1st.
Drawing on July 31 st for a chance to win one of 3 Rookie Experience packages and 3 Ride Along packages from the Richard Petty Driving Experience at Charlotte Motor Speedway on October 18, 2013.
Introductions Patterns & Anti-Patterns o Efficient ad hoc sets (Aaron) o Best practices for comma-delimited parameters (Aaron) o Implicit Conversions (Kevin) o Best practices in temporary structures (Kevin) Follow Up
13.Specifying the schema 14.SP_xyz Prefix 15.Unwanted recompiles 16.The “Kitchen Sink” stored procedure 17.SP_ExecuteSQL vs EXEC(…) 18.Execution Plan Self-Destruct 19.To MERGE or not to MERGE 20.Efficient ad hoc sets 21.Implicit conversions 22.Best practices for comma-delimited parameters 23.Best practices in temporary structures 1.Bad, Naughty Default Cursors 2.Correlated Subqueries 3.WHERE IN versus WHERE EXISTS 4.UNION versus UNION ALL 5.WHERE {NOT IN | EXISTS} versus LEFT JOIN 6.Queries optimized for SELECT but not DML statements 7.Compound index columns 8.Covering indexes 9.The Transitive Property 10.Queries with IN (…) or OR 11.Queries with wildcard searches 12.Using functions in WHERE or JOIN clauses
Example: a set of days in a range Recursive CTEs can do this, but… Create a permanent number/calendar table instead DEMO
SQL Server has to do a lot of extra work / scans when conversion operations are assumed by the SQL programmer. Happens all the time with data types you’d think wouldn’t need it, e.g. between date types and character types. Very useful data type conversion chart at Data type precedence call also have an impact: DEMO
Ian Stirk’s Column Mismatch Utility at Jonathan Kehayias’ plan cache analyzer at implicit-column-conversions-in-the-plan-cache.aspx. implicit-column-conversions-in-the-plan-cache.aspx Jonathan Kehayias’ index scan study at index-scans/ index-scans/
Example: pass a comma-separated list of OrderIDs String splitting is expensive, even using CLR Table-valued parameters are typically a better approach DEMO
Which are better, temp tables or temp variables? Temp TableTemp Variable Stored in?Tempdb Statistics?YesNo (1 row) Indexs/Keys?Yes1 UK / PK only Truncate?YesNo Recompiles?YesNo Parallelism?YesNo Metadata Overhead?LowLowest Lock Overhead?NormalLowest
Comma-delimited parameters may be better with TVPs than splitting strings. Implicit conversions can cause extra CPU work and/or index and table scans. Efficient ad hoc sets may not be best in recursive CTEs. Test your alternatives. Temporary tables and temporary variable each have their uses. Know when to use each.
1.Engage with our community: SQL Sentry on Facebook, SQLSentry.Net, SQLPerformance.com 2.Share your tough query problems with us: Download SQL Sentry Plan Explorer for free: Check out our other award winning tools: