龙空技术网

数据库笔试面试136——如何查找最近1分钟内,最消耗CPU的会话

DBA宝典 213

前言:

当前大家对“oracle查看cpu消耗”都比较关注,兄弟们都需要学习一些“oracle查看cpu消耗”的相关内容。那么小编在网络上网罗了一些有关“oracle查看cpu消耗””的相关知识,希望我们能喜欢,各位老铁们快快来学习一下吧!

在Oracle中,如何查找最近1分钟内,最消耗CPU的SQL语句及会话信息?

答案如下所示:

可以根据V$ACTIVE_SESSION_HISTORY视图来获取,如下所示:

① 查找最近一分钟内,最消耗CPU的SQL语句:

SELECT ASH.INST_ID,

ASH.SQL_ID,

(SELECT VS.SQL_TEXT

FROM GV$SQLAREA VS

WHERE VS.SQL_ID = ASH.SQL_ID

AND ASH.INST_ID = VS.INST_ID) SQL_TEXT,

ASH.SQL_CHILD_NUMBER,

ASH.SQL_OPNAME,

ASH.SESSION_INFO,

COUNTS,

PCTLOAD * 100 || '%' PCTLOAD

FROM (SELECT ASH.INST_ID,

ASH.SQL_ID,

ASH.SQL_CHILD_NUMBER,

ASH.SQL_OPNAME,

(ASH.MODULE || '--' || ASH.ACTION || '--' || ASH.PROGRAM || '--' ||

ASH.MACHINE || '--' || ASH.CLIENT_ID || '--' ||

ASH.SESSION_TYPE) SESSION_INFO,

COUNT(*) COUNTS,

ROUND(COUNT(*) / SUM(COUNT(*)) OVER(), 2) PCTLOAD,

DENSE_RANK() OVER(ORDER BY COUNT(*) DESC) RANK_ORDER

FROM GV$ACTIVE_SESSION_HISTORY ASH

WHERE ASH.SESSION_TYPE <> 'BACKGROUND'

AND ASH.SESSION_STATE = 'ON CPU'

AND SAMPLE_TIME > SYSDATE - 1 / (24 * 60)

GROUP BY ASH.INST_ID,

ASH.SQL_ID,

ASH.SQL_CHILD_NUMBER,

ASH.SQL_OPNAME,

(ASH.MODULE || '--' || ASH.ACTION || '--' || ASH.PROGRAM || '--' ||

ASH.MACHINE || '--' || ASH.CLIENT_ID || '--' ||

ASH.SESSION_TYPE)) ASH

WHERE RANK_ORDER <= 10

ORDER BY COUNTS DESC;

② 查找最近一分钟内,最消耗CPU的会话:

SELECT SESSION_ID,

COUNT(*)

FROM V$ACTIVE_SESSION_HISTORY V

WHERE V.SESSION_STATE = 'ON CPU'

AND V.SAMPLE_TIME > SYSDATE - 10/ (24 * 60)

GROUP BY SESSION_ID

ORDER BY COUNT(*) DESC;

③ 查找最近一分钟内,最消耗I/O的SQL语句:

SELECT ASH.INST_ID,

ASH.SQL_ID,

(SELECT VS.SQL_TEXT

FROM GV$SQLAREA VS

WHERE VS.SQL_ID = ASH.SQL_ID

AND ASH.INST_ID = VS.INST_ID) SQL_TEXT,

ASH.SQL_CHILD_NUMBER,

ASH.SQL_OPNAME,

ASH.SESSION_INFO,

COUNTS,

PCTLOAD * 100 || '%' PCTLOAD

