SELECT  creation_time  N'语句编译时间'
        ,last_execution_time  N'上次执行时间'
        ,total_physical_reads N'物理读取总次数'
        ,total_logical_reads/execution_count N'每次逻辑读次数'
        ,total_logical_reads  N'逻辑读取总次数'
        ,total_logical_writes N'逻辑写入总次数'
        , execution_count  N'执行次数'
        , total_worker_time/1000 N'所用的CPU总时间ms'
        , total_elapsed_time/1000  N'总花费时间ms'
        , (total_elapsed_time / execution_count)/1000  N'平均时间ms'
        ,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'执行语句'
FROM dm_exec_query_stats AS qs
CROSS APPLY 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;

 

 

 

 

--总的逻辑读写次数
select sum(qs.total_logical_reads)
from sys.dm_exec_query_stats as qs
cross apply sys.dm_exec_sql_text(qs.sql_handle) as st

--前n条语句的读写次数
select sum(qs1.total_logical_reads)
from sys.dm_exec_query_stats as qs1
cross apply sys.dm_exec_sql_text(qs1.sql_handle) as st1
where sql_handle in (
select top 100 qs.sql_handle
from sys.dm_exec_query_stats as qs
cross apply sys.dm_exec_sql_text(qs.sql_handle) as st
order by qs.total_logical_reads desc)

--共执行多少条语句
select count(*)
from sys.dm_exec_query_stats as qs
cross apply sys.dm_exec_sql_text(qs.sql_handle)

--语句执行的总次数
select sum(qs.execution_count)
from sys.dm_exec_query_stats as qs
cross apply sys.dm_exec_sql_text(qs.sql_handle) as st

--前50条逻辑读写最大的语句
select top 50
qs.creation_time,qs.last_execution_time,qs.execution_count,qs.total_worker_time,
qs.total_physical_reads,qs.max_physical_reads,qs.total_logical_reads,qs.max_logical_reads,qs.min_logical_reads,
st.text
from sys.dm_exec_query_stats as qs
cross apply sys.dm_exec_sql_text(qs.sql_handle) as st
--where st.text like '%dv_topic%'
order by qs.total_logical_reads desc
select count(*) from flux_page

ECLARE
@USER_ID INT,
@KILLCMD VARCHAR(100),
@WAIT_TIME INT
-- 设置空闲时间
SET @WAIT_TIME = 2;
-- 根据选择条件,选择出应该取消的进程
DECLARE cur_lock CURSOR FOR
SELECT spid
FROM master..sysprocesses
WHERE
DATEDIFF(minute, last_batch, getdate()) > @WAIT_TIME
AND blocked = 0
AND spid IN
(SELECT blocked
FROM master..sysprocesses);
-- 取消所有选择出的进程。
OPEN cur_lock;
FETCH NEXT FROM cur_lock INTO @USER_ID;
WHILE @@FETCH_STATUS = 0
BEGIN
SET @KILLCMD = 'kill ' + CAST(@USER_ID AS VARCHAR)
EXECUTE (@KILLCMD);
FETCH NEXT FROM cur_lock INTO @USER_ID;
END;
CLOSE cur_lock;
DEALLOCATE cur_lock;

--查询锁信息
select * from sys.dm_tran_locks
--查询newbjx数据库中表的数量
select count(*) from newbjx..sysobjects where type='u' and status>=0
DECLARE
@USER_ID INT,
@KILLCMD VARCHAR(100),
@WAIT_TIME INT
-- 设置空闲时间
SET @WAIT_TIME = 2;
-- 根据选择条件,选择出应该取消的进程
DECLARE cur_lock CURSOR FOR
SELECT spid
FROM master..sysprocesses
WHERE
DATEDIFF(minute, last_batch, getdate()) > @WAIT_TIME
AND blocked = 0
AND spid IN
(SELECT blocked
FROM master..sysprocesses);
-- 取消所有选择出的进程。
OPEN cur_lock;
FETCH NEXT FROM cur_lock INTO @USER_ID;
WHILE @@FETCH_STATUS = 0
BEGIN
SET @KILLCMD = 'kill ' + CAST(@USER_ID AS VARCHAR)
EXECUTE (@KILLCMD);
FETCH NEXT FROM cur_lock INTO @USER_ID;
END;
CLOSE cur_lock;
DEALLOCATE cur_lock;

--下面的示例返回使会话 ID 与某一 Windows 线程 ID 相关联的信息。
--可以在 Windows 性能监视器中监视该线程的性能。该查询不返回当前正在休眠的会话 ID。

SELECT STasks.session_id, SThreads.os_thread_id
FROM sys.dm_os_tasks AS STasks
INNER JOIN sys.dm_os_threads AS SThreads
ON STasks.worker_address = SThreads.worker_address
WHERE STasks.session_id IS NOT NULL
ORDER BY STasks.session_id;
GO

set statistics io on
set statistics time on

--单次执行耗费CPU时间最长的查询计划
SELECT TOP 5 total_worker_time,last_worker_time,max_worker_time,min_worker_time,
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) AS statement_text
FROM sys.dm_exec_query_stats AS qs CROSS APPLY sys.dm_exec_sql_text(qs.sql_handle) AS st
ORDER BY max_worker_time DESC
--执行次数最多的查询计划
SELECT TOP 5 creation_time,last_execution_time,execution_count,
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) AS statement_text
FROM sys.dm_exec_query_stats AS qs CROSS APPLY sys.dm_exec_sql_text(qs.sql_handle) AS st
ORDER BY execution_count DESC

--谁阻塞了我
SELECT blocked_query.session_id AS blocked_session_id,
blocking_query.session_id AS blocking_session_id,
sql_text.text AS blocking_text,
waits.wait_type AS blocking_resource
FROM sys.dm_exec_requests AS blocked_query
INNER JOIN
sys.dm_exec_requests AS blocking_query
ON blocked_query.blocking_session_id=blocking_query.session_id
CROSS APPLY
(SELECT * FROM sys.dm_exec_sql_text(blocking_query.sql_handle)) AS sql_text
INNER JOIN
sys.dm_os_waiting_tasks AS waits
ON waits.session_id=blocking_query.session_id

--哪一组TSQL或过程占用了最多的CPU时间:

SELECT TOP 50 sum(qs.total_worker_time)/1000 AS total_cpu_time,
sum(qs.execution_count)/1000 AS total_execution_count,
count(*) AS number_of_statements,
qs.plan_handle,qs.sql_handle
FROM sys.dm_exec_query_stats qs
GROUP BY qs.plan_handle
ORDER BY sum(qs.total_worker_time) DESC

posted on 2011-11-28 16:05  账号难注册  阅读(395)  评论(0编辑  收藏  举报