sql server query tuning best practices, part 4 of 6

18
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

Upload: isolde

Post on 24-Feb-2016

26 views

Category:

Documents


0 download

DESCRIPTION

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. New eBOOK Available!. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: SQL Server Query Tuning Best  Practices, Part 4 of 6

SQL SERVER QUERY TUNING BEST PRACTICES, PART 4 OF 6

Aaron BertrandSQL Sentry, Senior Consultant@AaronBertrand

Kevin KlineSQL Sentry, Dir of Engineering Services@KEKline

Page 2: SQL Server Query Tuning Best  Practices, Part 4 of 6

NEW eBOOKAVAILABLE!

Check http://SQLSentry.TV for links to the video, slides, and demo code

starting August 1st.

Page 3: SQL Server Query Tuning Best  Practices, Part 4 of 6

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.

Page 4: SQL Server Query Tuning Best  Practices, Part 4 of 6

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

Page 5: SQL Server Query Tuning Best  Practices, Part 4 of 6

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

Page 6: SQL Server Query Tuning Best  Practices, Part 4 of 6

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

Page 7: SQL Server Query Tuning Best  Practices, Part 4 of 6

BONUS REASON: SECURITY

dbo stuff

dbo.test

Aaron stuff

Aaron.test

select * from test

dbo.sptest

(Aaron) Exec sptest

Page 8: SQL Server Query Tuning Best  Practices, Part 4 of 6

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

Page 9: SQL Server Query Tuning Best  Practices, Part 4 of 6
Page 10: SQL Server Query Tuning Best  Practices, Part 4 of 6

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

Page 11: SQL Server Query Tuning Best  Practices, Part 4 of 6

UNWANTED RECOMPILESExecution Read from

system tableNOIn Memory?

compileoptimize

Execute

YE

S ReComp

Execute

Page 12: SQL Server Query Tuning Best  Practices, Part 4 of 6

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

Page 13: SQL Server Query Tuning Best  Practices, Part 4 of 6

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>

Page 14: SQL Server Query Tuning Best  Practices, Part 4 of 6

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

Page 15: SQL Server Query Tuning Best  Practices, Part 4 of 6

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

Page 16: SQL Server Query Tuning Best  Practices, Part 4 of 6

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

Page 17: SQL Server Query Tuning Best  Practices, Part 4 of 6

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.

Page 18: SQL Server Query Tuning Best  Practices, Part 4 of 6

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