怎么找到找出哪个SQL语句导致cpu占用如此高
2个回答
推荐于2018-08-23 · 知道合伙人软件行家
关注
展开全部
一般我们可以使用sql server自带的性能分析追踪工具sql profiler分析数据库设计所产生问题的来源,进行有针对性的处理。但我们也可以通过自己写SQL语句来有针对性的进行性能方面的查询。通常会用到如下三个系统视图:sys.sysprocesses ,dm_exec_sessions ,dm_exec_requests
--一、查看当前的数据库用户连接有多少
USE master
GO
SELECT *
FROM sys.[sysprocesses]
WHERE [spid] > 50
--AND DB_NAME([dbid])='gposdb'
SELECT COUNT(*)
FROM [sys].[dm_exec_sessions]
WHERE [session_id] > 50
--二、选取前10个最耗CPU时间的会话
SELECT TOP 10
[session_id] ,
[request_id] ,
[start_time] AS '开始时间' ,
[status] AS '状态' ,
[command] AS '命令' ,
dest.[text] AS 'sql语句' ,
DB_NAME([database_id]) AS '数据库名' ,
[blocking_session_id] AS '正在阻塞其他会话的会话ID' ,
[wait_type] AS '等待资源类型' ,
[wait_time] AS '等待时间' ,
[wait_resource] AS '等待的资源' ,
[reads] AS '物理读次数' ,
[writes] AS '写次数' ,
[logical_reads] AS '逻辑读次数' ,
[row_count] AS '返回结果行数'
FROM sys.[dm_exec_requests] AS der
CROSS APPLY sys.[dm_exec_sql_text](der.[sql_handle]) AS dest
WHERE [session_id] > 50
AND DB_NAME(der.[database_id]) = 'gposdb'
ORDER BY [cpu_time] DESC
--三、查询前10个最耗CPU时间的SQL语句
SELECT TOP 10
dest.[text] AS 'sql语句'
FROM sys.[dm_exec_requests] AS der
CROSS APPLY sys.[dm_exec_sql_text](der.[sql_handle]) AS dest
WHERE [session_id] > 50
ORDER BY [cpu_time] DESC
--四、查询会话中有多少个worker在等待
SELECT TOP 10
[session_id] ,
[request_id] ,
[start_time] AS '开始时间' ,
[status] AS '状态' ,
[command] AS '命令' ,
dest.[text] AS 'sql语句' ,
DB_NAME([database_id]) AS '数据库名' ,
[blocking_session_id] AS '正在阻塞其他会话的会话ID' ,
der.[wait_type] AS '等待资源类型' ,
[wait_time] AS '等待时间' ,
[wait_resource] AS '等待的资源' ,
[dows].[waiting_tasks_count] AS '当前正在进行等待的任务数' ,
[reads] AS '物理读次数' ,
[writes] AS '写次数' ,
[logical_reads] AS '逻辑读次数' ,
[row_count] AS '返回结果行数'
FROM sys.[dm_exec_requests] AS der
INNER JOIN [sys].[dm_os_wait_stats] AS dows ON der.[wait_type] = [dows].[wait_type]
CROSS APPLY sys.[dm_exec_sql_text](der.[sql_handle]) AS dest
WHERE [session_id] > 50
ORDER BY [cpu_time] DESC
--五、查询CPU占用高的语句
SELECT TOP 10
total_worker_time / execution_count AS avg_cpu_cost ,
plan_handle ,
execution_count ,
( SELECT SUBSTRING(text, statement_start_offset / 2 + 1,
( CASE WHEN statement_end_offset = -1
THEN LEN(CONVERT(NVARCHAR(MAX), text))
* 2
ELSE statement_end_offset
END - statement_start_offset ) / 2)
FROM sys.dm_exec_sql_text(sql_handle)
) AS query_text
FROM sys.dm_exec_query_stats
ORDER BY [avg_cpu_cost] DESC
--一、查看当前的数据库用户连接有多少
USE master
GO
SELECT *
FROM sys.[sysprocesses]
WHERE [spid] > 50
--AND DB_NAME([dbid])='gposdb'
SELECT COUNT(*)
FROM [sys].[dm_exec_sessions]
WHERE [session_id] > 50
--二、选取前10个最耗CPU时间的会话
SELECT TOP 10
[session_id] ,
[request_id] ,
[start_time] AS '开始时间' ,
[status] AS '状态' ,
[command] AS '命令' ,
dest.[text] AS 'sql语句' ,
DB_NAME([database_id]) AS '数据库名' ,
[blocking_session_id] AS '正在阻塞其他会话的会话ID' ,
[wait_type] AS '等待资源类型' ,
[wait_time] AS '等待时间' ,
[wait_resource] AS '等待的资源' ,
[reads] AS '物理读次数' ,
[writes] AS '写次数' ,
[logical_reads] AS '逻辑读次数' ,
[row_count] AS '返回结果行数'
FROM sys.[dm_exec_requests] AS der
CROSS APPLY sys.[dm_exec_sql_text](der.[sql_handle]) AS dest
WHERE [session_id] > 50
AND DB_NAME(der.[database_id]) = 'gposdb'
ORDER BY [cpu_time] DESC
--三、查询前10个最耗CPU时间的SQL语句
SELECT TOP 10
dest.[text] AS 'sql语句'
FROM sys.[dm_exec_requests] AS der
CROSS APPLY sys.[dm_exec_sql_text](der.[sql_handle]) AS dest
WHERE [session_id] > 50
ORDER BY [cpu_time] DESC
--四、查询会话中有多少个worker在等待
SELECT TOP 10
[session_id] ,
[request_id] ,
[start_time] AS '开始时间' ,
[status] AS '状态' ,
[command] AS '命令' ,
dest.[text] AS 'sql语句' ,
DB_NAME([database_id]) AS '数据库名' ,
[blocking_session_id] AS '正在阻塞其他会话的会话ID' ,
der.[wait_type] AS '等待资源类型' ,
[wait_time] AS '等待时间' ,
[wait_resource] AS '等待的资源' ,
[dows].[waiting_tasks_count] AS '当前正在进行等待的任务数' ,
[reads] AS '物理读次数' ,
[writes] AS '写次数' ,
[logical_reads] AS '逻辑读次数' ,
[row_count] AS '返回结果行数'
FROM sys.[dm_exec_requests] AS der
INNER JOIN [sys].[dm_os_wait_stats] AS dows ON der.[wait_type] = [dows].[wait_type]
CROSS APPLY sys.[dm_exec_sql_text](der.[sql_handle]) AS dest
WHERE [session_id] > 50
ORDER BY [cpu_time] DESC
--五、查询CPU占用高的语句
SELECT TOP 10
total_worker_time / execution_count AS avg_cpu_cost ,
plan_handle ,
execution_count ,
( SELECT SUBSTRING(text, statement_start_offset / 2 + 1,
( CASE WHEN statement_end_offset = -1
THEN LEN(CONVERT(NVARCHAR(MAX), text))
* 2
ELSE statement_end_offset
END - statement_start_offset ) / 2)
FROM sys.dm_exec_sql_text(sql_handle)
) AS query_text
FROM sys.dm_exec_query_stats
ORDER BY [avg_cpu_cost] DESC
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
有一个程序在运行,期间一直在调用mysql数据库做一些工作.数据没经过特别的配置等优化工作.
程序并没有特别耗费资源的数据库调用,一般一秒中最多也就是几十次数据库访问,有时候甚至更低,几次而已,数据库表中也无过多记录的表,除了一个有4000多条记录的表外,其他表中记录不足几百条.
今日对这个运行的程序进行了更新,不知道从啥时候开始,突然发现cpu占用达到40-70%,感觉很异常,但却发现是mysql-nt这个进行占用了一大半.
一时没想出好办法,于是在任务管理器中查看mysql-nt进程的"I/O读取"和"I/O写入项",发现"I/O读取"大致每几秒产生数万次甚至数十万次,本人认为这个数据过大,说明访问数据库太过频繁导致cpu占用急剧增加.
------解决方案--------------------------------------------------------
不清楚,帮顶
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询