sql server query tuning best practices
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 PresentationTRANSCRIPT
SQL SERVER QUERY TUNING BEST PRACTICES
Aaron BertrandSQL Sentry, Senior Consultant@AaronBertrand
Kevin KlineSQL Sentry, Dir of Engineering Services@KEKline
NEW eBOOKAVAILABLE!
Kindle promo code and links to the video, slides, and codes will be emailed
to all attendees after the webcast.
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.
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
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
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.
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
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
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
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
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
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
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’
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
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