FROM (SELECT ASH.INST_ID,

ASH.SQL_ID,

ASH.SQL_CHILD_NUMBER,

ASH.SQL_OPNAME,

(ASH.MODULE || '--' || ASH.ACTION || '--' || ASH.PROGRAM || '--' ||

ASH.MACHINE || '--' || ASH.CLIENT_ID || '--' ||

ASH.SESSION_TYPE) SESSION_INFO,

COUNT(*) COUNTS,

ROUND(COUNT(*) / SUM(COUNT(*)) OVER(), 2) PCTLOAD,

DENSE_RANK() OVER(ORDER BY COUNT(*) DESC) RANK_ORDER

FROM GV$ACTIVE_SESSION_HISTORY ASH

WHERE ASH.SESSION_TYPE <> 'BACKGROUND'

AND ASH.SESSION_STATE = 'WAITING'

AND ASH.SAMPLE_TIME > SYSDATE - 1 / (24 * 60)

AND ASH.WAIT_CLASS = 'USER I/O'

GROUP BY ASH.INST_ID,

ASH.SQL_ID,

ASH.SQL_CHILD_NUMBER,

ASH.SQL_OPNAME,

(ASH.MODULE || '--' || ASH.ACTION || '--' || ASH.PROGRAM || '--' ||

ASH.MACHINE || '--' || ASH.CLIENT_ID || '--' ||

ASH.SESSION_TYPE)) ASH

WHERE RANK_ORDER <= 10

ORDER BY COUNTS DESC;

④ 查找最近一分钟内,最消耗资源的SQL语句:

SELECT ASH.INST_ID,

ASH.SQL_ID,

(SELECT VS.SQL_TEXT

FROM GV$SQLAREA VS

WHERE VS.SQL_ID = ASH.SQL_ID

AND ASH.INST_ID = VS.INST_ID) SQL_TEXT,

ASH.SQL_CHILD_NUMBER,

ASH.SQL_OPNAME,

ASH.SESSION_INFO,

COUNTS,

PCTLOAD * 100 || '%' PCTLOAD

FROM (SELECT ASH.INST_ID,

ASH.SQL_ID,

ASH.SQL_CHILD_NUMBER,

ASH.SQL_OPNAME,

(ASH.MODULE || '--' || ASH.ACTION || '--' || ASH.PROGRAM || '--' ||

ASH.MACHINE || '--' || ASH.CLIENT_ID || '--' ||

ASH.SESSION_TYPE) SESSION_INFO,

COUNT(*) COUNTS,

ROUND(COUNT(*) / SUM(COUNT(*)) OVER(), 2) PCTLOAD,

DENSE_RANK() OVER(ORDER BY COUNT(*) DESC) RANK_ORDER

FROM GV$ACTIVE_SESSION_HISTORY ASH

WHERE ASH.SESSION_TYPE <> 'BACKGROUND'

AND ASH.SESSION_STATE = 'WAITING'

AND ASH.SAMPLE_TIME > SYSDATE - 1 / (24 * 60)

AND ASH.WAIT_CLASS = 'USER I/O'

GROUP BY ASH.INST_ID,

ASH.SQL_ID,

ASH.SQL_CHILD_NUMBER,

ASH.SQL_OPNAME,

(ASH.MODULE || '--' || ASH.ACTION || '--' || ASH.PROGRAM || '--' ||

ASH.MACHINE || '--' || ASH.CLIENT_ID || '--' ||

ASH.SESSION_TYPE)) ASH

WHERE RANK_ORDER <= 10

ORDER BY COUNTS DESC;

⑤ 查找最近一分钟内,最消耗资源的会话:

SELECT ASH.SESSION_ID,

ASH.SESSION_SERIAL#,

ASH.USER_ID,

ASH.PROGRAM,

SUM(DECODE(ASH.SESSION_STATE, 'ON CPU', 1, 0)) "CPU",

SUM(DECODE(ASH.SESSION_STATE, 'WAITING', 1, 0)) -

SUM(DECODE(ASH.SESSION_STATE,

'WAITING',

DECODE(ASH.WAIT_CLASS, 'USER I/O', 1, 0),

0)) "WAITING",

SUM(DECODE(ASH.SESSION_STATE,

'WAITING',

DECODE(ASH.WAIT_CLASS, 'USER I/O', 1, 0),

0)) "IO",

SUM(DECODE(ASH.SESSION_STATE, 'ON CPU', 1, 1)) "TOTAL"

FROM V$ACTIVE_SESSION_HISTORY ASH

WHERE ASH.SAMPLE_TIME > SYSDATE - 1 / (24 * 60)

GROUP BY ASH.SESSION_ID, ASH.USER_ID, ASH.SESSION_SERIAL#, ASH.PROGRAM

ORDER BY SUM(DECODE(ASH.SESSION_STATE, 'ON CPU', 1, 1));

&说明:

有关SQL监控的更多内容可以参考我的BLOG:。

标签: #oracle查看cpu消耗