Download presentation
Presentation is loading. Please wait.
Published byDevante Glidewell Modified over 10 years ago
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 Specifying the schema o SP_xyz Prefix o Queries with IN (…) / OR o Unwanted recompiles o Transitive property of indexes Prizes! Follow Up
5
13.Specifying the schema 14.SP_xyz Prefix 15.Unwanted recompiles 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
Always - when creating, altering, referencing objects Even if today everything is dbo o Object resolution works harder o Can yield multiple cached plans for the same query o DEMO
7
dbo stuff dbo.test Aaron stuff Aaron.test select * from test dbo.sptest (Aaron) Exec sptest
8
Stored procedures with the SP_ prefix can: o Cause metadata overhead o Induce needless SP:CacheMiss events About 10% performance hit (duration) in my tests Blog post: http://sqlperformance.com/sp_prefix
10
Meaning: o column IN (a,b,c) o column = a OR column = b OR column = c These optimize to the exact same plan IN is my personal preference (brevity) Do *not* replace with UNION or UNION ALL Can use TVPs to replace CSV/XML or dynamic SQL DEMO
11
Execution Read from system table NO In Memory? compile optimize Execute YES ReComp Execute
12
Expected: Because we request it: oCREATE PROC … WITH RECOMPILE or EXEC myproc … WITH RECOMPILE oSP_RECOMPILE foo Expected: Plan was aged out of memory Unexpected: Interleaved DDL and DML Unexpected: Big changes since last execution: o Schema changes to objects in underlying code o New/updated index statistics o Sp_configure
13
CREATE PROC testddldml AS … ; CREATE TABLE #testdml;-- (DDL) INSERT INTO #testdml;-- (DML + RECOMPILE) ALTER TABLE #testdml;-- (DDL) INSERT INTO #testdml;-- (DML + RECOMPILE) DROP TABLE #testdml;-- (DDL)
14
Schema changes: o Column additions, deletions o Data type changes o Constraint additions, deletions o Rule/Default bindings Index used by query is dropped
15
SQL Server recompiles to code to take advantage of new statistics for both manually and automatically created statistics: o Auto_update statistics o Auto_create statistics o Update statistics
16
In algebra: o when A = B and B = C, then … o A = C ! Some older versions of SQL Server do not know this. Incorporate the transitive property into JOIN and WHERE subclauses when appropriate: oSELECT … FROM table1 AS t1 oJOIN table2 AS t2 ON t2.my_id = t1.my_id oJOIN table3 AS t3 ON t3.my_id = t1.my_id AND t3.my_id = t2.my_id
17
Specify the schema, even if you only have dbo. Don’t use the SP_xyz Prefix. Understand queries with IN (…) / OR clauses. Remember unwanted recompiles. Don’t expect SQL Server to know the transitive property of indexes.
18
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.