PerfStats_Queries.sql 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. USE sqlnexus
  2. GO
  3. -- Find queries mostly waiting on CXPacket
  4. SELECT DISTINCT TOP 25 ISNULL(aq.[plan_total_exec_count],1) AS Nr_Execs, aq.dbname, tr.wait_type,
  5. CASE WHEN [resource_description] LIKE 'pagelock%' THEN 'Producer waiting on page lock'
  6. WHEN [resource_description] LIKE 'objectlock%' THEN 'Producer waiting on object lock'
  7. WHEN [resource_description] LIKE '%e_waitPipeNewRow%' THEN 'Producer waiting on consumer for a packet to fill'
  8. WHEN [resource_description] LIKE '%e_waitPipeGetRow%' THEN 'Consumer waiting on producer to fill a packet'
  9. ELSE 'Other' END AS [wait_resource_type],
  10. REPLACE(aq.procname, CHAR(9), ' ') AS procname, REPLACE(aq.stmt_text, CHAR(9), ' ') AS stmt_text,
  11. AVG(tr.wait_duration_ms) AS avg_wait_duration_ms,
  12. (aq.plan_total_cpu_ms/ISNULL(aq.[plan_total_exec_count],1)) AS avg_cpu_ms,
  13. (aq.plan_total_logical_reads/ISNULL(aq.[plan_total_exec_count],1)) AS avg_logical_reads,
  14. MIN(tr.runtime) AS First_Requested_at, MAX(tr.runtime) AS Last_Requested_at,
  15. tr.[program_name], tr.login_name, tr.[host_name]
  16. --,[resource_description]
  17. FROM tbl_REQUESTS AS tr
  18. INNER JOIN tbl_NOTABLEACTIVEQUERIES AS aq ON tr.session_id = aq.session_id AND tr.request_id = aq.request_id AND tr.runtime = aq.runtime
  19. WHERE (tr.wait_type = 'CXPACKET') AND dbname IS NOT NULL
  20. GROUP BY aq.dbname, aq.procname, aq.stmt_text, tr.wait_type, tr.[program_name], tr.login_name, tr.[host_name],
  21. [plan_total_exec_count],[plan_total_logical_reads],[plan_total_cpu_ms],
  22. CASE WHEN [resource_description] LIKE 'pagelock%' THEN 'Producer waiting on page lock'
  23. WHEN [resource_description] LIKE 'objectlock%' THEN 'Producer waiting on object lock'
  24. WHEN [resource_description] LIKE '%e_waitPipeNewRow%' THEN 'Producer waiting on consumer for a packet to fill'
  25. WHEN [resource_description] LIKE '%e_waitPipeGetRow%' THEN 'Consumer waiting on producer to fill a packet'
  26. ELSE 'Other' END
  27. --,[resource_description]
  28. ORDER BY AVG(tr.wait_duration_ms) DESC
  29. GO
  30. -- Find queries mostly waiting on latches
  31. SELECT DISTINCT TOP 25 ISNULL(aq.[plan_total_exec_count],1) AS Nr_Execs, aq.dbname, tr.wait_type,
  32. (SELECT CASE
  33. WHEN pageid = 1 OR pageid % 8088 = 0 THEN 'Is_PFS_Page'
  34. WHEN pageid = 2 OR pageid % 511232 = 0 THEN 'Is_GAM_Page'
  35. WHEN pageid = 3 OR (pageid - 1) % 511232 = 0 THEN 'Is_SGAM_Page'
  36. WHEN pageid IS NULL THEN NULL
  37. ELSE 'Is_not_PFS_GAM_SGAM_page' END
  38. FROM (SELECT CASE WHEN tr.[wait_type] LIKE 'PAGE%LATCH%' AND tr.[wait_resource] LIKE '%:%'
  39. THEN CAST(RIGHT(tr.[wait_resource], LEN(tr.[wait_resource]) - CHARINDEX(':', tr.[wait_resource], LEN(tr.[wait_resource])-CHARINDEX(':', REVERSE(tr.[wait_resource])))) AS int)
  40. ELSE NULL END AS pageid) AS latch_pageid
  41. ) AS wait_resource_type,
  42. tr.[wait_resource],
  43. REPLACE(aq.procname, CHAR(9), ' ') AS procname, REPLACE(aq.stmt_text, CHAR(9), ' ') AS stmt_text,
  44. AVG(tr.wait_duration_ms) AS avg_wait_duration_ms,
  45. (aq.plan_total_cpu_ms/ISNULL(aq.[plan_total_exec_count],1)) AS avg_cpu_ms,
  46. (aq.plan_total_logical_reads/ISNULL(aq.[plan_total_exec_count],1)) AS avg_logical_reads,
  47. MIN(tr.runtime) AS First_Requested_at, MAX(tr.runtime) AS Last_Requested_at,
  48. tr.[program_name], tr.login_name, tr.[host_name]
  49. FROM tbl_REQUESTS AS tr
  50. INNER JOIN tbl_NOTABLEACTIVEQUERIES AS aq ON tr.session_id = aq.session_id AND tr.request_id = aq.request_id AND tr.runtime = aq.runtime
  51. WHERE (tr.wait_type LIKE '%LATCH%') AND dbname IS NOT NULL
  52. GROUP BY aq.dbname, aq.procname, aq.stmt_text, tr.wait_type, tr.[program_name], tr.login_name, tr.[host_name], wait_resource,
  53. [plan_total_exec_count],[plan_total_logical_reads],[plan_total_cpu_ms]
  54. ORDER BY AVG(tr.wait_duration_ms) DESC
  55. GO
  56. -- Find queries mostly waiting on locks
  57. SELECT DISTINCT TOP 25 ISNULL(aq.[plan_total_exec_count],1) AS Nr_Execs, aq.dbname, tr.wait_type, tr.[wait_resource],
  58. REPLACE(aq.procname, CHAR(9), ' ') AS procname, REPLACE(aq.stmt_text, CHAR(9), ' ') AS stmt_text,
  59. AVG(tr.wait_duration_ms) AS avg_wait_duration_ms,
  60. (aq.plan_total_cpu_ms/ISNULL(aq.[plan_total_exec_count],1)) AS avg_cpu_ms,
  61. (aq.plan_total_logical_reads/ISNULL(aq.[plan_total_exec_count],1)) AS avg_logical_reads,
  62. MIN(tr.runtime) AS First_Requested_at, MAX(tr.runtime) AS Last_Requested_at,
  63. tr.[program_name], tr.login_name, tr.[host_name]
  64. FROM tbl_REQUESTS AS tr
  65. INNER JOIN tbl_NOTABLEACTIVEQUERIES AS aq ON tr.session_id = aq.session_id AND tr.request_id = aq.request_id AND tr.runtime = aq.runtime
  66. WHERE (tr.wait_type LIKE '%LCK%') AND dbname IS NOT NULL
  67. GROUP BY aq.dbname, aq.procname, aq.stmt_text, tr.wait_type, tr.[program_name], tr.login_name, tr.[host_name], [wait_resource],
  68. [plan_total_exec_count],[plan_total_logical_reads],[plan_total_cpu_ms]
  69. ORDER BY AVG(tr.wait_duration_ms) DESC, ISNULL(aq.[plan_total_exec_count],1) DESC
  70. GO
  71. -- Find queries mostly waiting on writelog
  72. SELECT DISTINCT TOP 25 ISNULL(aq.[plan_total_exec_count],1) AS Nr_Execs, aq.dbname, tr.wait_type,
  73. REPLACE(aq.procname, CHAR(9), ' ') AS procname, REPLACE(aq.stmt_text, CHAR(9), ' ') AS stmt_text,
  74. AVG(tr.wait_duration_ms) AS avg_wait_duration_ms,
  75. (aq.plan_total_cpu_ms/ISNULL(aq.[plan_total_exec_count],1)) AS avg_cpu_ms,
  76. (aq.plan_total_logical_reads/ISNULL(aq.[plan_total_exec_count],1)) AS avg_logical_reads,
  77. MIN(tr.runtime) AS First_Requested_at, MAX(tr.runtime) AS Last_Requested_at,
  78. tr.[program_name], tr.login_name, tr.[host_name]
  79. FROM tbl_REQUESTS AS tr
  80. INNER JOIN tbl_NOTABLEACTIVEQUERIES AS aq ON tr.session_id = aq.session_id AND tr.request_id = aq.request_id AND tr.runtime = aq.runtime
  81. WHERE (tr.wait_type LIKE 'WRITELOG%') AND dbname IS NOT NULL
  82. GROUP BY aq.dbname, aq.procname, aq.stmt_text, tr.wait_type, tr.[program_name], tr.login_name, tr.[host_name],
  83. [plan_total_exec_count],[plan_total_logical_reads],[plan_total_cpu_ms]
  84. ORDER BY ISNULL(aq.[plan_total_exec_count],1) DESC
  85. GO
  86. -- Find queries mostly waiting on async networkio
  87. SELECT DISTINCT TOP 25 ISNULL(aq.[plan_total_exec_count],1) AS Nr_Execs, aq.dbname, tr.wait_type, tr.[wait_resource],
  88. REPLACE(aq.procname, CHAR(9), ' ') AS procname, REPLACE(aq.stmt_text, CHAR(9), ' ') AS stmt_text,
  89. AVG(tr.wait_duration_ms) AS avg_wait_duration_ms,
  90. (aq.plan_total_cpu_ms/ISNULL(aq.[plan_total_exec_count],1)) AS avg_cpu_ms,
  91. (aq.plan_total_logical_reads/ISNULL(aq.[plan_total_exec_count],1)) AS avg_logical_reads,
  92. MIN(tr.runtime) AS First_Requested_at, MAX(tr.runtime) AS Last_Requested_at,
  93. tr.[program_name], tr.login_name, tr.[host_name]
  94. FROM tbl_REQUESTS AS tr
  95. INNER JOIN tbl_NOTABLEACTIVEQUERIES AS aq ON tr.session_id = aq.session_id AND tr.request_id = aq.request_id AND tr.runtime = aq.runtime
  96. WHERE (tr.wait_type LIKE 'ASYNC%') AND dbname IS NOT NULL
  97. GROUP BY aq.dbname, aq.procname, aq.stmt_text, tr.wait_type, tr.[program_name], tr.login_name, tr.[host_name], [wait_resource],
  98. [plan_total_exec_count],[plan_total_logical_reads],[plan_total_cpu_ms]
  99. ORDER BY AVG(tr.wait_duration_ms) DESC, ISNULL(aq.[plan_total_exec_count],1) DESC
  100. GO