View
38
Download
0
Category
Preview:
DESCRIPTION
SQL Server Query Tuning Best Practices, Part 5 of 6. Aaron Bertrand SQL Sentry, Senior Consultant @AaronBertrand. Kevin Kline SQL Sentry, Dir of Engineering Services @ KEKline. New eBOOK Available!. - PowerPoint PPT Presentation
Citation preview
SQL SERVER QUERY TUNING BEST PRACTICES, PART 5 OF 6
Aaron BertrandSQL Sentry, Senior Consultant@AaronBertrand
Kevin KlineSQL Sentry, Dir of Engineering Services@KEKline
NEW eBOOKAVAILABLE!
Check http://SQLSentry.TV for links to the video, slides, and demo code
starting August 1st.
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.
AGENDA• Introductions• Patterns & Anti-Patterns
o The “Kitchen Sink” stored procedureo SP_ExecuteSQL vs EXEC(…)o Execution Plan Self-Destructo To MERGE or not to MERGE
• Prizes!• Follow Up
SQL PATTERNS AND ANTI-PATTERNS13. Specifying the schema14. SP_xyz Prefix15. Unwanted recompiles16. The “Kitchen Sink” stored procedure17. SP_ExecuteSQL vs EXEC(…)18. Execution Plan Self-Destruct19. To MERGE or not to MERGE
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
THE "KITCHEN SINK" PROCEDURE• Many optional parameters to satisfy a variety of search
conditions:o Dynamic SQL is often the best route hereo Especially if Optimize For Ad Hoc Workloads is enabledo Could also use RECOMPILE, but that means you pay compile cost every time
• DEMO
SP_EXECUTESQL VS. EXEC(…)• Can promote better plan re-use• Encourages strongly typed parameters instead of
building up a massive string• DEMO
EXECUTION PLAN SELF-DESTRUCT• Execution plans are intended to be reused as much as
makes good sense.• There are a variety of hidden or inconspicuous reasons
that execution plans will self-destruct and never be reused:o Contradictory language settingso Contradictory collation settings, especially with Unicodeo Contradictory SET optionso Users with different default schemas not using schema prefix
CONTRADICTIONS• When SQL Server is faced with contradictory settings, it
will usually opt to generate a new execution plan:o If language settings are different on client and server, even at the Windows-level
settings.o If collation settings differ between servers, server and client, or between a
Transact-SQL batch and the objects they are operating upon. Especially problematic with Unicode.
SET OPTION HIERARCHY1. SET statement in code2. OLEDB/ODBC connection
string3. ODBC
o Control Panelo SQLConfigDatasource
4. OLEDB/ODBC option auto set 5. Database Level Settings (Alter
Database)6. Server Wide Settings
(sp_configure)
SET Option Requirements:
ON OFF Set-able By:
Arithabort s S,D,A
Concat_null_yields_null s S,D,A
Quoted_Identifier** s S,D,A
Ansi_nulls** sp S,D,A
Ansi_Padding s S,D,A
Ansi_Warnings sp S,D,A
Numeric_roundabort s S,D,A
Forceplan SET only
Ansi_Null_Dflt_On S,D,A
Ansi_Null_Dflt_Off S,D,A
s = required for index views or computed columnsp = required for distributed / linked queriesS, D, A = sp_configure, sp_dboption, Alter Database
TO MERGE OR NOT TO MERGE?• Unnecessarily complex syntax• Can complicate multi-operation trigger logic• By default, does *NOT* promise concurrency protection
or prevent race conditionso In the end it is not really any more efficient than separate statements
• Many unresolved bugs• More details at http://bit.ly/AB-vs-MERGE
SUMMARY• The “Kitchen Sink” stored procedure is bad. Specialize
your stored procedures• SP_ExecuteSQL vs EXEC(…) have different strengths
and weaknesses. Know when to use each.• Execution Plans can Self-Destruct due to hidden or
inconspicuous settings.• To MERGE or not to MERGE? It’s not as clear cut as
you might think.
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
Recommended