Presentation is loading. Please wait.

Presentation is loading. Please wait.

Aaron Bertrand SQL Sentry, Senior Kevin Kline SQL Sentry, Dir of Engineering

Similar presentations


Presentation on theme: "Aaron Bertrand SQL Sentry, Senior Kevin Kline SQL Sentry, Dir of Engineering"— Presentation transcript:

1 Aaron Bertrand SQL Sentry, Senior Consultant @AaronBertrand Kevin Kline SQL Sentry, Dir of Engineering Services @KEKline

2 Check http://SQLSentry.TV for links to the video, slides, and demo code starting August 1st.http://SQLSentry.TV

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

4 Introductions Patterns & Anti-Patterns o The “Kitchen Sink” stored procedure o SP_ExecuteSQL vs EXEC(…) o Execution Plan Self-Destruct o To MERGE or not to MERGE Prizes! Follow Up

5 13.Specifying the schema 14.SP_xyz Prefix 15.Unwanted recompiles 16.The “Kitchen Sink” stored procedure 17.SP_ExecuteSQL vs EXEC(…) 18.Execution Plan Self-Destruct 19.To MERGE or not to MERGE 1.Bad, Naughty Default Cursors 2.Correlated Subqueries 3.WHERE IN versus WHERE EXISTS 4.UNION versus UNION ALL 5.WHERE {NOT IN | EXISTS} versus LEFT JOIN 6.Queries optimized for SELECT but not DML statements 7.Compound index columns 8.Covering indexes 9.The Transitive Property 10.Queries with IN (…) or OR 11.Queries with wildcard searches 12.Using functions in WHERE or JOIN clauses

6 Many optional parameters to satisfy a variety of search conditions: o Dynamic SQL is often the best route here o Especially if Optimize For Ad Hoc Workloads is enabled o Could also use RECOMPILE, but that means you pay compile cost every time DEMO

7 Can promote better plan re-use Encourages strongly typed parameters instead of building up a massive string DEMO

8 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 settings o Contradictory collation settings, especially with Unicode o Contradictory SET options o Users with different default schemas not using schema prefix

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

10 1.SET statement in code 2.OLEDB/ODBC connection string 3.ODBC o Control Panel o SQLConfigDatasource 4.OLEDB/ODBC option auto set 5.Database Level Settings (Alter Database) 6.Server Wide Settings (sp_configure) SET Option Requirements: ONOFFSet-able By: Arithabort  S,D,A Concat_null_yields_null  S,D,A Quoted_Identifier**  S,D,A Ansi_nulls**  S,D,A Ansi_Padding  S,D,A Ansi_Warnings  S,D,A Numeric_roundabort  S,D,A ForceplanSET only Ansi_Null_Dflt_OnS,D,A Ansi_Null_Dflt_OffS,D,A  = required for index views or computed columns  = required for distributed / linked queries S, D, A = sp_configure, sp_dboption, Alter Database

11 Unnecessarily complex syntax Can complicate multi-operation trigger logic By default, does *NOT* promise concurrency protection or prevent race conditions o 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 http://bit.ly/AB-vs-MERGE

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

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


Download ppt "Aaron Bertrand SQL Sentry, Senior Kevin Kline SQL Sentry, Dir of Engineering"

Similar presentations


Ads by Google