时间:2021-07-01 10:21:17 帮助过:16人阅读
2、展示耗时查询:
-- List expensive queries DECLARE @MinExecutions int; 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
3、当前进程及其语句:
-- Current processes and their SQL statements SELECT PRO.loginame AS 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;
4、存储过程执行情况:
-- Stored Procedure Execution Statistics SELECT ISNULL(DBS.name, ‘‘) AS DatabaseName ,OBJECT_NAME(EPS.object_id, EPS.database_id) AS ObjectName ,EPS.cached_time AS CachedTime ,EPS.last_elapsed_time AS LastElapsedTime ,EPS.execution_count AS ExecutionCount ,EPS.total_worker_time / EPS.execution_count AS AvgWorkerTime ,EPS.total_elapsed_time / EPS.execution_count AS AvgElapsedTime ,(EPS.total_logical_reads + EPS.total_logical_writes) / EPS.execution_count AS AvgLogicalIO FROM sys.dm_exec_procedure_stats AS EPS LEFT JOIN sys.databases AS DBS ON EPS.database_id = DBS.database_id ORDER BY AvgWorkerTime DESC;
5、开销较大的查询:
/* 开销较大的查询 */ 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
SQL 几个查看性能的语句
标签:logic ediff style processes lan handle proc span rom