sql server query tuning best practices

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

Upload: davis

Post on 14-Jan-2016

72 views

Category:

Documents


4 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!. We’re giving away - 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!

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• Introducing Your Speakers• A Query Tuning Methodology – Kevin• Measuring Query Performance – Kevin• Query Tuning Patterns & Anti-Patterns – Aaron• Follow Up• Prizes!

Page 5: SQL Server Query Tuning Best Practices

MEAT-HEAD-OLOGY

• Take initial measurements

Configure the test environment

• Take comparative measurements

Run the test

• Know what to look for.

Assess the findings

Page 6: SQL Server Query Tuning Best Practices

TEST ENVIRONMENT• 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.os_wait_statso Yeah, ok. But which query DMVs?

Page 7: SQL Server Query Tuning Best Practices

PERIODIC TABLE OF SQL SERVER DYNAMIC MANAGEMENT OBJECTS

Page 8: SQL Server Query Tuning Best Practices

DM_OS_WAIT_STATS• “The Waiting is the Hardest Part”.

• Bottlenecks.

• Caveats for Relying on Wait Stats.

• Querying methodology around “The” Wait Stats DMV.

Page 9: SQL Server Query Tuning Best Practices

DM_EXEC_QUERY_STATS

• Query Performance Information

• System Resource Consumption:o CPUo Memoryo IO

Page 10: SQL Server Query Tuning Best Practices

DM_DB_INDEX_USAGE_STATS

• Reads by scans, seeks, lookups for both system and user activity.

• Writes for both system and user activity.

• Returns results for all databases, indexes so refine with predicates.

Page 11: SQL Server Query Tuning Best Practices

DM_IO_VIRTUAL_FILE_STATS• IO activity breakdown for each SQL data and log file on the

instance.

• Provides file size information too.

• It’s a function (DMF) so you must pass those parameters in!

Page 12: SQL Server Query Tuning Best Practices

DM_EXEC_REQUESTS

• Active requests being serviced.

• What users are doing on your instance.

• Usually requires additional info from dm_exec_sessions and the SQLOS DMOs.

DEMO!

Page 13: 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 14: SQL Server Query Tuning Best Practices

PATTERNS AND ANTI-PATTERNS• WHERE IN versus WHERE EXISTS• UNION versus UNION ALL• WHERE {NOT IN | EXISTS} versus LEFT JOIN• Tuning for SELECT versus INSERT, UPDATE, and DELETE• Compound index columns• Covering indexes• The Transitive Property• Queries with IN (…) or OR • Queries with LIKE ‘%’• Functions and calculations in WHERE or JOIN

TEASE!

Page 15: SQL Server Query Tuning Best Practices

DEMOS: DEFAULT CURSORS• Cursors are usually unnecessary, but when they are, use the

right options• The defaults are heavy-handed and guaranteed to be slow• Blog post: http://bit.ly/AB-cursors

Page 16: SQL Server Query Tuning Best Practices

DEMOS: CORRELATED SUBQUERIES

• Coercing SQL Server to evaluate multiple times• Think about converting these to joins• Gives the optimizer a fighting chance

Page 17: SQL Server Query Tuning Best Practices

DEMOS: NOT IN• Dangerous if source column is NULLable• LEFT OUTER JOIN is not always a good alternative• NOT EXISTS and EXCEPT are better (but can behave

differently)• Blog post: http://bit.ly/AB-NOTIN

Page 18: SQL Server Query Tuning Best Practices

FOLLOW UP1. Engage with our community: SQL Sentry on

Facebook, SQLSentry.Net, SQLPerformance.com.

2. Share your tough SQL Server problems with us: http://answers.sqlperformance.net

3. Download SQL Sentry Plan Explorer for free: http://www.sqlsentry.net/plan-explorer/sql-server-query-view.asp

4. Check out our other award winning tools: http://www.sqlsentry.net/download