Oracle sql优化
最近一段时间内SQL执行耗时top10
SELECT S.SQL_TEXT,
S.SQL_FULLTEXT,
S.SQL_ID,
ROUND(ELAPSED_TIME / 1000000 / (CASE
WHEN (EXECUTIONS = 0 OR NVL(EXECUTIONS, 1) = 1) THEN
1
ELSE
EXECUTIONS
END),
2) "执行时间'S'",
S.EXECUTIONS "执行次数",
S.OPTIMIZER_COST "COST",
S.SORTS,
S.MODULE, --连接模式(JDBC THIN CLIENT:程序)
-- S.LOCKED_TOTAL,
--S.PHYSICAL_READ_BYTES "物理读",
-- S.PHYSICAL_READ_REQUESTS "物理读请求",
--S.PHYSICAL_WRITE_REQUESTS "物理写",
-- S.PHYSICAL_WRITE_BYTES "物理写请求",
S.ROWS_PROCESSED "返回行数",
S.DISK_READS "磁盘读",
S.DIRECT_WRITES "直接路径写",
S.PARSING_SCHEMA_NAME,
S.LAST_ACTIVE_TIME
FROM GV$SQLAREA S
WHERE ROUND(ELAPSED_TIME / 1000000 / (CASE
WHEN (EXECUTIONS = 0 OR NVL(EXECUTIONS, 1) = 1) THEN
1
ELSE
EXECUTIONS
END),
2) > 5 --100 0000微秒=1S
AND S.PARSING_SCHEMA_NAME = USER
AND TO_CHAR(S.LAST_LOAD_TIME, 'YYYY-MM-DD') =
TO_CHAR(SYSDATE, 'YYYY-MM-DD')
AND S.COMMAND_TYPE IN (2, 3, 5, 6, 189)
ORDER BY "执行时间'S'" DESC;
--近期耗时的操作(用这个)
select b.*,a.SQL_TEXT,a.SQL_FULLTEXT,a.LAST_LOAD_TIME ,a.LAST_ACTIVE_TIME from SYS.V_$SQLAREA a,(select * from (SELECT C.SQL_ID,
(C.ETIME - NVL(O.ELAPSED_TIME_TOTAL, 0)) / 1000000 ELAPSED_DELTA_TIME
FROM (SELECT *
FROM (SELECT SQL_ID, PLAN_HASH_VALUE, SUM(ELAPSED_TIME) ETIME
FROM V$SQL S
WHERE S.PLAN_HASH_VALUE <> 0
GROUP BY SQL_ID, PLAN_HASH_VALUE
HAVING MAX(LAST_ACTIVE_TIME) > SYSDATE - 1 / 24
ORDER BY SUM(ELAPSED_TIME) DESC)
WHERE ROWNUM < 100) C,
(SELECT SQL_ID, ELAPSED_TIME_TOTAL, PLAN_HASH_VALUE
FROM DBA_HIST_SQLSTAT HIS
WHERE HIS.SNAP_ID = (SELECT MAX(SNAP_ID) FROM DBA_HIST_SNAPSHOT)
AND HIS.INSTANCE_NUMBER = USERENV('instance')) O
WHERE C.SQL_ID = O.SQL_ID
AND C.PLAN_HASH_VALUE = O.PLAN_HASH_VALUE
ORDER BY ELAPSED_DELTA_TIME DESC )
where rownum<10) b where a.sql_id=b.sql_id
select a.SQL_TEXT from SYS.V_$SQLAREA a,(select * from (SELECT C.SQL_ID,
(C.ETIME - NVL(O.ELAPSED_TIME_TOTAL, 0)) / 1000000 ELAPSED_DELTA_TIME
FROM (SELECT *
FROM (SELECT SQL_ID, PLAN_HASH_VALUE, SUM(ELAPSED_TIME) ETIME
FROM V$SQL S
WHERE S.PLAN_HASH_VALUE <> 0
GROUP BY SQL_ID, PLAN_HASH_VALUE
HAVING MAX(LAST_ACTIVE_TIME) > SYSDATE - 1 / 24
ORDER BY SUM(ELAPSED_TIME) DESC)
WHERE ROWNUM < 100) C,
(SELECT SQL_ID, ELAPSED_TIME_TOTAL, PLAN_HASH_VALUE
FROM DBA_HIST_SQLSTAT HIS
WHERE HIS.SNAP_ID = (SELECT MAX(SNAP_ID) FROM DBA_HIST_SNAPSHOT)
AND HIS.INSTANCE_NUMBER = USERENV('instance')) O
WHERE C.SQL_ID = O.SQL_ID
AND C.PLAN_HASH_VALUE = O.PLAN_HASH_VALUE
ORDER BY ELAPSED_DELTA_TIME DESC )
where rownum<10) b where a.sql_id=b.sql_id
---正在执行的
select a.username, a.sid,b.SQL_TEXT, b.SQL_FULLTEXT
from v$session a, v$sqlarea b
where a.sql_address = b.address
---执行过的
select b.SQL_TEXT,b.FIRST_LOAD_TIME,b.SQL_FULLTEXT
from v$sqlarea b
where b.FIRST_LOAD_TIME between '2009-10-15/09:24:47' and
'2009-10-15/09:24:47' order by b.FIRST_LOAD_TIME
(此方法好处可以查看某一时间段执行过的sql,并且 SQL_FULLTEXT 包含了完整的 sql 语句)
其他
select OSUSER,PROGRAM,USERNAME,SCHEMANAME,B.Cpu_Time,STATUS,B.SQL_TEXT
from V$SESSION A LEFT JOIN V$SQL B ON A.SQL_ADDRESS=B.ADDRESS AND A.SQL_HASH_VALUE=B.HASH_VALUE order by b.cpu_time desc
select address, sql_text, piece
from v$session, v$sqltext
where address = sql_address
-- and machine = < you machine name >
order by address, piece
查找前十条性能差的sql.
SELECT * FROM (select PARSING_USER_ID,EXECUTIONS,SORTS,
COMMAND_TYPE,DISK_READS,sql_text FROM v$sqlarea
order BY disk_reads DESC )where ROWNUM<10 ;
查看占io较大的正在运行的session
SELECT se.sid,se.serial#,pr.SPID,se.username,se.status,
se.terminal,se.program,se.MODULE,、se.sql_address,st.event,st.
p1text,si.physical_reads,
si.block_changes FROM v$session se,v$session_wait st,
v$sess_io si,v$process pr WHERE st.sid=se.sid AND st.
sid=si.sid AND se.PADDR=pr.ADDR AND se.sid>6 AND st.
wait_time=0 AND st.event NOT LIKE '%SQL%' ORDER BY physical_reads DESC
-----一下脚本执行结果:
SELECT * FROM ( SELECT PARSING_USER_ID EXECUTIONS,SORTS,COMMAND_TYPE,DISK_READS,sql_text
FROM v$sqlarea
ORDER BY disk_reads DESC)
WHERE ROWNUM<100
CPU和IO占用最多:
select * from (
select sql_text,executions,buffer_gets,disk_reads from v$sql
where buffer_gets > 100000
or disk_reads > 100000
order by buffer_gets+100*disk_reads desc
) where rownum<6
**********这个有用*****************
前5个花费最多CPU和时间:
select sql_text,executions,
round(elapsed_time/1000000,2) elapsed_seconds,
round(cpu_time/1000000,2) cpu_secs from
(select * from v$sql order by elapsed_time desc)
where rownum<6
---------------------查找活动会话的sql---------------
select sql_text,piece
from v$sqltext
where (address,hash_value) in ( select decode(sql_hash_value,0,prev_sql_addr,sql_address) ,
decode(sql_hash_value,0,prev_hash_value,sql_hash_value)
from v$session
where sid in (select sid from v$session where username='WQPF'and status='ACTIVE' and logon_time<sysdate-2/24) )
order by piece;
查看某个表上的索引
select * from all_indexes where table_owner='FUND_SUPERVISE' and table_name='T_STU_FEE_FLOW';
SELECT INDEX_NAME FROM USER_INDEXES WHERE TABLE_NAME='T_PRE_PAY_NO';
create index idx_pre_pay_no_flowcode on T_PRE_PAY_NO(flow_code);
SELECT INDEX_NAME FROM USER_INDEXES WHERE TABLE_NAME='T_STUDENT_INFO';