首页 > 代码库 > SQL SERVER 集合

SQL SERVER 集合

死锁和堵塞一直是性能测试执行中关注的重点。

下面是我整理的监控sql server数据库,在性能测试过程中是否出现死锁、堵塞的SQL语句,还算比较准备,留下来备用。

--每秒死锁数量SELECT  *FROM    sys.dm_os_performance_countersWHERE   counter_name LIKE ‘Number of Deadlocksc%‘;--查询当前阻塞WITH    CTE_SID ( BSID, SID, sql_handle )          AS ( SELECT   blocking_session_id ,                        session_id ,                        sql_handle               FROM     sys.dm_exec_requests               WHERE    blocking_session_id <> 0               UNION ALL               SELECT   A.blocking_session_id ,                        A.session_id ,                        A.sql_handle               FROM     sys.dm_exec_requests A                        JOIN CTE_SID B ON A.SESSION_ID = B.BSID             )    SELECT  C.BSID ,            C.SID ,            S.login_name ,            S.host_name ,            S.status ,            S.cpu_time ,            S.memory_usage ,            S.last_request_start_time ,            S.last_request_end_time ,            S.logical_reads ,            S.row_count ,            q.text    FROM    CTE_SID C             JOIN sys.dm_exec_sessions S ON C.sid = s.session_id            CROSS APPLY sys.dm_exec_sql_text(C.sql_handle) Q    ORDER BY sid

在压力测试过程中,不间断的按F5键执行上面的SQL语句,如果出现死锁或者堵塞现象,就会在执行结果中罗列出来。如果每次连续执行SQL,都有死锁或者堵塞出现,说明死锁或者堵塞的比较严重。

 

查看SQLServer最耗资源时间的SQL语句

执行最慢的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 ‘?tch%‘
ORDER BY
total_elapsed_time / execution_count DESC;
 
--总耗CPU最多的前个SQL:
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


--平均耗CPU最多的前个SQL:
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

SQL SERVER 集合