Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Redgate Community Circle: Tools For SQL Server Performance Tuning

244 views

Published on

This slide deck is in support of the live and video classes held by Redgate Software as part of the Community Circle initiative. This series of classes is meant to teach you the tools that can help to make query tuning easier in SQL Server. There are a number of different tools, all part of the fundamental Microsoft Data Platform offering, that can be used to help tune queries. This class outlines those tools and their uses.

Published in: Software
  • Be the first to comment

  • Be the first to like this

Redgate Community Circle: Tools For SQL Server Performance Tuning

  1. 1. SQL Server Tools For Query Tuning Grant Fritchey | Redgate Software
  2. 2. Goals Understand the native tools available to aid in query tuning.Understand Know how to apply which tools in a given situation in order to better aid query tuning.Know Learn how to use various tools in aid of performance tuning.Learn
  3. 3. DevOps Advocate @gfritchey Microsoft Data Platform MVP grant@scarydba.com scarydba.com
  4. 4. Code and Slides • https://github.com/ScaryDBA/PresentationCode/QueryTuningTools Code • https://slideshare.net Slides
  5. 5. Query Tuning Identify Investigate ModifyMeasure Automate
  6. 6. Identify Extended Events Query Store Dynamic Management Views
  7. 7. Investigate Query Store Execution Plans
  8. 8. Modify T-SQL Indexes Constraints Statistics SCOPED CONFIGURATION Keys
  9. 9. Measure Extended Events Query Store Dynamic Management Views SSMS Tooling
  10. 10. Automate Azure SQL Database SQL Server 2017+ Query Store
  11. 11. Query Tuning Identify Investigate ModifyMeasure Automate
  12. 12. Which SQL Server? SQL Server 2012+ Azure SQL Database Synapse SQL Server 2016+
  13. 13. The Tools! Extended Events Query Store Dynamic Management Views SSMS Execution Plans Automation:Bonus
  14. 14. Additional Reading SQL Server 2017 Query Performance Tuning Query Store for SQL Server 2019 SQL Server Execution Plans (3rd Edition, print copy)
  15. 15. Extended Events Community Resources Erik Darling: https://t.co/gZeXA8DjTw William Durkin: https://t.co/U1z9Huol0y Jes Schultz: https://t.co/1rkEJArFhH Swasheck https://swasheck.github.io/2016-11-28-index-rebuild- reorg/ Marcin Gminski https://t.co/hwd2Kuq6i3 Greg Moore https://t.co/sCowdWifmY Eduardo Pivaral https://www.mssqltips.com/sqlservertip/5748/start-and-stop- extended-events-sessions-automatically-using-sql-server-agent- jobs/ DBATools https://t.co/LIgB6JaHBA Gianluca Sartori https://t.co/AJTHp1tv6r Aaron Bertrand https://t.co/Pt4gQ4pTtQ Erin Stellato https://t.co/HUK0okH8Oz Jason Brimhall https://jasonbrimhall.info/2015/09/08/learning- extended-events-in-60-days/ Eric Cobb https://t.co/YvwFgrJJUe
  16. 16. Goals Understand the native tools available to aid in query tuning.Understand Know how to apply which tools in a given situation in order to better aid query tuning.Know Learn how to use various tools in aid of performance tuning.Learn
  17. 17. Extended Events
  18. 18. DevOps Advocate @gfritchey Microsoft Data Platform MVP grant@scarydba.com scarydba.com
  19. 19. Code and Slides • https://github.com/ScaryDBA/PresentationCode/QueryTuningTools Code • https://slideshare.net Slides
  20. 20. Extended events Lightweight Low cost Superior filtering On the server Causality tracking
  21. 21. Extended Events Package Events Actions Targets Types Predicates Maps
  22. 22. Extended Events Session Events Predicates Targets Actions
  23. 23. Extended events Used in Identify Measure
  24. 24. Demo
  25. 25. Additional Reading SQL Server 2017 Query Performance Tuning Query Store for SQL Server 2019 SQL Server Execution Plans (3rd Edition, print copy)
  26. 26. Extended Events Community Resources Erik Darling: https://t.co/gZeXA8DjTw William Durkin: https://t.co/U1z9Huol0y Jes Schultz: https://t.co/1rkEJArFhH Swasheck https://swasheck.github.io/2016-11-28-index-rebuild- reorg/ Marcin Gminski https://t.co/hwd2Kuq6i3 Greg Moore https://t.co/sCowdWifmY Eduardo Pivaral https://www.mssqltips.com/sqlservertip/5748/start-and-stop- extended-events-sessions-automatically-using-sql-server-agent- jobs/ DBATools https://t.co/LIgB6JaHBA Gianluca Sartori https://t.co/AJTHp1tv6r Aaron Bertrand https://t.co/Pt4gQ4pTtQ Erin Stellato https://t.co/HUK0okH8Oz Jason Brimhall https://jasonbrimhall.info/2015/09/08/learning- extended-events-in-60-days/ Eric Cobb https://t.co/YvwFgrJJUe
  27. 27. Query Store
  28. 28. DevOps Advocate @gfritchey Microsoft Data Platform MVP grant@scarydba.com scarydba.com
  29. 29. Plan Store Runtime Statistics Query Optimization Process Async Query Execution Async
  30. 30. Query Store Data: Plan Store Views sys.query_store_query query_id query_text_id query_hash object_id last_compile_start_time last_execution time avg_compile_duration count_compiles …and more sys.query_store_query_text query_text_id query_sql_text statement_sql_handle …and more sys.query_store_plan plan_id query_id engine_version compatibility_level query_plan_hash query_plan is_trivial_plan is_natively_compiled …and more
  31. 31. Query Store Data: Runtime Views sys.query_store_runtime_stats plan_id runtime_stats_interval_id count_executions avg_duration stdev_duration last_cpu_time min_cpu_time max_rowcount avg_dop avg_logical_io_reads avg_physical_io_reads …and more sys.query_store_runtime_stats_interval runtime_stats_interval_id start_time end_time comment sys.query_store_wait_stats plan_id runtime_stats_interval_id wait_category_desc avg_query_wait_time_ms std_dev_query_wait_time_ms
  32. 32. You control… Turn it on or Off Time to flush to disk Size of storage Days in storage Plans per query limit Automatic cleanup Capture Mode Aggregation Interval Length
  33. 33. Monitoring Extended Events Wait Statistics
  34. 34. Plan Forcing Plan Store Runtime Statistics Query Optimization Process Async Query Execution Async Plan forcing
  35. 35. Query Tuning Opportunities Changes in how you monitor New information available Bad parameter sniffing Cardinality Estimation issues Structural problems leading to plan differences
  36. 36. Upgrade Opportunities Restore your older database, leave compatibility level in place Turn on query store Run your load Switch compatibility level Identify regressions & force plans as needed
  37. 37. Query Store Used in Identify Measure Automate
  38. 38. DEMOS 38
  39. 39. Additional Reading SQL Server 2017 Query Performance Tuning Query Store for SQL Server 2019 SQL Server Execution Plans (3rd Edition, print copy)
  40. 40. Dynamic Management Views
  41. 41. DevOps Advocate @gfritchey Microsoft Data Platform MVP grant@scarydba.com scarydba.com
  42. 42. Dynamic Management Views (DMV) T-SQL Cache dependent Limited runtime info Ubiquitous
  43. 43. DMV: Transaction Related sys.dm_tran_locks sys.dm_tran_active_transactions sys.dm_tran_database_transactions
  44. 44. DMV: Index Related sys.dm_db_index_physic al_stats sys.dm_db_index_usage _stats sys.dm_db_missing_index_*
  45. 45. DMV: Query Related Live sys.dm_exec_requests Sys.dm_exec_session_wait_stats Sys.dm_exec_sessions Cache sys.dm_exec_query_stats sys.dm_exec_procedure_stats Cross- over sys.dm_exec_query_plan sys.dm_exec_sql_text sys.dm_exec_text_query_plan
  46. 46. DEMO
  47. 47. • Whoisactive.com • Glenn Berry
  48. 48. Dynamic Management Views Used in Identify Measure
  49. 49. Additional Reading SQL Server 2017 Query Performance Tuning Query Store for SQL Server 2019 SQL Server Execution Plans (3rd Edition, print copy)
  50. 50. SSMS Tools
  51. 51. DevOps Advocate @gfritchey Microsoft Data Platform MVP grant@scarydba.com scarydba.com
  52. 52. SSMS Tools Stuff we are not talking about Data Collection Database Engine Tuning Advisor 3rd Party Profiler
  53. 53. SSMS Tools Stuff we could talk about but won’t Statistics Database Objects 3rd Party Fragmentation
  54. 54. SSMS Tools Include Client Statistics Positives History Comparisons Consistent across edits Negatives Inaccurate Inconsistent with other data Difficult to capture
  55. 55. DEMO
  56. 56. SSMS Tools SET STATISTICS TIME Positives Easy Immediate Persistent Flexible Negatives Creates load
  57. 57. SSMS Tools SET STATISTICS IO Positives Easy Immediate Persistent Flexible Negatives Creates HUGE load
  58. 58. DEMO
  59. 59. SSMS Tools Query Properties Positives Immediate Easy Negatives Decimals
  60. 60. DEMO
  61. 61. SSMS Tools Database Scoped Configuration Positives Granular Specialized Persistent Negatives Hidden Inconsistent behavior
  62. 62. DEMO
  63. 63. SSMS Tools Used in Identify Investigate Measure Modify
  64. 64. Additional Reading SQL Server 2017 Query Performance Tuning Query Store for SQL Server 2019 SQL Server Execution Plans (3rd Edition, print copy)
  65. 65. Execution Plans
  66. 66. DevOps Advocate @gfritchey Microsoft Data Platform MVP grant@scarydba.com scarydba.com
  67. 67. What is an Execution Plan An execution plan describes the set of operations that the execution engine within SQL Server needs to perform in order to return the data or perform the action required by a query.
  68. 68. What Is In A Plan
  69. 69. What Is In A Plan
  70. 70. What Is In A Plan
  71. 71. What Is In A Plan
  72. 72. Execution Plans Estimated Execution Plan Actual Execution Plan
  73. 73. Execution Plans Execution Plan Execution Plan Plus Runtime Metrics
  74. 74. Plan Metrics Degree of Parallelism Memory Grant Wait Statistics Query Time Statistics Memory Grant Info Parameter Runtime Value
  75. 75. Operator Metrics Counts Per Thread Actual Rebinds Actual Rewinds Actual Rows Batches Actual CPU MS Actual End of Scans Actual Executions Actual Logical Reads Actual Physical Reads Actual Read Aheads Actual Elapsed DMS Actual Lob Logical Reads Actual Lob Physical Reads Actual Lob Read Aheads Input Memory Grant Output Memory Grant Used Memory Grant
  76. 76. What To Look For
  77. 77. First Operator Warnings Most Costly Operations Fat Pipes Extra Operators Scans Estimated vs. Actual What To Look For
  78. 78. First Operator Plan size Compile time Missing Indexes Optimization level Parameter • Compiled value • Runtime Value Query hash Reason for early termination ANSI settings
  79. 79. Where To Go, What To Read It’s all in the Properties Identify Node ID Combine plans and metrics Every operator is unique (to a degree) Track output from each operator
  80. 80. DEMO
  81. 81. Live Query Statistics Live Execution Query Plan 2014+ Data only in 2012 Must activate with query Can’t run on executing query No recording
  82. 82. Comparing Execution Plans SSMS 17.4+ 2016+ Hilight differences One plan must be file Identify similarities Includes properties
  83. 83. DEMO
  84. 84. Execution Plans Used in Investigate Modify
  85. 85. Additional Reading SQL Server 2017 Query Performance Tuning Query Store for SQL Server 2019 SQL Server Execution Plans (3rd Edition, print copy)
  86. 86. Automation
  87. 87. DevOps Advocate @gfritchey Microsoft Data Platform MVP grant@scarydba.com scarydba.com
  88. 88. Adaptive Query Processing 2017 Multi-Statement Table- Valued Functions Interleaved execution No data modification No CROSS APPLY Marked in execution plan Tracking through extended events Batch Mode Memory Grants Literally revises memory grant Batch Mode processing only 2x or greater on memory over 1mb No RECOMPILE Tracking through extended events Batch Mode Adaptive Joins Defers join choice Batch Mode processing only Hash or loop joins only Tracking through Query Store
  89. 89. DEMO
  90. 90. Adaptive Query Processing 2019 Approximate COUNT DISTINCT Minimum 1 million rows Aggregates columns with high number of distinct values Approximation is acceptable Row Mode Memory Grants Literally revises memory grant Batch and Row Mode processing 2x or greater on memory over 1mb No RECOMPILE Tracking through extended events Batch Mode In Row Store Query involves 100,000+ rows Analytic operators Relieves CPU binding Columnstore not needed Table Variable Deferred Compilation Gathers table variable row counts Uses those to create execution plan
  91. 91. Automated Tuning Weaponizing Query Store Machine learning Azure SQL Database only Index creation Drop Indexes
  92. 92. Automated Plan Regression Further weaponization of Query Store 2017 Only (not Azure!) sys.dm_db _tuning_ recommendations Last good plan Self correcting
  93. 93. DEMO
  94. 94. Additional Reading SQL Server 2017 Query Performance Tuning Query Store for SQL Server 2019 SQL Server Execution Plans (3rd Edition, print copy)

×