1 / 13

SQL Server Query Tuning Best Practices

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!.

kuniko
Download Presentation

SQL Server Query Tuning Best Practices

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. SQL Server Query Tuning Best Practices Aaron BertrandSQL Sentry, Senior Consultant@AaronBertrand Kevin KlineSQL Sentry, Dir of Engineering Services@KEKline

  2. New eBOOKAvailable! Kindle promo code and links to the video, slides, and codes will be emailed to all attendees after the webcast.

  3. 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.

  4. Agenda • Introductions • Don’t forget the test harness – Kevin • Quick tips on Assessment – Kevin • Patterns & Anti-Patterns – Aaron • UNION versus UNION ALL – Aaron • Index Structures – Kevin • Compound Indexes – Aaron • Covering Indexes – Kevin • Follow Up

  5. Test Environment • Your querytest harnessshould include some stuff. • Code to clear the caches: * • DBCC [FreeProcCache | FreeSystemCache | FlushProcInDB(<dbid>) ] • DBCC DropCleanBuffers • Code to set measurements: • SET STATISTICS TIME • SET STATISTICS IO • SET SHOWPLAN [TEXT | XML] • Code for Dynamic Management Views (DMV) checks. • System info – sys.dm_os_performance_counters and sys.dm_os_wait_stats

  6. Assessing the Findings • Red Flags Query Operators: • Lookups • Scans • Spools • Parallelism Operations • Red Flags Elsewhere: • Dissimilar estimated versus actual row counts • High physical reads • Missing index/statistics alarms • Large sort operations • Implicit data type conversions • Using live demo, we’ll show you patterns to use and anti-patterns to beware.

  7. Patterns and Anti-Patterns • Bad, Naughty Default Cursors • Correlated Subqueries • WHERE INversusWHERE 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

  8. UNION vs. UNION ALL • UNION ALL performs better than UNION because of an unpublicized side-effect • UNION eliminates duplicates by sorting • Can require worktable, additional sort operators • Can have effect on performance and tempdb • Can change semantics of query results DEMO

  9. Bonus Tip: SET NOCOUNT ON • SET NOCOUNT ON suppresses all DONE_IN_PROC messages until enabled for the session • Can add up to very big savings when included in any procedural code such as procedures, functions (UDFs), triggers, and batches DEMO

  10. Index Structure • 8K pages • Leaf pages ARE the data. • Non-leaf pages are pointers. Root Page Level 2 Intermediate Pages Level 1 Level 0 Leaf Pages

  11. Compound Indexes • Compound indexes are most useful from the leftmost column to the rightmost column, in the order they appeared in the CREATE INDEX statement. Example: CREATE NONCLUSTERED INDEX ndx_foo ON foo(a, b, c, d) • The following WHERE clauses will access the NDX_FOO: • WHERE a = @a • WHERE a = @a AND b = @b • The following WHERE clauses may access only part of NDX_FOO: • WHERE a = @a AND d = @d • WHERE a = @a AND c = @c AND b = @b • The following WHERE clauses will usually ignore NDX_FOO: • WHERE b = @b AND c = @c DEMO

  12. Covering Indexes • A covering index is one that answers a query entirely from its own intermediate pages, rather than going all the way down the tree to leaf pages. • All columns referenced in the query need to be in the index. • Don’t confuse a covering index with an included columns index DEMO

  13. Follow Up • Engage with our community: SQL Sentry on Facebook, SQLSentry.Net, SQLPerformance.com • Share your tough query problems with us: http://answers.sqlperformance.com • Download SQL Sentry Plan Explorer for free: http://www.sqlsentry.net/plan-explorer/ • Check out our other award winning tools: http://www.sqlsentry.net/download

More Related