Advertisement
hecrus

as_perf

Nov 8th, 2020
2,673
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
T-SQL 7.50 KB | None | 0 0
  1. USE [Falcon]
  2. GO
  3. /****** Object:  StoredProcedure [dbo].[as_perf]    Script Date: 08.11.2020 20:20:15 ******/
  4. SET ANSI_NULLS ON
  5. GO
  6. SET QUOTED_IDENTIFIER ON
  7. GO
  8.  
  9. ALTER PROCEDURE [dbo].[as_perf]
  10.    
  11. AS
  12. BEGIN
  13.    
  14.  
  15. select 'use master; GRANT VIEW SERVER STATE TO login' 'Дать права на спец запросы'
  16.  
  17. select 'Запросы с большим CPU' SECTION
  18.  
  19. SELECT TOP (5)
  20.     qs.total_worker_time AS Total_CPU,
  21.     db_name(st.dbID) db,
  22.     st.text,
  23.     total_CPU_inSeconds = --Converted from microseconds
  24.     qs.total_worker_time/1000000,
  25.     average_CPU_inSeconds = --Converted from microseconds
  26.     (qs.total_worker_time/1000000) / qs.execution_count,
  27.     qs.total_elapsed_time,
  28.     total_elapsed_time_inSeconds = --Converted from microseconds
  29.     qs.total_elapsed_time/1000000
  30.     qs.execution_count
  31.     --qp.query_plan
  32. FROM sys.dm_exec_query_stats AS qs
  33. CROSS APPLY sys.dm_exec_sql_text(qs.sql_handle) AS st
  34. CROSS apply sys.dm_exec_query_plan (qs.plan_handle) AS qp
  35. ORDER BY qs.total_worker_time DESC OPTION (RECOMPILE);
  36.  
  37. select 'TOP 5 хранимок по CPU' SECTION
  38. SELECT TOP 5    total_worker_time / execution_count AS AVG_CPU,
  39.     db_name(d.database_id) db, OBJECT_NAME(object_id, database_id) 'sp'
  40.    
  41.       ,total_elapsed_time / execution_count AS AVG_ELAPSED
  42.       ,total_logical_reads / execution_count AS AVG_LOGICAL_READS
  43.       ,total_logical_writes / execution_count AS AVG_LOGICAL_WRITES
  44.       ,total_physical_reads  / execution_count AS AVG_PHYSICAL_READS,
  45.  
  46.     d.last_elapsed_time, d.execution_count,
  47.     d.cached_time, d.last_execution_time, d.total_elapsed_time 
  48. FROM sys.dm_exec_procedure_stats AS d
  49. ORDER BY total_worker_time / execution_count DESC;
  50.  
  51.  
  52.  
  53.  
  54.  
  55.  
  56. select 'Проблемные запросы по памяти' SECTION
  57.  
  58. set transaction isolation level read uncommitted
  59. select
  60.     top 10
  61.     convert(money, (qs.total_elapsed_time))/(execution_count*1000)  AVG_DURATION_MS,
  62.     db_name(st.dbid)as db,
  63.     case
  64.         when sql_handle IS NULL then ' '
  65.         else(substring(st.text,(qs.statement_start_offset+2)/2,(
  66.             case
  67.                 when qs.statement_end_offset =-1 then len(convert(nvarchar(MAX),st.text))*2      
  68.                 else qs.statement_end_offset    
  69.             end - qs.statement_start_offset)/2  ))
  70.     end as text,
  71.     creation_time,
  72.     last_execution_time,
  73.     execution_count,
  74.     total_worker_time/1000 as CPU,
  75.     convert(money, (total_worker_time))/(execution_count*1000)as [AvgCPUTime],
  76.     qs.total_elapsed_time/1000 as TotDuration,
  77.     total_logical_reads as [Reads],
  78.     total_logical_writes as [Writes],
  79.     total_logical_reads+total_logical_writes as [AggIO],
  80.     convert(money, (total_logical_reads+total_logical_writes)/(execution_count + 0.0))as [AvgIO],
  81.     object_schema_name(st.objectid, st.dbid)+'.'+object_name(st.objectid, st.dbid) as object_name
  82. from sys.dm_exec_query_stats  qs
  83. cross apply sys.dm_exec_sql_text(sql_handle) st
  84. where total_logical_reads > 0
  85. order by AVG_DURATION_MS desc
  86.  
  87. select 'Запросы с большим кол-вом строк' SECTION
  88. set transaction isolation level read uncommitted
  89. select
  90.     top 5
  91.     max_rows ROWS_COUNT,
  92.     db_name (dbID) db,
  93.     case
  94.         when sql_handle IS NULL then ' '
  95.         else(substring(st.text,(qs.statement_start_offset+2)/2,(
  96.             case
  97.                 when qs.statement_end_offset =-1 then len(convert(nvarchar(MAX),st.text))*2      
  98.                 else qs.statement_end_offset    
  99.             end - qs.statement_start_offset)/2+1  ))
  100.     end as query_text,
  101.     execution_count,
  102.     convert( nvarchar, last_execution_time, 120) last_time,
  103.     total_worker_time/1000 as CPU,
  104.     convert(money, (total_worker_time))/(execution_count*1000)as [AvgCPUTime],
  105.     qs.total_elapsed_time/1000 as TotDuration,
  106.     convert(money, (qs.total_elapsed_time))/(execution_count*1000)as [AvgDur]  
  107. from sys.dm_exec_query_stats  qs
  108. cross apply sys.dm_exec_sql_text(sql_handle) st
  109. --where total_logical_reads > 0
  110. order by max_rows desc
  111.  
  112.  
  113. select 'Долгие запросы по времени' SECTION
  114.  
  115. SELECT TOP 10
  116.     qs.total_elapsed_time / qs.execution_count / 1000000.0 AS AVG_SEC,
  117.     DB_NAME(qt.dbid) AS db,
  118.     case
  119.         when sql_handle IS NULL then ' '
  120.         else(substring(qt.text,(qs.statement_start_offset+2)/2,(
  121.             case
  122.                 when qs.statement_end_offset =-1 then len(convert(nvarchar(MAX),qt.text))*2      
  123.                 else qs.statement_end_offset    
  124.             end - qs.statement_start_offset)/2+1  ))
  125.     end as query_text,
  126.     qs.total_elapsed_time / 1000000.0 AS total_seconds,
  127.     qs.execution_count,    
  128.     o.name AS object_name    
  129.   FROM sys.dm_exec_query_stats qs
  130.     CROSS APPLY sys.dm_exec_sql_text(qs.sql_handle) as qt
  131.     LEFT OUTER JOIN sys.objects o ON qt.objectid = o.object_id
  132. where   qs.total_elapsed_time / qs.execution_count / 1000000.0 > 0.2
  133.   ORDER BY AVG_SEC DESC;
  134.  
  135. select 'Процессы SQL Server по CPU (DBCC INPUTBUFFER(@spid))' SECTION
  136.  
  137. SELECT top 5 cpu, db_name(dbid) db,  spid, kpid,  memusage FROM sysprocesses
  138. order by cpu desc
  139.  
  140. select 'Часто выполняемые sql запросы' SECTION
  141.  
  142. SELECT distinct top 10 execution_count,   DB_NAME(dbid), text , objectid
  143.        FROM sys.dm_exec_query_stats AS a
  144. CROSS APPLY sys.dm_exec_sql_text(a.sql_handle) AS b
  145.    ORDER BY execution_count DESC
  146.  
  147. select 'Размеры таблиц (по количеству строк)' SECTION
  148.  
  149. SELECT top 5 QUOTENAME(SCHEMA_NAME(sOBJ.schema_id)) + '.' + QUOTENAME(sOBJ.name) AS [TableName]
  150.       , SUM(sPTN.Rows) AS [RowCount]
  151. FROM sys.objects AS sOBJ INNER JOIN sys.partitions AS sPTN
  152.             ON sOBJ.object_id = sPTN.object_id
  153. WHERE sOBJ.type = 'U'
  154.       AND sOBJ.is_ms_shipped = 0x0   AND index_id < 2 -- 0:Heap, 1:Clustered
  155. GROUP BY sOBJ.schema_id, sOBJ.name
  156. ORDER BY [RowCount] desc
  157.  
  158. select 'Размеры таблиц (Мб)' SECTION
  159.  
  160. SELECT top 10
  161.     CAST(ROUND(((SUM(a.total_pages) * 8) / 1024.00), 2) AS NUMERIC(36, 2)) AS TotalSpaceMB,
  162.     t.NAME AS TableName,
  163.     --s.Name AS SchemaName,
  164.     p.rows,
  165.     --SUM(a.total_pages) * 8 AS TotalSpaceKB,
  166.     --SUM(a.used_pages) * 8 AS UsedSpaceKB,
  167.     CAST(ROUND(((SUM(a.used_pages) * 8) / 1024.00), 2) AS NUMERIC(36, 2)) AS UsedSpaceMB,
  168.     --(SUM(a.total_pages) - SUM(a.used_pages)) * 8 AS UnusedSpaceKB,
  169.     CAST(ROUND(((SUM(a.total_pages) - SUM(a.used_pages)) * 8) / 1024.00, 2) AS NUMERIC(36, 2)) AS UnusedSpaceMB
  170. FROM
  171.     sys.tables t
  172. INNER JOIN      
  173.     sys.indexes i ON t.OBJECT_ID = i.object_id
  174. INNER JOIN
  175.     sys.partitions p ON i.object_id = p.OBJECT_ID AND i.index_id = p.index_id
  176. INNER JOIN
  177.     sys.allocation_units a ON p.partition_id = a.container_id
  178. LEFT OUTER JOIN
  179.     sys.schemas s ON t.schema_id = s.schema_id
  180. WHERE
  181.     t.NAME NOT LIKE 'dt%'
  182.     AND t.is_ms_shipped = 0
  183.     AND i.OBJECT_ID > 255
  184. GROUP BY
  185.     t.Name, s.Name, p.Rows
  186. ORDER BY
  187.     TotalSpaceMB DESC, t.Name
  188.    
  189.  
  190. select 'БЛОКИРОВКИ - ПРОЦЕССЫ с блокировкой' SECTION
  191.  
  192. SELECT * FROM sys.sysprocesses
  193. WHERE blocked > 0
  194.  
  195. select 'БЛОКИРОВКИ - Сессии с блокировкой' SECTION
  196. SELECT  
  197.     der.blocking_session_id AS BlockingSessionID
  198.     ,dest.text AS BlockingStatement, *
  199. FROM sys.dm_exec_connections AS sdec
  200. INNER JOIN sys.dm_exec_requests AS der
  201.     ON sdec.session_id = der.blocking_session_id
  202. INNER JOIN sys.dm_os_waiting_tasks AS dowt
  203.     ON der.session_id = dowt.session_id
  204. CROSS APPLY sys.dm_exec_sql_text(sdec.most_recent_sql_handle) AS dest
  205.  
  206. select 'БЛОКИРОВКИ - Транзакции с блокировкой' SECTION
  207. SELECT
  208.     request_session_id AS SPID
  209.     ,DB_NAME(resource_database_id) AS DatabaseName,
  210.     *
  211. FROM sys.dm_tran_locks
  212.  
  213. select 'БЛОКИРОВКИ - sp_lock' SECTION
  214. exec sp_lock
  215.  
  216. END;
  217.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement