SqlServer SQLServer database of view tables, stored procedures, time-consuming queries, the current process, the overhead of a larger statement

- Statement Check database tables 
the SELECT   s2.dbid,
          the DB_NAME (s2.dbid) the AS  [ database name ] ,
          - s1.sql_handle, 
         ( the SELECT  the TOP  . 1 
                     the SUBSTRING (S2. Text , statement_start_offset /  2  +  . 1 ,
                               ( ( CASE WHEN statement_end_offset = -1
                                        THEN ( LEN(CONVERT(NVARCHAR(MAX), s2.text))
                                               * 2 )
                                        ELSE statement_end_offset
                                   END ) - statement_start_offset ) / 2 + 1)
         ) AS  [ statement ] ,
         execution_count the AS  [ executions ] ,
         last_execution_time AS  [ last start time of execution of the plan ] ,
         total_worker_time the AS  [ since compilation execution The total amount of CPU time (sec) ] ,
         last_worker_time AS  [ last execution plan used by the CPU time (in microseconds) ] ,
         min_worker_time the AS  [ had occupied during a single CPU minimum execution time (sec) ] ,
         max_worker_time AS  [ had occupied during a single execution maximum CPU time (in microseconds) ] ,
         total_logical_reads the AS  [ total logical read ] ,
         last_logical_reads the AS  [ Last read logic ] ,
         min_logical_reads the AS  [ happened logical reads ] ,
         max_logical_reads the AS  [ Maximum read logic ] ,
         total_logical_writes the AS  [ total write logic ] ,
         last_logical_writes AS  [ last logical write ] ,
         min_logical_writes the AS  [ minimum write logic ] ,
         max_logical_writes AS [最大逻辑写]
 FROM    sys.dm_exec_query_stats AS s1
         CROSS APPLY sys.dm_exec_sql_text(sql_handle) AS s2
 WHERE   s2.objectid IS NULL
 ORDER BY last_worker_time DESC


 - Statement Check stored procedure to execute 
  the SELECT 
            the DB_NAME ( the ISNULL (EPS.database_id, '')) [ Database Name ] 
       - the ISNULL (DBS.name, '') DatabaseName the AS 
       , the OBJECT_NAME (. The EPS object_id , EPS.database_id) [ stored procedure name ]  - the aS the ObjectName 
       , EPS.cached_time [ added to the cache time ] - the aS CachedTime 
       , EPS.last_elapsed_time 'latest execution time spent (sec)' - the aS LastElapsedTime 
       , EPS.last_worker_time 'last run the stored procedure with CPU time (sec) '
       , EPS.execution_count [ number of times since the last compilation performed ] - AS ExecutionCount 
       , EPS.total_worker_time / EPS.execution_count [ average amount of CPU time to execute each time used (microseconds) ] - AS AvgWorkerTime 
       , EPS.total_elapsed_time / EPS.execution_count [ average CPC execution elapsed time (sec) ] - the aS AvgElapsedTime 
       , (EPS.total_logical_reads + EPS.total_logical_writes) 
         / EPS.execution_count the aS AvgLogicalIO
       ,b.text [存储过程内容] 
 FROM sys.dm_exec_procedure_stats AS EPS
 CROSS APPLY sys.dm_exec_sql_text(EPS.sql_handle) b
 ORDER BY EPS.last_elapsed_time DESC;   

 - show time-consuming queries 
 DECLARE  @MinExecutions  int ; 
  the SET  @MinExecutions  =  5 
   
 SELECT EQS.total_worker_time AS TotalWorkerTime 
       ,EQS.total_logical_reads + EQS.total_logical_writes AS TotalLogicalIO 
       ,EQS.execution_count As ExeCnt 
       ,EQS.last_execution_time AS LastUsage 
       ,EQS.total_worker_time / EQS.execution_count as AvgCPUTimeMiS 
       ,(EQS.total_logical_reads + EQS.total_logical_writes) / EQS.execution_count  
        AS AvgLogicalIO 
       ,DB.name AS DatabaseName 
       ,SUBSTRING(EST.text 
                 ,1 + EQS.statement_start_offset / 2 
                 ,(CASE WHEN EQS.statement_end_offset = -1  
                        THEN LEN(convert(nvarchar(max), EST.text)) * 2  
                        ELSE EQS.statement_end_offset END  
                  - EQS.statement_start_offset) / 2 
                 ) AS SqlStatement 
       -- Optional with Query plan; remove comment to show, but then the query takes !!much longer time!! 
       --,EQP.[query_plan] AS [QueryPlan] 
 FROM sys.dm_exec_query_stats AS EQS 
      CROSS APPLY sys.dm_exec_sql_text(EQS.sql_handle) AS EST 
      CROSS APPLY sys.dm_exec_query_plan(EQS.plan_handle) AS EQP 
      LEFT JOIN sys.databases AS DB 
          ON EST.dbid = DB.database_id      
 WHERE EQS.execution_count > @MinExecutions 
       AND EQS.last_execution_time > DATEDIFF(MONTH, -1, GETDATE()) 
 ORDER BY AvgLogicalIo DESC,AvgCPUTimeMiS DESC


 - the current process and its statement: 
the SELECT PRO.loginame AS the LoginName
       ,DB.name AS DatabaseName 
       ,PRO.[status] as ProcessStatus 
       ,PRO.cmd AS Command 
       ,PRO.last_batch AS LastBatch 
       ,PRO.cpu AS Cpu 
       ,PRO.physical_io AS PhysicalIo 
       ,SES.row_count AS [RowCount] 
       ,STM.[text] AS SQLStatement 
 FROM sys.sysprocesses AS PRO 
      INNER JOIN sys.databases AS DB 
          ON PRO.dbid = DB.database_id 
      INNER JOIN sys.dm_exec_sessions AS SES 
         ON PRO.spid = SES.session_id 
      CROSS APPLY sys.dm_exec_sql_text(PRO.sql_handle) AS STM      
 WHERE PRO.spid >= 50  -- Exclude system processes 
 ORDER BY PRO.physical_io DESC ,PRO.cpu DESC;

 - 5, the cost of a larger inquiry: 
 the SELECT   ss.SUM_execution_count,
         t.text ,
         ss.SUM_total_elapsed_time ,
         ss.sum_total_worker_time ,
         ss.sum_total_logical_reads ,
         ss.sum_total_logical_writes
 FROM    ( SELECT    S.plan_handle ,
                     SUM(s.execution_count) SUM_Execution_count ,
                     SUM(s.total_elapsed_time) SUM_total_elapsed_time ,
                     SUM(s.total_worker_time) SUM_total_worker_time ,
                     SUM(s.total_logical_reads) SUM_total_logical_reads ,
                     SUM(s.total_logical_writes) SUM_total_logical_writes
           FROM      sys.dm_exec_query_stats s
           GROUP BY  S.plan_handle
         ) AS ss
         CROSS APPLY sys.dm_exec_sql_text(ss.plan_handle) t
 ORDER BY sum_total_logical_reads DESC 

 

Guess you like

Origin www.cnblogs.com/weifeng123/p/12099578.html