Successfully reported this slideshow.
Your SlideShare is downloading. ×

SQL Server Tools for Query Tuning

Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Loading in …3
×

Check these out next

1 of 71 Ad

SQL Server Tools for Query Tuning

Download to read offline

Query tuning within SQL Server can be a tough skill to master. The new tooling released with SQL Server 2016 and 2017 changes how you identify poorly performing queries, troubleshoot their behavior and tune the queries, all a little easier.
This workshop teaches new techniques for tuning queries using all the new tools introduced in SQL Server 2016 and 2017. You'll be able to put this knowledge to work immediately, not only in your 2016 or better instances but also in your Azure SQL Database databases. You will be tuning your queries faster and more accurately using the new tools available.

Query tuning within SQL Server can be a tough skill to master. The new tooling released with SQL Server 2016 and 2017 changes how you identify poorly performing queries, troubleshoot their behavior and tune the queries, all a little easier.
This workshop teaches new techniques for tuning queries using all the new tools introduced in SQL Server 2016 and 2017. You'll be able to put this knowledge to work immediately, not only in your 2016 or better instances but also in your Azure SQL Database databases. You will be tuning your queries faster and more accurately using the new tools available.

Advertisement
Advertisement

More Related Content

Slideshows for you (20)

Similar to SQL Server Tools for Query Tuning (20)

Advertisement

More from Grant Fritchey (20)

Recently uploaded (20)

Advertisement

