sql_util_消耗資源

zgy13121發表於2008-10-18

Select * From v$sqlarea Order By cpu_time Desc

--最耗資源 SELECT SQL_TEXT,hash_value, executions, buffer_gets, disk_reads, parse_calls FROM V$SQLAREA WHERE buffer_gets > 10000000 OR disk_reads > 1000000 ORDER BY buffer_gets + 100 * disk_reads DESC;

--其中包括 Database Wait Time Ratio(資料庫等待時間比)

--和 Database CPU Time Ratio(資料庫 CPU 時間比)這兩個時間度量

select METRIC_NAME, VALUE

from SYS.V_$SYSMETRIC

where METRIC_NAME IN ('Database CPU Time Ratio', 'Database Wait Time Ratio') AND INTSIZE_CSEC = (select max(INTSIZE_CSEC) from SYS.V_$SYSMETRIC);

--還可以使用以下查詢快速瞭解資料庫在前一個小時執行時整體效能是否下降過:

select end_time, value from sys.v_$sysmetric_history where metric_name = 'Database CPU Time Ratio' order by 1;

--查詢 V$SYSMETRIC_SUMMARY 檢視以充分了解整個資料庫效率的最小值、最大值和平均值:

select CASE METRIC_NAME WHEN 'SQL Service Response Time' then 'SQL Service Response Time (secs)' WHEN 'Response Time Per Txn' then 'Response Time Per Txn (secs)' ELSE METRIC_NAME END METRIC_NAME, CASE METRIC_NAME WHEN 'SQL Service Response Time' then ROUND((MINVAL / 100),2) WHEN 'Response Time Per Txn' then ROUND((MINVAL / 100),2) ELSE MINVAL END MININUM, CASE METRIC_NAME WHEN 'SQL Service Response Time' then ROUND((MAXVAL / 100),2) WHEN 'Response Time Per Txn' then ROUND((MAXVAL / 100),2) ELSE MAXVAL END MAXIMUM, CASE METRIC_NAME WHEN 'SQL Service Response Time' then ROUND((AVERAGE / 100),2) WHEN 'Response Time Per Txn' then ROUND((AVERAGE / 100),2) ELSE AVERAGE END AVERAGE from SYS.V_$SYSMETRIC_SUMMARY where METRIC_NAME in ('CPU Usage Per Sec', 'CPU Usage Per Txn', 'Database CPU Time Ratio', 'Database Wait Time Ratio', 'Executions Per Sec', 'Executions Per Txn', 'Response Time Per Txn', 'SQL Service Response Time', 'User Transaction Per Sec') ORDER BY 1

--如果響應時間比所需的時間長,則資料庫管理員需要了解哪些使用者活動型別使資料庫執行如此困難

select case db_stat_name when 'parse time elapsed' then 'soft parse time' else db_stat_name end db_stat_name, case db_stat_name when 'sql execute elapsed time' then time_secs - plsql_time when 'parse time elapsed' then time_secs - hard_parse_time else time_secs end time_secs, case db_stat_name when 'sql execute elapsed time' then round(100 * (time_secs - plsql_time) / db_time,2) when 'parse time elapsed' then round(100 * (time_secs - hard_parse_time) / db_time,2) else round(100 * time_secs / db_time,2) end pct_time from (select stat_name db_stat_name, round((value / 1000000),3) time_secs from sys.v_$sys_time_model where stat_name not in('DB time','background elapsed time', 'background cpu time','DB CPU')), (select round((value / 1000000),3) db_time from sys.v_$sys_time_model where stat_name = 'DB time'), (select round((value / 1000000),3) plsql_time from sys.v_$sys_time_model where stat_name = 'PL/SQL execution elapsed time'), (select round((value / 1000000),3) hard_parse_time from sys.v_$sys_time_model where stat_name = 'hard parse elapsed time') order by 2 desc;

--除活動時間外,資料庫管理員還需要知道全域性等待時間。

--在 Oracle 資料庫 10g 之前,資料庫管理員必須檢視一個一個的等待事件來了解等待和瓶頸,

--但 Oracle 現在透過等待類為等待提供了一個摘要/彙總機制:

select WAIT_CLASS, TOTAL_WAITS, round(100 * (TOTAL_WAITS / SUM_WAITS),2) PCT_WAITS, ROUND((TIME_WAITED / 100),2) TIME_WAITED_SECS, round(100 * (TIME_WAITED / SUM_TIME),2) PCT_TIME

from

(select WAIT_CLASS, TOTAL_WAITS, TIME_WAITED from V$SYSTEM_WAIT_CLASS where WAIT_CLASS != 'Idle'),

(select sum(TOTAL_WAITS) SUM_WAITS, sum(TIME_WAITED) SUM_TIME from V$SYSTEM_WAIT_CLASS

where WAIT_CLASS != 'Idle')

order by 5 desc;

--現在揭示批次總等待時間是由使用者 I/O 等待而導致要比嘗試和測量單個等待事件來獲得全域性映像要容易得多。

--與響應時間度量一樣,您還可以使用如下所示的查詢按時間回顧上一小時的情況:

select to_char(a.end_time,'DD-MON-YYYY HH:MI:SS') end_time, b.wait_class, round((a.time_waited / 100),2) time_waited

from sys.v_$waitclassmetric_history a, sys.v_$system_wait_class b where a.wait_class# = b.wait_class# and b.wait_class != 'Idle'

order by 1,2;

--當然,您可以使用 V$SESS_TIME_MODEL 檢視只專注於一個 SID,並獲取會話的所有統計資料。

--還可以使用以下查詢檢視使用新等待類的當前會話:

select a.sid, b.username, a.wait_class, a.total_waits, round((a.time_waited / 100),2) time_waited_secs

from sys.v_$session_wait_class a, sys.v_$session b

where b.sid = a.sid and b.username is not null and a.wait_class != 'Idle' order by 5 desc;

/* 在 Oracle 資料庫 10g 中,已向 V$SQLAREA 中新增了六個新的與等待相關的時間列。 APPLICATION_WAIT_TIME CONCURRENCY_WAIT_TIME CLUSTER_WAIT_TIME USER_IO_WAIT_TIME PLSQL_EXEC_TIME JAVA_EXEC_TIME */

--一個可用於查詢具有最高使用者 I/O 等待的前n個 SQL 語句可以是:

select * from (select sql_text, sql_id, elapsed_time, cpu_time, user_io_wait_time from sys.v_$sqlarea order by 5 desc) where rownum <200 order by sql select from b where and and> 0;

[@more@]

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

相關文章