1. 程式人生 > >sql效能查詢語句

sql效能查詢語句

查詢系統中最耗時的 SQL

select * from (
	select * from V$SQLSTATS
	
	-- 最耗時的 SQL
	-- ELAPSED_TIME 指的是總耗時(毫秒),平均耗時 = ELAPSED_TIME/EXECUTIONS
	-- order by ELAPSED_TIME DESC
	
	-- 查詢執行次數最多的 SQL
	-- order by EXECUTIONS DESC
	
	-- 讀硬碟最多的 SQL
	-- order by DISK_READS DESC
	
	-- 最費 CPU 的 SQL
	-- order by BUFFER_GETS DESC
) where rownum <=50;

查詢次數最多:

select *

from (select s.SQL_TEXT,

s.EXECUTIONS "執行次數",

s.PARSING_USER_ID "使用者名稱",

rank() over(order by EXECUTIONS desc) EXEC_RANK

from v$sql s

left join all_users u

on u.USER_ID = s.PARSING_USER_ID) t

where exec_rank <= 100;

注:rank() over是的作用是查出指定條件後進行一個排名

查詢執行最慢:

select *

from (select sa.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

from v$sqlarea sa

left join all_users u

on sa.PARSING_USER_ID = u.user_id

where sa.EXECUTIONS > 0

order by (sa.ELAPSED_TIME / sa.EXECUTIONS) desc)

where rownum <= 50;

查詢表鎖:

    SELECT l.session_id sid, s.serial#, l.locked_mode, l.oracle_username, s.user#,
    l.os_user_name,s.machine, s.terminal,a.sql_text, a.action
    FROM v$sqlarea a,v$session s, v$locked_object l
    WHERE l.session_id = s.sid
    AND s.prev_sql_addr = a.address
    ORDER BY sid, s.serial#;

查詢session

select distinct a.sid,b.SERIAL#,b.PROCESS,b.STATUS from v$session_wait a,v$session b
where a.SID=b.SID