SQL Server Tools for Query 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. • Know how to apply which tools in a given situation in order to better aid query tuning. • Learn how to use various tools in aid of performance tuning.
  3. 3. GRANT FRITCHEY scarydba.com grant@scarydba.com @gfritchey youtube.com/c/GrantFritchey
  4. 4. QUERY TUNING Identify Investigate ModifyMeasure Automate
  5. 5. IDENTIFY Extended Events Query Store Dynamic Management Views
  6. 6. INVESTIGATE Query Store Execution Plans
  7. 7. MODIFY T-SQL Indexes Constraints Statistics SCOPED CONFIGURATION Keys
  8. 8. MEASURE Extended Events Query Store Dynamic Management Views SSMS Tooling
  9. 9. AUTOMATE Azure SQL Database SQL Server 2017 Query Store
  10. 10. QUERY TUNING Identify Investigate ModifyMeasure Automate
  11. 11. WHICH SQL SERVER? SQL Server 2012+ Azure SQL Database Azure SQL Data Warehouse SQL Server 2017
  12. 12. The Tools • Extended Events • Query Store • Dynamic Management Views • SSMS • Execution Plans • Automation
  13. 13. EXTENDED EVENTS
  14. 14. EXTENDED EVENTS Lightweight Low cost Superior filtering On the server Causality tracking
  15. 15. EXTENDED EVENTS Package Events Actions Targets Types Predicates Maps
  16. 16. EXTENDED EVENTS Session Targets Events Actions
  17. 17. EXTENDED EVENTS Used in Identify Measure
  18. 18. DEMO
  19. 19. QUERY STORE
  20. 20. 20 Plan Store Runtime Statistics Query Optimization Process Async Query Execution Async
  21. 21. QUERY STORE DATA: PLAN STORE VIEWS 21 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
  22. 22. QUERY STORE DATA: RUNTIME VIEWS 22 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
  23. 23. YOU CONTROL… 23 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
  24. 24. MONITORING 24 Extended Events Wait Statistics
  25. 25. PLAN FORCING 25 Plan Store Runtime Statistics Query Optimization Process Async Query Execution Async Plan forcing
  26. 26. DEMOS 26
  27. 27. QUERY TUNING OPPORTUNITIES 27 Changes in how you monitor New information available Bad parameter sniffing Cardinality estimation issues Structural problems leading to plan differences
  28. 28. UPGRADE OPPORTUNITIES 28 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
  29. 29. QUERY STORE Used in Identify Measure Automate
  30. 30. DYNAMIC MANAGEMENT VIEWS
  31. 31. DYNAMIC MANAGEMENT VIEWS (DMV) T-SQL Cache dependent Limited runtime info Ubiquitous
  32. 32. DYNAMIC MANAGEMENT VIEWS Transaction Related Sys.dm_tran_locks Sys.dm_tran_active_transactions Sys.dm_tran_database_transactions
  33. 33. DYNAMIC MANAGEMENT VIEWS Index Related sys.dm_db_index_physical_stats sys.dm_db_index_usage_stats sys.dm_db_missing_index_*
  34. 34. DYNAMIC MANAGEMENT VIEWS Execution 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
  35. 35. DEMO
  36. 36. STRICTLY SPEAKING, CHEATING • Whoisactive.com
  37. 37. DYNAMIC MANAGEMENT VIEWS Used in Identify Measure
  38. 38. SSMS TOOLS
  39. 39. SSMS TOOLS Stuff we are not talking about Data Collection Database Engine Tuning Advisor 3rd Party Profiler
  40. 40. SSMS TOOLS Stuff we could talk about but won’t Statistics Database Objects 3rd Party Fragmentation
  41. 41. SSMS TOOLS Include Client Statistics Positives History Comparisons Consistent across edits Negatives Inaccurate Inconsistent with other data Difficult to capture
  42. 42. SSMS TOOLS SET STATISTICS TIME Positives Easy Immediate Persistent Flexible Negatives Creates load Inconsistent with other data
  43. 43. SSMS TOOLS SET STATISTICS IO Positives Easy Immediate Persistent Flexible Negatives Creates HUGE load Inconsistent with other data
  44. 44. SSMS TOOLS Database Scoped Configuration Positives Granular Specialized Persistent Negatives Hidden Inconsistent behavior
  45. 45. SSMS TOOLS Used in Identify Investigate Measure Modify
  46. 46. DEMO
  47. 47. EXECUTION PLANS
  48. 48. 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.
  49. 49. WHAT IS IN A PLAN
  50. 50. WHAT IS IN A PLAN
  51. 51. WHAT IS IN A PLAN
  52. 52. WHAT IS IN A PLAN
  53. 53. EXECUTION PLANS Estimated Actual Estimated Plan with metrics
  54. 54. PLAN METRICS Degree of Parallelism Memory Grant Wait Statistics Query Time Statistics Memory Grant Info Parameter Runtime Value
  55. 55. 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
  56. 56. WHAT TO LOOK FOR
  57. 57. First Operator Warnings Most Costly Operations Fat Pipes Extra Operators Scans Estimated vs. Actual
  58. 58. FIRST OPERATOR Plan size Compile time Missing Indexes Optimization level Parameter • Compiled value • Runtime Value Query hash Reason for early termination ANSI settings
  59. 59. 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
  60. 60. LIVE QUERY STATISTICS Living Query Plan 2014+ Data only in 2012 Must activate with query Can’t run on executing query No recording
  61. 61. COMPARING EXECUTION PLANS SSMS 17.4+ 2016+ Hilight differences One plan must be file Identify similarities Includes properties
  62. 62. EXECUTION PLANS Used in Investigate Modify
  63. 63. AUTOMATION
  64. 64. ADAPTIVE QUERY PROCESSING 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
  65. 65. AUTOMATED TUNING Weaponizing Query Store Machine learning Azure SQL Database only Index creation Drop Indexes
  66. 66. AUTOMATED PLAN REGRESSION Further weaponization of Query Store 2017 Only (not Azure!) sys.dm_db _tuning_ recommendations Last good plan Self correcting
  67. 67. WRAP-UP
  68. 68. QUERY TUNING Identify Investigate ModifyMeasure Automate
  69. 69. DOCUMENTATION • Introducing SQL Server Extended Events • Plan Regressions in SQL Server 2017 • https://docs.microsoft.com/en-us/sql/relational- databases/automatic-tuning/automatic-tuning • https://docs.microsoft.com/en-us/azure/sql-database/sql- database-automatic-tuning
  70. 70. GOALS • Understand the native tools available to aid in query tuning. • Know how to apply which tools in a given situation in order to better aid query tuning. • Learn how to use various tools in aid of performance tuning.
  71. 71. LET’S TALK scarydba.com grant@scarydba.com @gfritchey Grant Fritchey

×