-->尋找低效的SQL語句 ,下麵的語句主要適用於從視圖v$sqlarea中獲得當前運行下且耗用buffer_gets較多的SQL語句 SELECT executions , disk_reads , buffer_gets , ROUND( ( buffer_gets - disk_reads ) ...
-->尋找低效的SQL語句 ,下麵的語句主要適用於從視圖v$sqlarea中獲得當前運行下且耗用buffer_gets較多的SQL語句
SELECT executions
, disk_reads
, buffer_gets
, ROUND( ( buffer_gets
- disk_reads )
/ buffer_gets, 2 )
hit_ratio
, ROUND( disk_reads / executions, 2 ) reads_per_run
, sql_text
FROM v$sqlarea
WHERE executions > 0
AND buffer_gets > 0
AND ( buffer_gets
- disk_reads )
/ buffer_gets < 0.80
ORDER BY 4 DESC;
--查詢低效的sql
SELECT EXECUTIONS, DISK_READS, BUFFER_GETS,
ROUND ((BUFFER_GETS-DISK_READS)/BUFFER_GETS, 2) Hit_radio,
ROUND (DISK_READS/EXECUTIONS, 2) Reads_per_run,
SQL_TEXT
FROM V$SQLAREA
WHERE EXECUTIONS>0
AND BUFFER_GETS > 0
AND (BUFFER_GETS-DISK_READS)/BUFFER_GETS < 0.8
ORDER BY 4 DESC;
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/1440, '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/1440, '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
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;
二、查詢次數最多的 sql
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;
select * from v$sql_monitor where sql_text is not null;
select * from v$sql t
where t.LAST_ACTIVE_TIME>(sysdate - interval '1' MINUTE) --執行1分鐘內的SQL語句
-- and t.PARSING_SCHEMA_NAME = 'LSBIKE' --資料庫
-- and (t.MODULE is null or t.MODULE not like '%PL/SQL%') --不是在某些終端里的執行
-- and lower(t.SQL_TEXT) like '%select%' --查詢某類SQL語句
order by t.LAST_ACTIVE_TIME desc;
--TOP SQL
select *
from (select round(nvl((sqt.elap / 1000000), to_number(null)),2) "Elapsed Time (s)",
round( nvl((sqt.cput / 1000000), to_number(null)),2) "CPU Time (s)",
sqt.exec,
round(decode(sqt.exec,
0,
to_number(null),
(sqt.elap / sqt.exec / 1000000)),2) "Elap per Exec (s)",
round((100 *
(sqt.elap / (select sum(e.value) - sum(b.value)
from dba_hist_sys_time_model b,
dba_hist_sys_time_model e
where b.snap_id = &beg_snap and
e.snap_id = &end_snap and
b.dbid = &dbid and
e.dbid = &dbid and
b.instance_number = &inst_num and
e.instance_number = &inst_num and
e.stat_name = 'DB time' and
b.stat_name = 'DB time'))) ,2)norm_val,
sqt.sql_id,
decode(sqt.module, null, null, 'Module: ' || sqt.module) SqlModule,
nvl(to_nchar(SUBSTR(st.sql_text,1,2000)) , (' ** SQL Text Not Available ** ')) SqlText
from (select sql_id,
max(module) module,
sum(elapsed_time_delta) elap,
sum(cpu_time_delta) cput,
sum(executions_delta) exec
from dba_hist_sqlstat
where dbid = &dbid and
instance_number = &inst_num and
snap_id > &beg_snap and
snap_id <= &end_snap
group by sql_id) sqt,
dba_hist_sqltext st
where st.sql_id(+) = sqt.sql_id and
st.dbid(+) = &dbid
order by nvl(sqt.elap, -1) desc,
sqt.sql_id)
where rownum < 65 and
(rownum <= 10 or norm_val > 1);
--
select a.EVENT, a.SQL_ID, a.MACHINE, b.SQL_TEXT, b.SQL_FULLTEXT,b.FIRST_LOAD_TIME,b.LAST_LOAD_TIME,b.LAST_ACTIVE_TIME
from v$session a, v$sql b
where a.SQL_ID = b.SQL_ID
and a.USERNAME is not null
and a.STATUS = 'ACTIVE';
--查看正在執行的sql執行計劃
--display_cursor 為第一步查出來的SQL_ID
select * from table(dbms_xplan.display_cursor('7k0dhtw1zudrw'))
--等待事件以及語句情況
select event,sql_id, mi, count(mi)
from (select substrb(event,1,30) event, sql_id, to_char(sample_time, 'yyyymmdd hh24mi') mi --,
--session_id
from dba_hist_active_sess_history
where sql_id = '5s1x1tmt570pn'
and sample_time > to_date('20190513 0940', 'yyyymmdd hh24mi')
and sample_time < to_date('20190513 1910', 'yyyymmdd hh24mi'))
group by event,sql_id, mi
order by mi ;
enq: SQ - contention
5s1x1tmt570pn SELECT ( TO_CHAR(SYSDATE, 'YYYYMMDDHH24miss') || 410299 || LPAD(SEQ_BKE010.NEXTVAL, 10, '0') ) FROM DUAL
7pycct8f0sur2 SELECT ( TO_CHAR(SYSDATE, 'YYYYMMDDHH24miss') || 410299 || LPAD(SEQ_AAZ217.NEXTVAL, 10, '0') ) FROM DUAL
2bxcdvtcvykcv SELECT ( TO_CHAR(SYSDATE, 'YYYYMMDDHH24miss') || 410200 || LPAD(SEQ_BKZ522.NEXTVAL, 10, '0') ) FROM DUAL
--看等待事件的
SELECT t2.sid,
t2.SERIAL#,
t1.SPID OS_PID,
t3.SQL_ID,
t2.EVENT,
t2.P1TEXT,
t2.P1,
t2.p2TEXT,
t2.P2,
t2.p3TEXT,
t2.P3,
t3.SQL_FULLTEXT
FROM v$process t1,
v$session t2,
v$sql t3
WHERE t1.ADDR = t2.PADDR
AND t2.STATUS = 'ACTIVE' -- 'INACTIVE'
and t2.SQL_ID = t3.SQL_ID
and t2.event not like 'SQL%';
select t2.sid, t2.SERIAL#, -- 庫級唯一定位一個session t1.SPID OS_PID, -- 操作系統的PID t3.SQL_ID, t2.EVENT,-- SQLID和等待事件 t2.P1TEXT, t2.P1, -- 等待事件的P1信息 t2.p2TEXT, t2.P2, -- 等待事件的P2信息 t2.p3TEXT, t2.P3, -- 等待事件的P3信息 t3.SQL_FULLTEXT -- 被執行的SQL完整內容 from v$process t1, v$session t2, v$sql t3 where t1.ADDR = t2.PADDR and t2.STATUS = 'ACTIVE' -- 表示當前正在執行SQL的會話
-- and t2.STATUS = 'INACTIVE'
-- 表示當前等待執行SQL的會話
-- and t2.STATUS = 'KILLED'
-- 表示當前會話正在被殺掉,未提交事務強制回滾
and t2.SQL_ID = t3.SQL_ID; -- t2.sid, t2.SERIAL# 可以用作庫級殺死會話:alter system kill session 't2.sid, t2.SERIAL#';
-- t1.SPID OS_PID 可以用作系統級殺死會話:kill -9 t1.SPID
-- t2.EVENT 和 P1、P2、P3 不同的等待事件的P1、P2和P3的信息是不盡相同的,具體要查官檔
-- t3.SQL_FULLTEXT 是一個CLOB類型的欄位
--1.從V$SQLAREA視圖中選出最糟糕的查詢
select b.username username,a.disk_reads reads,
a.executions exec,a.disk_reads / decode (a.executions, 0, 1,a.executions) rds_exec_ratio,
a.sql_text STATEMENT
from v$sqlarea a, dba_users b
where a.parsing_user_id = b.user_id
and a.disk_reads > 10000
order by a.disk_reads desc;
--2.從V$SQL視圖中選出最糟糕的查詢
select *
from (select address, --address替換sql_text
rank() over (order by buffer_gets desc ) as rank_bufgets,
to_char(100 * ratio_to_report(buffer_gets) over (), '999.99') pct_buf
from v$sql )
where rank_bufgets < 11;
declare
tune_task varchar2(30);
tune_sql clob;
begin
tune_task := dbms_sqltune.create_tuning_task(
sql_id => '6v864r3vc9qbc',
task_name => 'tune_test2',
description => 'Provide SQL ID'
);
end;
/
--執行DBMS_SQLTUNE並查看建議
exec dbms_sqltune.execute_tuning_task(task_name => 'tune_test2');
set long 10000 longchunksize 10000 linesize 150 pagesize 200
select dbms_sqltune.report_tuning_task('tune_test2') from dual;
--查看和刪除調優任務
select owner,task_name,advisor_name,created from dba_advisor_tasks order by created;
exec dbms_sqltune.drop_tuning_task(task_name => '&&task_name');