1. 程式人生 > >檢視Oracle最耗時的SQL

檢視Oracle最耗時的SQL

有很多種方法可以用來找出哪些SQL語句需要優化,但是很久以來,最簡單的方法都是分析儲存在V$SQL檢視中的快取的SQL資訊。通過V$SQL檢視,可以確定具有高消耗時間、CUP和IO讀取的SQL語句。

1.檢視總消耗時間最多的前10條SQL語句
 select *
from (select v.sql_id,
v.child_number,
v.sql_text,
last_load_time,
v.PARSING_USER_ID,
ROUND(v.ELAPSED_TIME / 1000000 / (CASE
               WHEN (EXECUTIONS = 0 OR NVL(EXECUTIONS, 1 ) = 1) THEN
                1
               ELSE
                EXECUTIONS
             END),
             2) "執行時間'S'",
 v.SQL_FULLTEXT,
v.cpu_time,
v.disk_reads,
rank() over(order by v.elapsed_time desc) elapsed_rank
from v$sql v  ) a
where elapsed_rank <= 100  and   last_load_time > to_char(sysdate - 1/24, 'YYYY-MM-DD/HH:MI:SS')    order by "執行時間'S'" desc

查詢最近一小時內最慢的SQL:

select executions, cpu_time/1e6 as cpu_sec, elapsed_time/1e6 as elapsed_sec, round(elapsed_time/sqrt(executions)) as important, v.* 
from v$sql v 
where executions > 10 and last_load_time > to_char(sysdate - 1/24, 'YYYY-MM-DD/HH:MI:SS')  
order by important desc

2.檢視CPU消耗時間最多的前10條SQL語句
select *
from (select v.sql_id,
v.child_number,
v.sql_text,
v.elapsed_time,
v.cpu_time,
v.disk_reads,
rank() over(order by v.cpu_time desc) elapsed_rank
from v$sql v) a
where elapsed_rank <= 10;

3.檢視消耗磁碟讀取最多的前10條SQL語句
select *
from (select v.sql_id,
v.child_number,
v.sql_text,
v.elapsed_time,
v.cpu_time,
v.disk_reads,
rank() over(order by v.disk_reads desc) elapsed_rank
from v$sql v) a
where elapsed_rank <= 10;

一、查詢執行最慢的sql

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 select * from
(selectsa.SQL_TEXT, sa.SQL_FULLTEXT, sa.EXECUTIONS"執行次數", round(sa.ELAPSED_TIME / 1000000, 2)"總執行時間", round(sa.ELAPSED_TIME / 1000000 / sa.EXECUTIONS, 2)"平均執行時間", sa.COMMAND_TYPE, sa.PARSING_USER_ID"使用者ID", u.username"使用者名稱", sa.HASH_VALUE fromv$sqlarea sa leftjoinall_users u onsa.PARSING_USER_ID = u.user_id wheresa.EXECUTIONS > 0 orderby(sa.ELAPSED_TIME / sa.EXECUTIONS)desc) whererownum <= 50;

二、查詢次數最多的 sql

?
1 2 3 4 5 6 7 8 9 select * from(selects.SQL_TEXT, s.EXECUTIONS"執行次數", s.PARSING_USER_ID"使用者名稱", rank() over(orderbyEXECUTIONS desc) EXEC_RANK fromv$sql s leftjoinall_users u onu.USER_ID = s.PARSING_USER_ID) t whereexec_rank <= 100;