sql server query tuning best practices

15
SQL SERVER QUERY TUNING BEST PRACTICES Aaron Bertrand SQL Sentry, Senior Consultant @AaronBertrand Kevin Kline SQL Sentry, Dir of Engineering Services @KEKline

Upload: anitra

Post on 24-Jan-2016

47 views

Category:

Documents


0 download

DESCRIPTION

SQL Server Query Tuning Best Practices. 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

SQL SERVER QUERY TUNING BEST PRACTICES

Aaron BertrandSQL Sentry, Senior Consultant@AaronBertrand

Kevin KlineSQL Sentry, Dir of Engineering Services@KEKline

Page 2: SQL Server Query Tuning Best Practices

NEW eBOOKAVAILABLE!

Kindle promo code and links to the video, slides, and codes will be emailed

to all attendees after the webcast.

Page 3: SQL Server Query Tuning Best Practices

We’re giving away

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

AGENDA• Introductions• Don’t forget the test harness – Kevin• Quick tips on assessment – Kevin • Patterns & Anti-Patterns – Aaron

o Queries optimized for SELECT statements but not DML statementso Using functions in WHERE and JOIN clauseso Queries with wildcard searches

• Prizes!• Follow Up

Page 5: SQL Server Query Tuning Best Practices

TEST HARNESS• Your query test harness should include some stuff.• Code to clear the caches: *

o DBCC [FreeProcCache | FreeSystemCache | FlushProcInDB(<dbid>) ]

o DBCC DropCleanBuffers

• Code to set measurements:o SET STATISTICS TIMEo SET STATISTICS IOo SET SHOWPLAN [TEXT | XML]

• Code for Dynamic Management Views (DMV) checks.o System info – sys.dm_os_performance_counters and

sys.dm_os_wait_stats

Page 6: SQL Server Query Tuning Best Practices

ASSESSING THE FINDINGS• Red Flags Query Operators:

o Lookupso Scanso Spoolso Parallelism Operations

• Red Flags Elsewhere:o Dissimilar estimated versus actual row countso High physical readso Missing statistics alarmso Large sort operationso Implicit data type conversions

• Using live demo, we’ll show you patterns to use and anti-patterns to beware.

Page 7: SQL Server Query Tuning Best Practices

PATTERNS AND ANTI-PATTERNS1. 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 8: SQL Server Query Tuning Best Practices

OPTIMIZING FOR SELECT VS. DML

• Big differences between a SELECT and a DML statement that effects the same rows.

• Shouldn’t blindly create every index the Tuning Advisor or execution plan tells you to.

• Blog post - http://bit.ly/AB-BlindIndex

Page 9: SQL Server Query Tuning Best Practices

READS & INDEX STRUCTURE• 8K pages• Leaf pages ARE the data.• Non-leaf pages are pointers.

Leaf Pages

Root Page

Level 0

Intermediate Pages

Level 1

Level 2

Page 10: SQL Server Query Tuning Best Practices

WRITES & INDEX STRUCTURE• Each change to the leaf pages requires all index

structures be updated.

Leaf Pages

Root Page

Level 0

Intermediate Pages

Level 1

Level 2

Page Split

DML

Actualplace-ment

Page 11: SQL Server Query Tuning Best Practices

WHAT’S THE USAGE OF AN INDEX?

• Easy to answer with two DMVs!o sys.dm_db_index_physical_statso sys.dm_db_index_usage_stats

Page 12: SQL Server Query Tuning Best Practices

FUNCTIONS IN THE WHERE AND JOIN CLAUSES

• Functions / calculations on columns of a WHERE or JOIN clause can make SQL Server ignore indexes:o WHERE qty * 12 > 10000

• Instead, move function / calculation to SARG:o WHERE qty > 10000/12

DEMO

Page 13: SQL Server Query Tuning Best Practices

WILDCARD SEARCHES USING ‘%’

• Queries that use the LIKE clause have two simple rules:o LIKE can use indexes if the pattern starts with a character

string, such as WHERE lname LIKE ‘w%’o LIKE cannot use an index if the pattern starts with a leading

wildcard, such as WHERE lname LIKE ‘%alton’

Page 14: SQL Server Query Tuning Best Practices

SUMMARY• Queries optimized for SELECT statements but

not DML statementso Know your whole workload before you create your indexes

• Using functions in WHERE and JOIN clauseso Negate the use of indexes

• Queries with wildcard searcheso Negate the use of indexeso Make an index less effective

Page 15: SQL Server Query Tuning Best Practices

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