demo-full.sql 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. use wideworldimporters
  2. go
  3. /********************************************************
  4. * SETUP - clear everything
  5. ********************************************************/
  6. EXEC [dbo].[initialize]
  7. /********************************************************
  8. * PART I
  9. * Plan regression identification.
  10. ********************************************************/
  11. -- 1. Start workload - execute procedure 30 times:
  12. -- Confirm that execution time is (3 sec total)
  13. begin
  14. declare @packagetypeid int = 7;
  15. exec dbo.report @packagetypeid
  16. end
  17. go 30
  18. -- 2. Execute procedure that causes plan regression
  19. exec dbo.regression
  20. -- 3. Start workload again - verify that is slower.
  21. -- Execution time should be (8secs in total)
  22. -- Notice we only ran it 20 times and it was still 50%+ slower
  23. begin
  24. declare @packagetypeid int = 7;
  25. exec dbo.report @packagetypeid
  26. end
  27. go 20
  28. -- 4. Find recommendation recommended by database:
  29. SELECT reason, score,
  30. JSON_VALUE(state, '$.currentValue') state,
  31. JSON_VALUE(state, '$.reason') state_transition_reason,
  32. JSON_VALUE(details, '$.implementationDetails.script') script,
  33. planForceDetails.*
  34. FROM sys.dm_db_tuning_recommendations
  35. CROSS APPLY OPENJSON (Details, '$.planForceDetails')
  36. WITH ( [query_id] int '$.queryId',
  37. [new plan_id] int '$.regressedPlanId',
  38. [recommended plan_id] int '$.recommendedPlanId'
  39. ) as planForceDetails;
  40. -- Note: User can apply script and force the recommended plan to correct the error.
  41. -- In part II will be shown better approach - automatic tuning.
  42. /********************************************************
  43. * PART II
  44. * Automatic tuning
  45. ********************************************************/
  46. /********************************************************
  47. * RESET - clear everything
  48. ********************************************************/
  49. DBCC FREEPROCCACHE;
  50. ALTER DATABASE current SET QUERY_STORE CLEAR ALL;
  51. -- Enable automatic tuning on the database:
  52. ALTER DATABASE current
  53. SET AUTOMATIC_TUNING ( FORCE_LAST_GOOD_PLAN = ON);
  54. -- Verify that actual state on FLGP is ON:
  55. SELECT name, desired_state_desc, actual_state_desc, reason_desc
  56. FROM sys.database_automatic_tuning_options;
  57. -- 1. Start workload - execute procedure 30 times like in the phase I
  58. -- Execution should be around 3-4 secs again.
  59. begin
  60. declare @packagetypeid int = 7;
  61. exec dbo.report @packagetypeid
  62. end
  63. go 30
  64. -- 2. Execute the procedure that causes plan regression
  65. exec dbo.regression
  66. -- 3. Start workload again - verify that it is slower.
  67. -- Execution should be again 50%+ slower with only 20 executions
  68. begin
  69. declare @packagetypeid int = 7;
  70. exec dbo.report @packagetypeid
  71. end
  72. go 20
  73. -- 4. Find recommendation that returns query perf regression
  74. -- and check is it in Verifying state:
  75. SELECT reason, score,
  76. JSON_VALUE(state, '$.currentValue') state,
  77. JSON_VALUE(state, '$.reason') state_transition_reason,
  78. JSON_VALUE(details, '$.implementationDetails.script') script,
  79. planForceDetails.*
  80. FROM sys.dm_db_tuning_recommendations
  81. CROSS APPLY OPENJSON (Details, '$.planForceDetails')
  82. WITH ( [query_id] int '$.queryId',
  83. [new plan_id] int '$.regressedPlanId',
  84. [recommended plan_id] int '$.recommendedPlanId'
  85. ) as planForceDetails;
  86. -- 5. Wait until recommendation is applied and start workload again - verify that it is faster.
  87. -- Execution should be back to normal execution
  88. begin
  89. declare @packagetypeid int = 7;
  90. exec dbo.report @packagetypeid
  91. end
  92. go 20
  93. -- Open query store dialogs in SSMS and show that better plan is forced looking at the Top Resource Consuming Queries Report