标签:str lock object_id base dep state -- col execution
1、查询SQL Server执行过的SQL语句(执行次数)
有的时候,需要知道近段时间SQLSERVER执行了什么语句,可以用下面的方法:
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
SELECT der.session_id, der.start_time, datediff(ms,der.start_time,getdate()) as ms, der.cpu_time as cputime, der.logical_reads,
dest.[text] AS ‘sqlcmd‘, der.*
FROM sys.[dm_exec_requests] AS der
CROSS APPLY
sys.[dm_exec_sql_text](der.[sql_handle]) AS dest
WHERE [session_id]>50 and [database_id]=db_id()
ORDER BY [cputime] DESC
2、开销较大的查询
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
3、找出执行时间最长的10条SQL(适用于SQL SERVER 2005及其以上版本)
如果想对SQL作筛选,可将not like ‘%fetch%‘ 换成 like ‘%user%‘就可以找出SQL语句中含有user关键字的SQL
SELECT (total_elapsed_time / execution_count)/1000 N‘平均时间ms‘ ,total_elapsed_time/1000 N‘总花费时间ms‘ ,total_worker_time/1000 N‘所用的CPU总时间ms‘ ,total_physical_reads N‘物理读取总次数‘ ,total_logical_reads/execution_count N‘每次逻辑读次数‘ ,total_logical_reads N‘逻辑读取总次数‘ ,total_logical_writes N‘逻辑写入总次数‘ ,execution_count N‘执行次数‘ ,SUBSTRING(st.text, (qs.statement_start_offset/2) + 1 ,((CASE statement_end_offset WHEN -1 THEN DATALENGTH(st.text) ELSE qs.statement_end_offset END - qs.statement_start_offset)/2) + 1) N‘执行语句‘ ,creation_time N‘语句编译时间‘ ,last_execution_time N‘上次执行时间‘ FROM sys.dm_exec_query_stats AS qs CROSS APPLY sys.dm_exec_sql_text(qs.sql_handle) st WHERE SUBSTRING(st.text, (qs.statement_start_offset/2) + 1, ((CASE statement_end_offset WHEN -1 THEN DATALENGTH(st.text) ELSE qs.statement_end_offset END - qs.statement_start_offset)/2) + 1) not like ‘fetch%‘ ORDER BY total_elapsed_time / execution_count DESC;
4、找出执行最慢的SQL语句(适用于SQL SERVER 2005及其以上版本)
--给N赋初值为30 declare @n int set @n=30;with maco as ( select top (@n) plan_handle, sum(total_worker_time) as total_worker_time , sum(execution_count) as execution_count , count(1) as sql_count from sys.dm_exec_query_stats group by plan_handle order by sum(total_worker_time) desc ) select t.text , a.total_worker_time , a.execution_count , a.sql_count from maco a cross apply sys.dm_exec_sql_text(plan_handle) t
5、找出最耗时的前N条T-SQL语句 (适用于SQL SERVER 2005及其以上版本)
SELECT TOP 20 total_worker_time/1000 AS [总消耗CPU 时间(ms)],execution_count [运行次数], qs.total_worker_time/qs.execution_count/1000 AS [平均消耗CPU 时间(ms)], last_execution_time AS [最后一次执行时间],min_worker_time /1000 AS [最小执行时间(ms)], max_worker_time /1000 AS [最大执行时间(ms)], SUBSTRING(qt.text,qs.statement_start_offset/2+1, (CASE WHEN qs.statement_end_offset = -1 THEN DATALENGTH(qt.text) ELSE qs.statement_end_offset END -qs.statement_start_offset)/2 + 1) AS [使用CPU的语法], qt.text [完整语法], qt.dbid, dbname=db_name(qt.dbid), qt.objectid,object_name(qt.objectid,qt.dbid) ObjectName FROM sys.dm_exec_query_stats qs WITH(nolock) CROSS apply sys.dm_exec_sql_text(qs.sql_handle) AS qt WHERE execution_count>1 ORDER BY (qs.total_worker_time/qs.execution_count/1000) DESC
6、平均耗CPU最多的前20个SQL (SQL SERVER 2008或以上版本)
SELECT TOP 20 total_worker_time/1000 AS [总消耗CPU 时间(ms)],execution_count [运行次数], qs.total_worker_time/qs.execution_count/1000 AS [平均消耗CPU 时间(ms)], last_execution_time AS [最后一次执行时间],max_worker_time /1000 AS [最大执行时间(ms)], SUBSTRING(qt.text,qs.statement_start_offset/2+1, (CASE WHEN qs.statement_end_offset = -1 THEN DATALENGTH(qt.text) ELSE qs.statement_end_offset END -qs.statement_start_offset)/2 + 1) AS [使用CPU的语法], qt.text [完整语法], qt.dbid, dbname=db_name(qt.dbid), qt.objectid,object_name(qt.objectid,qt.dbid) ObjectName FROM sys.dm_exec_query_stats qs WITH(nolock) CROSS apply sys.dm_exec_sql_text(qs.sql_handle) AS qt WHERE execution_count>1 ORDER BY total_worker_time DESC
7、总耗CPU最多的前20个SQL (SQL SERVER 2008或以上版本)
SELECT TOP 20
total_worker_time/1000 AS [总消耗CPU 时间(ms)],execution_count [运行次数],
qs.total_worker_time/qs.execution_count/1000 AS [平均消耗CPU 时间(ms)],
last_execution_time AS [最后一次执行时间],max_worker_time /1000 AS [最大执行时间(ms)],
SUBSTRING(qt.text,qs.statement_start_offset/2+1,
(CASE WHEN qs.statement_end_offset = -1
THEN DATALENGTH(qt.text)
ELSE qs.statement_end_offset END -qs.statement_start_offset)/2 + 1)
AS [使用CPU的语法], qt.text [完整语法],
qt.dbid, dbname=db_name(qt.dbid),
qt.objectid,object_name(qt.objectid,qt.dbid) ObjectName
FROM sys.dm_exec_query_stats qs WITH(nolock)
CROSS apply sys.dm_exec_sql_text(qs.sql_handle) AS qt
WHERE execution_count>1
ORDER BY total_worker_time DESC
8、逻辑读最高的前20条语句
select top 20
p.name as [SP Name],
deps.total_logical_reads as [TotalLogicalReads],
deps.total_logical_reads / deps.execution_count as [AvgLogicalReads],
deps.execution_count,
ISNULL(deps.execution_count/DATEDIFF(Second,deps.cached_time,Getdate()),0) as [Calls/Second],
deps.total_elapsed_time,
deps.total_elapsed_time / deps.execution_count as [avg_elapsed_time] ,
deps.cached_time
from sys.procedures as p
inner join sys.dm_exec_procedure_stats as deps on p.[object_id] = deps.[object_id]
where deps.database_id = DB_ID()
order by deps.total_logical_reads desc
9、最耗时的前20个存储过程
SELECT TOP
20 a.object_id,--对象名称
a.database_id,--数据库名称
OBJECT_NAME( object_id, database_id ) AS ‘proc name‘,--存储过程名称
a.cached_time,--存储过程添加到缓存的时间
a.last_execution_time,--上次执行时间
a.total_elapsed_time,--总耗时
a.total_elapsed_time/ a.execution_count AS [avg_elapsed_time],--平均耗时
a.execution_count,--运行次数
a.total_physical_reads/ a.execution_count avg_physical_reads,--平均物理读取数
a.total_logical_writes,--总逻辑写入次数
a.total_logical_writes/ a.execution_count avg_logical_reads,--平均逻辑写入次数
a.last_elapsed_time,--上次运行耗时
a.[total_worker_time] --此存储过程自编译以来执行所用的 CPU 时间总量(微秒)
FROM
sys.dm_exec_procedure_stats a
ORDER BY
[avg_elapsed_time] DESC;
标签:str lock object_id base dep state -- col execution
原文地址:https://www.cnblogs.com/furenjian/p/13379317.html