獲得消耗cpu較高的topsql

2021-07-12 00:22:29 字數 2715 閱讀 6708



一、select a.snap_id,

a.dbid,

a.instance_number,

db_time,

to_char(trunc((db_time - lag(db_time, 1, db_time)

over(partition by stat_name,

instance_number order by snap_id)) / 1000000 / 60,

2)) db_time_

from (select a.snap_id,

a.dbid,

a.instance_number,

a.stat_name,

sum(a.value) db_time

from dba_hist_sys_time_model a

where a.stat_name = 'db time'

--and a.instance_number = 2

group by a.snap_id, a.dbid, a.instance_number, a.stat_name) a;

select a.snap_id,

a.dbid,

a.instance_number,

db_time,

/*    to_char(trunc((db_time - lag(db_time, 1, db_time)

over(partition by stat_name,

instance_number order by snap_id)) / 1000000 / 60,

2)) db_time_,   */

b.end_interval_time

from (select a.snap_id,

a.dbid,

a.instance_number,

a.stat_name,

sum(a.value) db_time

from dba_hist_sys_time_model a

where a.stat_name = 'db time'

group by a.snap_id, a.dbid, a.instance_number, a.stat_name) a,dba_hist_snapshot b where a.snap_id=b.snap_id;

執行上面兩個語句得到資料之後按照snap_id對應起來,可以得到snap_id、db_time_、end_interval_time這仨個字段,就可以確定某個時間點的db_time值與snap_id所對應的具體的時間。

然後把相應時間段的snap_id代入下面的語句即可得到top10的語句

select  to_char(t.instance_number) i_n,t.sql_id,to_char(t.username) username,

to_char(t.disk_reads) d_r,to_char(t.buffer_gets_nvl) buffer_nvl,t.bget buffer_gets,t.elap_time,t.cpu_time,

t.exec,t.elap_nvl,t.sql_text

from

(select  sqt.bget,

sqt.exec,

username,

instance_number,

disk_reads,

decode(sqt.exec, 0, to_number(null) , (sqt.bget / sqt.exec)) buffer_gets_nvl,

nvl((sqt.cput / 1000000), to_number(null)) cpu_time ,

nvl((sqt.elap / 1000000), to_number(null)) elap_time,

(sqt.elap / 1000000) /  sqt.exec  elap_nvl,

sqt.sql_id,

st.sql_text

from

(select sql_id,parsing_schema_name username,instance_number,

sum(buffer_gets_delta) bget,

sum(executions_delta) exec,

sum(cpu_time_delta) cput,

sum(elapsed_time_delta) elap,

sum(disk_reads_delta) disk_reads

from dba_hist_sqlstat

where snap_id>=30281 and snap_id<=30307   ---------------把相關時間點的snap_id寫到這裡即可

group by sql_id,parsing_schema_name,instance_number) sqt,

dba_hist_sqltext st

where st.sql_id(+) = sqt.sql_id

and sqt.exec != 0

and st.sql_text not like '%dual%%'

order by nvl((sqt.cput / 1000000), to_number(null)) desc, sqt.sql_id ,username,instance_number,disk_reads)  t

where rownum < 65

and (rownum <= 10);

sqlserver定位消耗資源比較高的sql

sqlserver檢視消耗資源的sql 具體如下 可以使用sql 快速定位資料庫裡cpu,io 消耗比較高的sql 1.定位消耗cpu,io 最高的程序資訊 selectspid,dbid,cpu,physical io,login time,last batch,status,hostname,p...

造成高CPU消耗的原因

1.索引的缺失 根據執行計畫以及追蹤,確定缺少的索引,新增索引 2.統計資訊過時 統計資訊過時,執行計畫就會不是最優的,可能產生大量的表掃瞄,需要更新統計資訊 update statistics table or indexed view name 3.where子句中使用函式 where 子句中使...

VKTM程序消耗大量CPU的問題

11g中引入了vktm後台程序,vktm是virtual keeper of time的縮寫,該程序負責提供時鐘時間 每秒更新一次 以及參考時間服務 每20ms更新一次,僅在程序高優先順序情況下可用 該參考時間服務用於各種基於時間間隔的度量。vktm在sga中發布這些計時資訊,以便各種rdbms c...