Natural Born Killers, performanceissues to avoidRichard DouglasEditor in chief of SQLServerPedia
Natural Born Killerhttp://www.flickr.com/photos/merille/4747615138/sizes/z/in/photostream/2   Natural Born Killers
Adhering to best practices?http://www.flickr.com/photos/12693492@N04/1338123903/sizes/m/in/photostream/3   Natural Born Ki...
Your host                           • Richard Douglas                           • Systems Consultant /                    ...
Agenda• Statistics• Table Design• SELECT pitfalls• RBAR• Scalar UDF’s• Indices• Key lookups• Sargability• Table variables•...
Statistics• SQL uses a cost based optimizer• Costs influenced by statistics6   Natural Born Killers
Statistics• Creating Statistics    – Automatic    – Manual       –    CREATE STATISTICS       –    Sp_CreateStats• Updatin...
Table Design                           http://www.flickr.com/photos/aresauburnphotos/2699269321/sizes/o/in/photost        ...
Example of bad design9   Natural Born Killers
Example of a better design10   Natural Born Killers
Table Design – Size comparison11   Natural Born Killers
Good table design benefits• Minimise the CPU overhead.• Increase the number of records in the buffer cache.• Reduce the am...
Source: http://cheezburger.com/View/593976499213   Natural Born Killers
SELECT *• Returns all data from one or more tables.     –   Pulls unnecessary data into the buffer cache     –   Can resul...
SELECT * - A resolution15   Natural Born Killers
RBAR Vs. Set Based16   Natural Born Killers
Scalar User Defined Functions• The Good     – Re-Usable code• The Bad     – Runs once per record in record set     – They ...
Scalar UDF Solutions and Alternatives• If you have to use them:     – View the actual IO usage in Profiler• If you can rep...
Indexing strategiesIt’s all about DWI knowledge:                            D W n                            a o          ...
Golden Rules for Clustered IndexesBad example; Surname, Firstname, Middle Initial• Narrow                                 ...
Golden Rules for NonClustered Indexes• Have an optimal clustered key• Not narrow• Reduce unnecessary overhead      – Fixed...
Key Lookups – The silent killer25   Natural Born Killers
Sargability• SARGable – “Search ARGument able”• T-SQL functions around a predicate can break  SARGability• Some caveats to...
Table Variables Vs. Temp TablesTable Variables• Fast when used with small data sets.• Have a limited scope• Use less locki...
Table Variables Vs. Temp TablesTemp Tables• Uses statistics     – Stat population will cause recompiles (statement level i...
Parameter Sniffing• What is it?     – It’s all a matter of statistics• How do I know when it will affect me?     – Monitor...
Demo30
Free posters, training DVDs, white papers, and more     http://www.quest.com/landing/?id=57001.        SQL Server Blog Agg...
Solution Area                        Product                                         Description    Backup and            ...
Upcoming SlideShare
Loading in …5
×

'Natural born killers, SQL performance issues to avoid'

1,567 views

Published on

Slides from SQL presentation by Richard Douglas ,Quest Systems Consultant | Editor in Chief – SQLServerPedia

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,567
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
0
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

'Natural born killers, SQL performance issues to avoid'

  1. 1. Natural Born Killers, performanceissues to avoidRichard DouglasEditor in chief of SQLServerPedia
  2. 2. Natural Born Killerhttp://www.flickr.com/photos/merille/4747615138/sizes/z/in/photostream/2 Natural Born Killers
  3. 3. Adhering to best practices?http://www.flickr.com/photos/12693492@N04/1338123903/sizes/m/in/photostream/3 Natural Born Killers
  4. 4. Your host • Richard Douglas • Systems Consultant / Editor in Chief of SQLServerPedia • SQL Server MCITPro • Maidenhead SQL User Group Leader • Blog: http://SQL.RichardDouglas.co.uk • Twitter: @SQLRich • Email: Richard.Douglas@Quest.com Richard_Douglas@Dell.com4 Natural Born Killers
  5. 5. Agenda• Statistics• Table Design• SELECT pitfalls• RBAR• Scalar UDF’s• Indices• Key lookups• Sargability• Table variables• Parameter sniffing5 Natural Born Killers
  6. 6. Statistics• SQL uses a cost based optimizer• Costs influenced by statistics6 Natural Born Killers
  7. 7. 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 STATISTICS7 Natural Born Killers
  8. 8. Table Design http://www.flickr.com/photos/aresauburnphotos/2699269321/sizes/o/in/photost ream/8 Natural Born Killers
  9. 9. Example of bad design9 Natural Born Killers
  10. 10. Example of a better design10 Natural Born Killers
  11. 11. Table Design – Size comparison11 Natural Born Killers
  12. 12. 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.12 Natural Born Killers
  13. 13. Source: http://cheezburger.com/View/593976499213 Natural Born Killers
  14. 14. SELECT *• Returns all data from one or more tables. – Pulls unnecessary data into the buffer cache – Can result in excessive IO – Increases network activity – Can hide important data in Missing Index DMV’s• Adding new attributes – May break existing code• Increases code fix time• SQL Azure – Paying for data transfer.14 Natural Born Killers
  15. 15. SELECT * - A resolution15 Natural Born Killers
  16. 16. RBAR Vs. Set Based16 Natural Born Killers
  17. 17. 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 IO statistics17 Natural Born Killers
  18. 18. 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 CLR18 Natural Born Killers
  19. 19. Indexing strategiesIt’s all about DWI knowledge: D W n a o I t r t a k e l r o n a a d l s19 Natural Born Killers
  20. 20. Golden Rules for Clustered IndexesBad example; Surname, Firstname, Middle Initial• Narrow Impacts nonclustered indexes• Static• Progressive Fragmentation• 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 23 • 1 byte for NULL values and NULL bitmap Natural Born Killers
  21. 21. 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
  22. 22. Key Lookups – The silent killer25 Natural Born Killers
  23. 23. Sargability• SARGable – “Search ARGument able”• T-SQL functions around a predicate can break SARGability• Some caveats to remember26 Natural Born Killers
  24. 24. Table Variables Vs. Temp TablesTable 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 sets27 Natural Born Killers
  25. 25. Table Variables Vs. Temp TablesTemp 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 sets28 Natural Born Killers
  26. 26. 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 Guides29 Natural Born Killers
  27. 27. Demo30
  28. 28. Free posters, training DVDs, white papers, and more http://www.quest.com/landing/?id=57001. SQL Server Blog Aggregator & Wiki at http://www.sqlserverpedia.com.2. Product Overviews at http://www.quest.com/sql-server/#c-Performance- Management. Plus, http://toadworld.com!3. Discussion forums at http://sqlserver.quest.com/forumindex.jspa?categoryID=208.4. Product video tutorials at http://db- management.com/blog/category/sqlserver/tutorials-sqlserver/. 31 Natural Born Killers
  29. 29. Solution Area Product Description Backup and Fast, flexible backup and recovery with Recovery industry-leading compression technology Performance Discover and resolve performance issues in production before they impact end users and & Operations service levels Performance Deepest possible understanding of database Tuning performance and norms Development Plan and develop applications that deliver both functionality and optimal performance Comprehensive schema, object, security and Administration change management Community, Project Community crowdsourcing for SQL Server Knowledge, tracing and performance information! Training Lucy © 2012 Quest Software Inc. All rights reserved. Pg. 32

×