![Page 1: Natural Born Killers, Performance issues to avoid](https://reader035.vdocuments.mx/reader035/viewer/2022081401/559c47c91a28ab85218b4834/html5/thumbnails/1.jpg)
Natural Born Killers, performance issues to avoid
Richard Douglas
![Page 2: Natural Born Killers, Performance issues to avoid](https://reader035.vdocuments.mx/reader035/viewer/2022081401/559c47c91a28ab85218b4834/html5/thumbnails/2.jpg)
2 Natural Born Killers
Natural Born Killer
http://www.flickr.com/photos/merille/4747615138/sizes/z/in/photostream/
![Page 3: Natural Born Killers, Performance issues to avoid](https://reader035.vdocuments.mx/reader035/viewer/2022081401/559c47c91a28ab85218b4834/html5/thumbnails/3.jpg)
3 Natural Born Killers
![Page 4: Natural Born Killers, Performance issues to avoid](https://reader035.vdocuments.mx/reader035/viewer/2022081401/559c47c91a28ab85218b4834/html5/thumbnails/4.jpg)
4 Natural Born Killers
Source: http://cheezburger.com/View/5939764992
![Page 5: Natural Born Killers, Performance issues to avoid](https://reader035.vdocuments.mx/reader035/viewer/2022081401/559c47c91a28ab85218b4834/html5/thumbnails/5.jpg)
5 Natural Born Killers
Adhering to best practices?
http://www.flickr.com/photos/12693492@N04/1338123903/sizes/m/in/photostream/
![Page 6: Natural Born Killers, Performance issues to avoid](https://reader035.vdocuments.mx/reader035/viewer/2022081401/559c47c91a28ab85218b4834/html5/thumbnails/6.jpg)
6 Natural Born Killers
Your host
• Richard Douglas
• Systems Consultant
• SQL Server MCITPro
• Maidenhead SQL User Group Leader
• Blog: http://SQL.RichardDouglas.co.uk
• Twitter: @SQLRich
• Email: [email protected][email protected]
![Page 7: Natural Born Killers, Performance issues to avoid](https://reader035.vdocuments.mx/reader035/viewer/2022081401/559c47c91a28ab85218b4834/html5/thumbnails/7.jpg)
7 Natural Born Killers
Agenda
• Statistics
• Table Design
• Scalar UDF’s
• Indices
• Key lookups
• Sargability
• Table variables
• Parameter sniffing
![Page 8: Natural Born Killers, Performance issues to avoid](https://reader035.vdocuments.mx/reader035/viewer/2022081401/559c47c91a28ab85218b4834/html5/thumbnails/8.jpg)
8 Natural Born Killers
Statistics
• SQL uses a cost based optimizer
• Costs influenced by statistics
![Page 9: Natural Born Killers, Performance issues to avoid](https://reader035.vdocuments.mx/reader035/viewer/2022081401/559c47c91a28ab85218b4834/html5/thumbnails/9.jpg)
9 Natural Born Killers
Statistics
• Creating Statistics– Automatic
– Manual
– CREATE STATISTICS
– sp_CreateStats
• Updating Statistics
• Sp_UpdateStats
• UPDATE STATISTICS
Permanent Table Temporary Table
1st record 1st record
- 6 records
LT 500 recs, 500 changes LT 500 recs, 500 changes
GT 500 recs, 500 changes + 20%
GT 500 recs, 500 changes + 20%
![Page 10: Natural Born Killers, Performance issues to avoid](https://reader035.vdocuments.mx/reader035/viewer/2022081401/559c47c91a28ab85218b4834/html5/thumbnails/10.jpg)
10 Natural Born Killers
Table Design
http://www.flickr.com/photos/aresauburnphotos/2699269321/sizes/o/in/photost
ream/
![Page 11: Natural Born Killers, Performance issues to avoid](https://reader035.vdocuments.mx/reader035/viewer/2022081401/559c47c91a28ab85218b4834/html5/thumbnails/11.jpg)
11 Natural Born Killers
Example of bad design
![Page 12: Natural Born Killers, Performance issues to avoid](https://reader035.vdocuments.mx/reader035/viewer/2022081401/559c47c91a28ab85218b4834/html5/thumbnails/12.jpg)
12 Natural Born Killers
Example of a better design
![Page 13: Natural Born Killers, Performance issues to avoid](https://reader035.vdocuments.mx/reader035/viewer/2022081401/559c47c91a28ab85218b4834/html5/thumbnails/13.jpg)
13 Natural Born Killers
Table Design – Size comparison
![Page 14: Natural Born Killers, Performance issues to avoid](https://reader035.vdocuments.mx/reader035/viewer/2022081401/559c47c91a28ab85218b4834/html5/thumbnails/14.jpg)
14 Natural Born Killers
Good table design benefits
• Minimise the CPU overhead.
• Increase the number of records in the buffer cache.
• Reduce the amount of physical disk IO.
• Reduce the amount of network traffic.
• Reduce the data file size(s).
• Reduce the working size of the Transaction Log.
• Reduce Full/Diff /T-Log backup file size.Thereby increasing your ability to deliver your RTO.
• Keep transaction time to a minimum.
![Page 15: Natural Born Killers, Performance issues to avoid](https://reader035.vdocuments.mx/reader035/viewer/2022081401/559c47c91a28ab85218b4834/html5/thumbnails/15.jpg)
![Page 16: Natural Born Killers, Performance issues to avoid](https://reader035.vdocuments.mx/reader035/viewer/2022081401/559c47c91a28ab85218b4834/html5/thumbnails/16.jpg)
19 Natural Born Killers
Scalar User Defined Functions
• The Good– Re-Usable code
• The Bad– Runs once per record in record set
– They don’t take advantage of parallelism
– They use Nested Loop joins regardless
• The Ugly– So ugly it’s hidden from the query plan and IO statistics
![Page 17: Natural Born Killers, Performance issues to avoid](https://reader035.vdocuments.mx/reader035/viewer/2022081401/559c47c91a28ab85218b4834/html5/thumbnails/17.jpg)
20 Natural Born Killers
Scalar UDF Solutions and Alternatives
• If you have to use them:– View the actual IO usage in Profiler
• If you can replace them:– Look at Table Value Functions
– Look at CLR
![Page 18: Natural Born Killers, Performance issues to avoid](https://reader035.vdocuments.mx/reader035/viewer/2022081401/559c47c91a28ab85218b4834/html5/thumbnails/18.jpg)
21 Natural Born Killers
Indices
![Page 19: Natural Born Killers, Performance issues to avoid](https://reader035.vdocuments.mx/reader035/viewer/2022081401/559c47c91a28ab85218b4834/html5/thumbnails/19.jpg)
22 Natural Born Killers
It’s all about DWI knowledge:
Indexing strategies
D W Ia
t
a
o
r
k
l
o
a
d
n
t
e
r
n
a
l
s
![Page 20: Natural Born Killers, Performance issues to avoid](https://reader035.vdocuments.mx/reader035/viewer/2022081401/559c47c91a28ab85218b4834/html5/thumbnails/20.jpg)
23 Natural Born Killers
Bad example; Surname, Firstname, Middle Initial
Golden Rules for Clustered Indexes
• Narrow
• Static
• Progressive
• Unique
• Fixed width
• Not Null
Impacts nonclusteredindexes
Fragmentation
Space impact
This example will have 13 bytes of overhead alone;
• 4 byte uniquifier
• 2 byte variable offset + 6 bytes for variable length fields
• 1 byte for NULL values and NULL bitmap
![Page 21: Natural Born Killers, Performance issues to avoid](https://reader035.vdocuments.mx/reader035/viewer/2022081401/559c47c91a28ab85218b4834/html5/thumbnails/21.jpg)
24 Natural Born Killers
Golden Rules for NonClustered Indexes
• Have an optimal clustered key
• Not narrow
• Reduce unnecessary overhead
– Fixed Width
– Not null
• Consolidate
• Index foreign keys
![Page 22: Natural Born Killers, Performance issues to avoid](https://reader035.vdocuments.mx/reader035/viewer/2022081401/559c47c91a28ab85218b4834/html5/thumbnails/22.jpg)
25 Natural Born Killers
Key Lookups – The silent killer
![Page 23: Natural Born Killers, Performance issues to avoid](https://reader035.vdocuments.mx/reader035/viewer/2022081401/559c47c91a28ab85218b4834/html5/thumbnails/23.jpg)
26 Natural Born Killers
Sargability
• SARGable – “Search ARGument able”
• T-SQL functions around a predicate can break SARGability
• Some caveats to remember
![Page 24: Natural Born Killers, Performance issues to avoid](https://reader035.vdocuments.mx/reader035/viewer/2022081401/559c47c91a28ab85218b4834/html5/thumbnails/24.jpg)
27 Natural Born Killers
Table Variables Vs. Temp Tables
Table Variables
• Fast when used with small data sets.
• Have a limited scope
• Use less locking and logging resources than temp tables– http://sql.richarddouglas.co.uk/archive/2011/06/rollback-gotchas-
part-2-2.html
• Estimated statistics always show 1 record
• Cannot be altered after they are declared
• Mythbuster - MAY be memory only, this isn’t guaranteed
• Generally faster with smaller data sets
![Page 25: Natural Born Killers, Performance issues to avoid](https://reader035.vdocuments.mx/reader035/viewer/2022081401/559c47c91a28ab85218b4834/html5/thumbnails/25.jpg)
28 Natural Born Killers
Table Variables Vs. Temp Tables
Temp Tables
• Uses statistics– Stat population will cause recompiles (statement level in 2005 +)
– Can create better plans
• Wider scope
• Can be rolled back
• Can be altered after creation
• Generally faster with larger data sets
![Page 26: Natural Born Killers, Performance issues to avoid](https://reader035.vdocuments.mx/reader035/viewer/2022081401/559c47c91a28ab85218b4834/html5/thumbnails/26.jpg)
29 Natural Born Killers
Parameter Sniffing
• What is it?– It’s all a matter of statistics
• How do I know when it will affect me?– Monitor the IO and CPU
– This means baselining your environment
• What are my options?– Rewrite dynamic queries
– Query Hints:
– WITH RECOMPILE
– OPTIMIZE FOR
– Plan Guides
![Page 27: Natural Born Killers, Performance issues to avoid](https://reader035.vdocuments.mx/reader035/viewer/2022081401/559c47c91a28ab85218b4834/html5/thumbnails/27.jpg)
30 Natural Born Killers
Demos
![Page 28: Natural Born Killers, Performance issues to avoid](https://reader035.vdocuments.mx/reader035/viewer/2022081401/559c47c91a28ab85218b4834/html5/thumbnails/28.jpg)
31 Natural Born Killers
Summary
• Statistics
• Table Design
• Scalar UDF’s
• Indices
• Key lookups
• Sargability
• Table variables
• Parameter sniffing
![Page 29: Natural Born Killers, Performance issues to avoid](https://reader035.vdocuments.mx/reader035/viewer/2022081401/559c47c91a28ab85218b4834/html5/thumbnails/29.jpg)
32 Dell SoftwareUnderstanding Indices
Any questions?