Sql query analyzer & maintenance


Published on

16 februari 2011

Published in: Technology
1 Like
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Sql query analyzer & maintenance

  1. 1. SQL Analyzer tools&Database Maintenance<br />Hans Schellingerhoudt<br />16 februari 2011<br />Presentation is createdforNspyre Technologie Group .Net<br />
  2. 2. Program<br /><ul><li>SQL Query Analyzer
  3. 3. Introduction
  4. 4. Execution plan
  5. 5. Column Statistics
  6. 6. Analyzer
  7. 7. Query tuning
  8. 8. Optimization
  9. 9. User Statistics
  10. 10. Demo
  11. 11. Other analyzer tools
  12. 12. Introduction Profiler
  13. 13. Tuning advisor
  14. 14. Demo
  15. 15. Database Maintenance
  16. 16. Introduction
  17. 17. Maintenance tasks
  18. 18. Demo
  19. 19. Questions</li></ul>2<br />
  20. 20. <ul><li>SQL Query Analyzer</li></ul>3<br />
  21. 21. Introduction<br />Introduction “Query Analyzer”<br /><ul><li>SQL Query Analyzer no longer exist as single “tool” (since SQL Server 2005/2008).
  22. 22. Integrated in Management Studio.
  23. 23. Visualizes execution plan / statistics information
  24. 24. A query execution plan outlines how the SQL Server query optimizer actually ran (or will run).
  25. 25. An execution plan, simply put, is the result of the query optimizer's attempt to calculate the most efficient way to imple­ment the request represented by the T-SQL query you sub­mitted.
  26. 26. You can use the execution plan to identify the exact piece of SQL code that is causing the problem</li></ul>4<br />
  27. 27. Execution plan<br />Facts about the SQL Query Analyzer<br /><ul><li>SQL Query Analyzer has number of steps, one of them is the query optimizer.
  28. 28. Optimizer uses the statistics to calculate the cost-based plan, the optimal way to execute the query. It calculates a number of plans.
  29. 29. Statistics exists for relevant columns and indexes.
  30. 30. Estimated execution plan is the output of the optimizer.
  31. 31. Actual execution plan is the output of the actual query execution
  32. 32. Estimated and Actual execution plan can differ if statistics is not accurate.</li></ul>5<br />
  33. 33. Column statistics<br />Column statistics<br />Generally, column statistics, if needed by the Query Optimizer, are generated automatically by SQL Server when queries are being optimized by the Query Optimizer.SQL Server's engine will update the statistic when:<br /><ul><li>When data is initially added to an empty table
  34. 34. The table had > 500 records when statistics were last collected and the lead column of the statistics object has now increased by 500 records since that collection date
  35. 35. The table had < 500 records when statistics were last collected and the lead column of the statistics object has now increased by 500 records + 20% of the row count from the previous statistics collection date</li></ul>-There are two cases when SQL Server will not automatically create column statistics, even when they fit the criteria described above. These include: when the costs needed to create the column statistics are larger than the costs of the query plan; and when SQL Server is too busy.<br />6<br />
  36. 36. Column statistics<br />Column statistics continued<br />There are two cases when SQL Server will not automatically create column statistics, even when they fit the criteria described in the previous sheet. These include: <br /><ul><li>When the costs needed to create the column statistics are larger than the costs of the query plan;
  37. 37. And when SQL Server is too busy.</li></ul>7<br />
  38. 38. Run analyzer<br />How to run the SQL Query Analyser (and User statistics):<br /><ul><li>Open “new query” in management studio.
  39. 39. Swith on the buttons “Actual execution plan” (and “User statistics”).
  40. 40. Run the query and the information will be shown in the execution plan (en user statistics) windows. </li></ul>8<br />
  41. 41. Run analyzer<br />Use the results of the execution plan:<br />9<br />
  42. 42. Query tuning<br />If you see any of the following in an execution plan, you should consider them warning signs and investigate them for potential performance problems:<br /><ul><li>Hash operations
  43. 43. Index or Table scans
  44. 44. Sort</li></ul>Each of them are less than ideal from a performance perspective.<br />10<br />
  45. 45. Optimization<br />General optimalisation steps:<br />Use primairy keys<br />Use foreign keys<br />Use constrains<br />Create indexes for the where clauses<br />User ‘included’ indexes for query results (read)<br />Use the execution plan for further optimalisation steps.<br />Use the profiles, see next subject, verification of the previous steps.<br />Note: For write actions it is better to have not to mutch indexes.<br />11<br />Use these by default in the design process, should be … for most of the joins.<br />
  46. 46. User statistics<br />User Statistics introduction:<br /><ul><li>Do not relate queries execution plan with query cost.
  47. 47. The amount of the data returned is considerably large, which is causing any query to run slow.
  48. 48. Looking at the result set and looking at the user Statistics.
  49. 49. Measure how much data is retrieved from server.
  50. 50. It will capture details for different trials and they are compared with each other.</li></ul>12<br />
  51. 51. User statistics<br />13<br />Green arrows indicate improved statistics and red arrows indicate degrading statistics.<br />A maximum of 10 trials are averaged in the display window<br />
  52. 52. Demo<br />Demo Query Analyser<br /> - Execution plan<br /> - User statistics <br />14<br />
  53. 53. <ul><li>Otheranalyzer tools (profiler)</li></ul>15<br />
  54. 54. Profilerintroduction<br />16<br />Database profiler<br />
  55. 55. Profiler<br />17<br />Using Profiler with the Database Engine Tuning Advisor<br />
  56. 56. TuningAdvisor<br />18<br />Using Profiler with the Database Engine Tuning Advisor<br />
  57. 57. TuningAdvisor<br />19<br />Using Profiler with the Database Engine Tuning Advisor<br />
  58. 58. TuningAdvisor<br />20<br />
  59. 59. <ul><li>Database Maintenance</li></ul>21<br />
  60. 60. IntroductionMaintenance<br />Database maintenancetasks<br />Managing data and transaction log files<br />Eliminating index fragmentation<br />Ensuring accurate, up-to-date statistics<br />Establishing an effective backup strategy<br />22<br />
  61. 61. Maintenancetasks<br />Transaction log files<br />Transaction log is separated file.<br />Transaction file will grow for each database change.<br />The steps of grow can be configured.<br />23<br />
  62. 62. Maintenancetasks<br />Index fragmentation<br />Indexes are used for performance<br />Indexes will be fragmented by delete, merge and insert actions.<br />Indexes can also be fragmented by database (auto)shrink. Don’t use shrink! <br />24<br />
  63. 63. Maintenancetasks<br />Accurate, up-to-date statistics<br />Provides information nessessary for the optimizer to make its calculation (Estimated and actual execution plan should be the same).<br />Rebuild the statistics<br />25<br />
  64. 64. Maintenancetasks<br />Effective backup strategy<br />Backup the actual data situation<br />Back in time (transaction time)<br />26<br />
  65. 65. Maintenancetasks<br />Automatic processes in SQL Server:<br /><ul><li>SQL-T statements in a JOB scheduling</li></ul>Executed with SQL statements for dedicated actions.<br />Don’t use this for maintenance, use the Maintenance Plans.<br /><ul><li>Maintenance Plans</li></ul>Part of the database management tools.<br />Part is called Maintenance Plans.<br />Note: May generate unwanted overhead on a production system and “locks”. Plan maintenance at a ‘correct’ moment.<br />27<br />
  66. 66. Maintenancetasks<br />Demo Maintenance Plans<br />28<br />
  67. 67. Conclusion<br />General Conclusion<br /><ul><li>Use default design roles (create keys and indexes).
  68. 68. Check important queries with an execute plan.
  69. 69. Run profiler when you expect performance issues and on regular basis.
  70. 70. Schedule maintenance Plans for the normal maintenance tasks.</li></ul>Note: SQL Server instance installation and configuration is not part of this presentation. But for performance issues, your most ‘important’ databases/tables, could or must be configured on hardware/spindels with a fast raid configuration!<br />29<br />
  71. 71. References<br />Link references:<br />E-Books* references:<br />-SQL Server Execution Plans by Grant Fritchey, ISBN 978-1-906434-04-5<br />-Mastering SQL Server Profiler by Brad M McGehee, ISBN 978-1-906434-11-3<br />*eBooks also used for some pictures.<br />30<br />
  72. 72. Questions?<br />End of presentation<br />31<br />