SQL SERVER QUERY TUNING BEST PRACTICES, PART 4 OF 6 Aaron Bertrand SQL Sentry, Senior Consultant @AaronBertrand Kevin Kline SQL Sentry, Dir of Engineering Services @KEKline
Feb 24, 2016
SQL SERVER QUERY TUNING BEST PRACTICES, PART 4 OF 6
Aaron BertrandSQL Sentry, Senior Consultant@AaronBertrand
Kevin KlineSQL Sentry, Dir of Engineering Services@KEKline
NEW eBOOKAVAILABLE!
Check http://SQLSentry.TV for links to the video, slides, and demo code
starting August 1st.
Your 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.
AGENDA• Introductions• Patterns & Anti-Patterns
o Specifying the schemao SP_xyz Prefixo Queries with IN (…) / ORo Unwanted recompileso Transitive property of indexes
• Prizes!• Follow Up
SQL PATTERNS AND ANTI-PATTERNS13. Specifying the schema14. SP_xyz Prefix15. Unwanted recompiles
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
SPECIFYING THE SCHEMA• Always - when creating, altering, referencing objects• Even if today everything is dbo
o Object resolution works hardero Can yield multiple cached plans for the same queryo DEMO
BONUS REASON: SECURITY
dbo stuff
dbo.test
Aaron stuff
Aaron.test
select * from test
dbo.sptest
(Aaron) Exec sptest
THE DREADED SP_ PREFIX• Stored procedures with the SP_ prefix can:
o Cause metadata overheado Induce needless SP:CacheMiss events
• About 10% performance hit (duration) in my tests• Blog post: http://sqlperformance.com/sp_prefix
QUERIES WITH IN (…) / OR• Meaning:
o column IN (a,b,c)o column = a OR column = b OR column = c
• These optimize to the exact same plan• IN is my personal preference (brevity)• Do *not* replace with UNION or UNION ALL• Can use TVPs to replace CSV/XML or dynamic SQL• DEMO
UNWANTED RECOMPILESExecution Read from
system tableNOIn Memory?
compileoptimize
Execute
YE
S ReComp
Execute
CAUSES OF RECOMPILE• Expected: Because we request it:
o CREATE PROC … WITH RECOMPILE or EXEC myproc … WITH RECOMPILEo SP_RECOMPILE foo
• Expected: Plan was aged out of memory• Unexpected: Interleaved DDL and DML• Unexpected: Big changes since last execution:
o Schema changes to objects in underlying codeo New/updated index statisticso Sp_configure
INTERLEAVED DDL AND DML• CREATE PROC testddldml AS … ;• CREATE TABLE #testdml; -- (DDL)• <some T-SQL code here>• INSERT INTO #testdml; -- (DML + RECOMPILE)• <some T-SQL code here>• ALTER TABLE #testdml; -- (DDL)• <some T-SQL code here>• INSERT INTO #testdml; -- (DML + RECOMPILE)• <some T-SQL code here>• DROP TABLE #testdml; -- (DDL)• <some T-SQL code here>
SCHEMA CHANGES TO OBJECTS• Schema changes:
o Column additions, deletionso Data type changeso Constraint additions, deletionso Rule/Default bindings
• Index used by query is dropped
NEW/UPDATED INDEX STATISTICS• SQL Server recompiles to code to take advantage of
new statistics for both manually and automatically created statistics:o Auto_update statisticso Auto_create statisticso Update statistics
TRANSITIVE PROPERTY OF INDEXES• In algebra:
o when A = B and B = C, then …o A = C !
• Some older versions of SQL Server do not know this. • Incorporate the transitive property into JOIN and
WHERE subclauses when appropriate:o SELECT … FROM table1 AS t1o JOIN table2 AS t2 ON t2.my_id = t1.my_ido JOIN table3 AS t3 ON t3.my_id = t1.my_id
• AND t3.my_id = t2.my_id
SUMMARY• Specify the schema, even if you only have dbo.• Don’t use the SP_xyz Prefix.• Understand queries with IN (…) / OR clauses.• Remember unwanted recompiles.• Don’t expect SQL Server to know the transitive property
of indexes.
FOLLOW UP1. Engage with our community: SQL Sentry on
Facebook, SQLSentry.Net, SQLPerformance.com2. Share your tough query problems with us:
http://answers.sqlperformance.com3. 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