查詢oracle效能SQL

huangdazhu發表於2014-04-01

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

1.檢視總消耗時間最多的前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.elapsed_time desc) elapsed_rank
from v$sql v) a
where elapsed_rank <= 10;

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;

4、檢視消耗記憶體多的sql
select b.username,        a. buffer_gets,        a.executions,        a.disk_reads / decode(a.executions, 0, 1, a.executions),        a.sql_text SQL   from v$sqlarea a, dba_users b  where a.parsing_user_id = b.user_id    and a.disk_reads > 10000  order by disk_reads desc; 
5、檢視邏輯讀多的SQL
select* from(select buffer_gets, sql_text          from v$sqlarea         where buffer_gets>500000         order by buffer_gets desc) where rownum<=30; 
6、檢視執行次數多的SQL
select sql_text, executions   from (select sql_text, executions from v$sqlarea order by executions desc)  where rownum < 81; 
7、檢視讀硬碟多的SQL
select sql_text, disk_reads from(select sql_text, disk_reads from v$sqlarea order by disk_reads desc) where rownum<21; 
8、檢視排序多的SQL
select sql_text, sorts from(select sql_text, sorts from v$sqlarea order by sorts desc) where rownum<21; 
9、分析的次數太多,執行的次數太少,要用綁變數的方法來寫sql
select substr(sql_text, 1, 80) "sql", count(*), sum(executions) "totexecs"
 from v$sqlarea  where executions < 5  group by substr(sql_text, 1, 80)
having count(*) > 30  order by 2; 




來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/28869493/viewspace-1133941/,如需轉載,請註明出處,否則將追究法律責任。

相關文章