aaron bertrand sql sentry, senior consultant @aaronbertrand kevin kline sql sentry, dir of...
TRANSCRIPT
![Page 1: Aaron Bertrand SQL Sentry, Senior Consultant @AaronBertrand Kevin Kline SQL Sentry, Dir of Engineering Services @KEKline](https://reader035.vdocuments.mx/reader035/viewer/2022062404/551b66df550346d6338b48ec/html5/thumbnails/1.jpg)
SQL SERVER QUERY TUNING BEST PRACTICES, PART 6 OF 6
Aaron BertrandSQL Sentry, Senior Consultant@AaronBertrand
Kevin KlineSQL Sentry, Dir of Engineering Services@KEKline
![Page 2: Aaron Bertrand SQL Sentry, Senior Consultant @AaronBertrand Kevin Kline SQL Sentry, Dir of Engineering Services @KEKline](https://reader035.vdocuments.mx/reader035/viewer/2022062404/551b66df550346d6338b48ec/html5/thumbnails/2.jpg)
NEW eBOOKAVAILABLE!
Check http://SQLSentry.TV for links to the video, slides, and demo code
starting August 1st.
![Page 3: Aaron Bertrand SQL Sentry, Senior Consultant @AaronBertrand Kevin Kline SQL Sentry, Dir of Engineering Services @KEKline](https://reader035.vdocuments.mx/reader035/viewer/2022062404/551b66df550346d6338b48ec/html5/thumbnails/3.jpg)
Drawing on July 31st 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.
![Page 4: Aaron Bertrand SQL Sentry, Senior Consultant @AaronBertrand Kevin Kline SQL Sentry, Dir of Engineering Services @KEKline](https://reader035.vdocuments.mx/reader035/viewer/2022062404/551b66df550346d6338b48ec/html5/thumbnails/4.jpg)
AGENDA• 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
![Page 5: Aaron Bertrand SQL Sentry, Senior Consultant @AaronBertrand Kevin Kline SQL Sentry, Dir of Engineering Services @KEKline](https://reader035.vdocuments.mx/reader035/viewer/2022062404/551b66df550346d6338b48ec/html5/thumbnails/5.jpg)
SQL PATTERNS AND ANTI-PATTERNS13. Specifying the schema14. SP_xyz Prefix15. Unwanted recompiles16. The “Kitchen Sink” stored procedure17. SP_ExecuteSQL vs EXEC(…)18. Execution Plan Self-Destruct19. To MERGE or not to MERGE20. Efficient ad hoc sets 21. Implicit conversions 22. Best practices for comma-delimited
parameters 23. Best practices in temporary structures
1. Bad, Naughty Default Cursors2. Correlated Subqueries3. WHERE IN versus WHERE EXISTS4. UNION versus UNION ALL5. WHERE {NOT IN | EXISTS} versus
LEFT JOIN6. Queries optimized for SELECT but not DML
statements7. Compound index columns8. Covering indexes9. The Transitive Property10. Queries with IN (…)or OR 11. Queries with wildcard searches12. Using functions in WHERE or JOIN clauses
![Page 6: Aaron Bertrand SQL Sentry, Senior Consultant @AaronBertrand Kevin Kline SQL Sentry, Dir of Engineering Services @KEKline](https://reader035.vdocuments.mx/reader035/viewer/2022062404/551b66df550346d6338b48ec/html5/thumbnails/6.jpg)
GENERATE AD-HOC SETS EFFICIENTLY
• Example: a set of days in a range• Recursive CTEs can do this, but…• Create a permanent number/calendar table instead
• DEMO
![Page 7: Aaron Bertrand SQL Sentry, Senior Consultant @AaronBertrand Kevin Kline SQL Sentry, Dir of Engineering Services @KEKline](https://reader035.vdocuments.mx/reader035/viewer/2022062404/551b66df550346d6338b48ec/html5/thumbnails/7.jpg)
IMPLICIT CONVERSIONS• 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 http://bit.ly/15bDRRA. • Data type precedence call also have an impact: http://
bit.ly/13Zio1f.
• DEMO
![Page 8: Aaron Bertrand SQL Sentry, Senior Consultant @AaronBertrand Kevin Kline SQL Sentry, Dir of Engineering Services @KEKline](https://reader035.vdocuments.mx/reader035/viewer/2022062404/551b66df550346d6338b48ec/html5/thumbnails/8.jpg)
IMPLICIT CONVERSION RESOURCES
• Ian Stirk’s Column Mismatch Utility at http://www.sqlservercentral.com/articles/Administration/65138/.
• Jonathan Kehayias’ plan cache analyzer at http://sqlblog.com/blogs/jonathan_kehayias/archive/2010/01/08/finding-implicit-column-conversions-in-the-plan-cache.aspx.
• Jonathan Kehayias’ index scan study at http://www.sqlskills.com/blogs/jonathan/implicit-conversions-that-cause-index-scans/
![Page 9: Aaron Bertrand SQL Sentry, Senior Consultant @AaronBertrand Kevin Kline SQL Sentry, Dir of Engineering Services @KEKline](https://reader035.vdocuments.mx/reader035/viewer/2022062404/551b66df550346d6338b48ec/html5/thumbnails/9.jpg)
COMMA-DELIMITED PARAMETERS
• Example: pass a comma-separated list of OrderIDs• String splitting is expensive, even using CLR• Table-valued parameters are typically a better approach
• DEMO
![Page 10: Aaron Bertrand SQL Sentry, Senior Consultant @AaronBertrand Kevin Kline SQL Sentry, Dir of Engineering Services @KEKline](https://reader035.vdocuments.mx/reader035/viewer/2022062404/551b66df550346d6338b48ec/html5/thumbnails/10.jpg)
TEMPORARY STRUCTURES• Which are better, temp tables or temp variables?
Temp Table Temp Variable
Stored in? Tempdb Tempdb
Statistics? Yes No (1 row)
Indexs/Keys? Yes 1 UK / PK only
Truncate? Yes No
Recompiles? Yes No
Parallelism? Yes No
Metadata Overhead? Low Lowest
Lock Overhead? Normal Lowest
![Page 11: Aaron Bertrand SQL Sentry, Senior Consultant @AaronBertrand Kevin Kline SQL Sentry, Dir of Engineering Services @KEKline](https://reader035.vdocuments.mx/reader035/viewer/2022062404/551b66df550346d6338b48ec/html5/thumbnails/11.jpg)
SUMMARY• 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.
![Page 12: Aaron Bertrand SQL Sentry, Senior Consultant @AaronBertrand Kevin Kline SQL Sentry, Dir of Engineering Services @KEKline](https://reader035.vdocuments.mx/reader035/viewer/2022062404/551b66df550346d6338b48ec/html5/thumbnails/12.jpg)
FOLLOW UP1. Engage with our community: SQL Sentry on
Facebook, SQLSentry.Net, SQLPerformance.com
2. Share your tough query problems with us: http://answers.sqlperformance.com
3. Download SQL Sentry Plan Explorer for free: http://www.sqlsentry.net/plan-explorer/
4. Check out our other award winning tools: http://www.sqlsentry.net/download