Natural Born Killers,
performance issues to avoid
Richard Douglas
Natural Born Killer
http://www.flickr.com/photos/merille/4747615138/sizes/z/in/photostream/
2
Natural Born Killers
3
Natural Born Killers
Source: http://cheezburger.com/View/5939764992
4
Natural Born Killers
Adhering to best practices?
http://www.flickr.com/photos/12693492@N04/1338123903/sizes/m/in/photostream/
5
Natural Born...
Your host
• Richard Douglas
• Systems Consultant
• SQL Server MCITPro
• Maidenhead SQL User Group Leader
• Blog: http://S...
Agenda
• Statistics
• Table Design
• Scalar UDF’s
• Indices
• Key lookups
• Sargability
• Table variables
• Parameter sn...
Statistics
• SQL uses a cost based optimizer
• Costs influenced by statistics
8
Natural Born Killers
Statistics
• Creating Statistics
– Automatic
– Manual
– CREATE STATISTICS
– sp_CreateStats
• Updating Statistics
Permanen...
Table Design
http://www.flickr.com/photos/aresauburnphotos/2699269321/sizes/o/in/photost
ream/
10
Natural Born Killers
Example of bad design
11
Natural Born Killers
Example of a better design
12
Natural Born Killers
Table Design – Size comparison
13
Natural Born Killers
Good table design benefits
• Minimise the CPU overhead.
• Increase the number of records in the buffer cache.
• Reduce the...
Scalar User Defined Functions
• The Good
– Re-Usable code
• The Bad
– Runs once per record in record set
– They don’t tak...
Scalar UDF Solutions and Alternatives
• If you have to use them:
– View the actual IO usage in Profiler
• If you can repl...
Indices
21
Natural Born Killers
Indexing strategies
It’s all about DWI knowledge:
D W I
a
t
a
22
Natural Born Killers
o
r
k
l
o
a
d
n
t
e
r
n
a
l
s
Golden Rules for Clustered Indexes
Bad example; Surname, Firstname, Middle Initial
• Static
Impacts nonclustered
indexes
...
Golden Rules for NonClustered Indexes
• Have an optimal clustered key
• Not narrow
• Reduce unnecessary overhead
– Fixed W...
Key Lookups – The silent killer
25
Natural Born Killers
Sargability
• SARGable – “Search ARGument able”
• T-SQL functions around a predicate can break
SARGability
• Some caveats ...
Table Variables Vs. Temp Tables
Table Variables
• Fast when used with small data sets.
• Have a limited scope
• Use less ...
Table Variables Vs. Temp Tables
Temp Tables
• Uses statistics
– Stat population will cause recompiles (statement level in ...
Parameter Sniffing
• What is it?
– It’s all a matter of statistics
• How do I know when it will affect me?
– Monitor the ...
Demos
30
Natural Born Killers
Summary
• Statistics
• Table Design
• Scalar UDF’s
• Indices
• Key lookups
• Sargability
• Table variables
• Parameter sni...
Any questions?
32
Understanding Indices
Dell Software
Thank you
Richard_Douglas@Dell.com
@SQLRich
http://bit.ly/11jr4fC
Natural Born Killers, Performance issues to avoid
of 30

Natural Born Killers, Performance issues to avoid

SQL Server is now a mature RDBMS platform. In this session Richard Douglas walks through a number of areas of the product that are misused or misunderstood. The session promotes good table and index design as well as when to use temporary tables and table variables.
Published on: Mar 3, 2016
Published in: Technology      
Source: www.slideshare.net


Transcripts - Natural Born Killers, Performance issues to avoid

  • 1. Natural Born Killers, performance issues to avoid Richard Douglas
  • 2. Natural Born Killer http://www.flickr.com/photos/merille/4747615138/sizes/z/in/photostream/ 2 Natural Born Killers
  • 3. 3 Natural Born Killers
  • 4. Source: http://cheezburger.com/View/5939764992 4 Natural Born Killers
  • 5. Adhering to best practices? http://www.flickr.com/photos/12693492@N04/1338123903/sizes/m/in/photostream/ 5 Natural Born Killers
  • 6. Your host • Richard Douglas • Systems Consultant • SQL Server MCITPro • Maidenhead SQL User Group Leader • Blog: http://SQL.RichardDouglas.co.uk • Twitter: @SQLRich • Email: Richard.Douglas@Quest.com Richard_Douglas@Dell.com 6 Natural Born Killers
  • 7. Agenda • Statistics • Table Design • Scalar UDF’s • Indices • Key lookups • Sargability • Table variables • Parameter sniffing 7 Natural Born Killers
  • 8. Statistics • SQL uses a cost based optimizer • Costs influenced by statistics 8 Natural Born Killers
  • 9. Statistics • Creating Statistics – Automatic – Manual – CREATE STATISTICS – sp_CreateStats • Updating 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% • Sp_UpdateStats • UPDATE STATISTICS 9 Natural Born Killers
  • 10. Table Design http://www.flickr.com/photos/aresauburnphotos/2699269321/sizes/o/in/photost ream/ 10 Natural Born Killers
  • 11. Example of bad design 11 Natural Born Killers
  • 12. Example of a better design 12 Natural Born Killers
  • 13. Table Design – Size comparison 13 Natural Born Killers
  • 14. 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. 14 Natural Born Killers
  • 15. 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 19 Natural Born Killers
  • 16. 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 20 Natural Born Killers
  • 17. Indices 21 Natural Born Killers
  • 18. Indexing strategies It’s all about DWI knowledge: D W I a t a 22 Natural Born Killers o r k l o a d n t e r n a l s
  • 19. Golden Rules for Clustered Indexes Bad example; Surname, Firstname, Middle Initial • Static Impacts nonclustered indexes • Progressive Fragmentation • Narrow • Unique • Fixed width Space impact • Not Null 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 23 Natural Born Killers
  • 20. Golden Rules for NonClustered Indexes • Have an optimal clustered key • Not narrow • Reduce unnecessary overhead – Fixed Width – Not null • Consolidate • Index foreign keys 24 Natural Born Killers
  • 21. Key Lookups – The silent killer 25 Natural Born Killers
  • 22. Sargability • SARGable – “Search ARGument able” • T-SQL functions around a predicate can break SARGability • Some caveats to remember 26 Natural Born Killers
  • 23. 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-gotchaspart-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 27 Natural Born Killers
  • 24. 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 28 Natural Born Killers
  • 25. 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 29 Natural Born Killers
  • 26. Demos 30 Natural Born Killers
  • 27. Summary • Statistics • Table Design • Scalar UDF’s • Indices • Key lookups • Sargability • Table variables • Parameter sniffing 31 Natural Born Killers
  • 28. Any questions? 32 Understanding Indices Dell Software
  • 29. Thank you Richard_Douglas@Dell.com @SQLRich http://bit.ly/11jr4fC

Related Documents