-- -----------------------------------------------------------------------------------
-- File Name : http://www.oracle-base.com/dba/monitoring/top_sql.sql
-- Author : DR Timothy S Hall
-- Description : Displays a list of SQL statements that are using the most resources.
-- Comments : The address column can be use as a parameter with SQL_Text.sql to display the full statement.
-- Requirements : Access to the V$ views.
-- Call Syntax : @top_sql (number)
-- Last Modified: 15/07/2000
-- -----------------------------------------------------------------------------------
SET LINESIZE 500
SET PAGESIZE 1000
SET VERIFY OFF
SELECT *
FROM (SELECT Substr(a.sql_text,1,50) sql_text,
Trunc(a.disk_reads/Decode(a.executions,0,1,a.executions)) reads_per_execution,
a.buffer_gets,
a.disk_reads,
a.executions,
a.sorts,
a.address
FROM v$sqlarea a
ORDER BY 2 DESC)
WHERE rownum <= &&1;
SET PAGESIZE 14
-- -----------------------------------------------------------------------------------
-- File Name : http://www.oracle-base.com/dba/monitoring/top_sessions.sql
-- Author : DR Timothy S Hall
-- Description : Displays information on all database sessions ordered by executions.
-- Requirements : Access to the V$ views.
-- Call Syntax : @top_sessions.sql (reads, execs or cpu)
-- Last Modified: 21/02/2005
-- -----------------------------------------------------------------------------------
SET LINESIZE 500
SET PAGESIZE 1000
SET VERIFY OFF
COLUMN username FORMAT A15
COLUMN machine FORMAT A25
COLUMN logon_time FORMAT A20
SELECT NVL(a.username, '(oracle)') AS username,
a.osuser,
a.sid,
a.serial#,
c.value AS &1,
a.lockwait,
a.status,
a.module,
a.machine,
a.program,
TO_CHAR(a.logon_Time,'DD-MON-YYYY HH24:MI:SS') AS logon_time
FROM v$session a,
v$sesstat c,
v$statname d
WHERE a.sid = c.sid
AND c.statistic# = d.statistic#
AND d.name = DECODE(UPPER('&1'), 'READS', 'session logical reads',
'EXECS', 'execute count',
'CPU', 'CPU used by this session',
'CPU used by this session')
ORDER BY c.value DESC;
SET PAGESIZE 14
-- -----------------------------------------------------------------------------------
-- File Name : http://www.oracle-base.com/dba/monitoring/top_latches.sql
-- Author : DR Timothy S Hall
-- Description : Displays information about the top latches.
-- Requirements : Access to the V$ views.
-- Call Syntax : @top_latches
-- Last Modified: 15-JUL-2000
-- -----------------------------------------------------------------------------------
SET LINESIZE 200
SELECT l.latch#,
l.name,
l.gets,
l.misses,
l.sleeps,
l.immediate_gets,
l.immediate_misses,
l.spin_gets
FROM v$latch l
WHERE l.misses > 0
ORDER BY l.misses DESC;
-- -----------------------------------------------------------------------------------
-- File Name : http://www.oracle-base.com/dba/10g/session_waits.sql
-- Author : DR Timothy S Hall
-- Description : Displays information on all database session waits.
-- Requirements : Access to the V$ views.
-- Call Syntax : @session_waits
-- Last Modified: 11/03/2005
-- -----------------------------------------------------------------------------------
SET LINESIZE 200
SET PAGESIZE 1000
COLUMN username FORMAT A20
COLUMN event FORMAT A30
COLUMN wait_class FORMAT A15
SELECT NVL(s.username, '(oracle)') AS username,
s.sid,
s.serial#,
sw.event,
sw.wait_class,
sw.wait_time,
sw.seconds_in_wait,
sw.state
FROM v$session_wait sw,
v$session s
WHERE s.sid = sw.sid
ORDER BY sw.seconds_in_wait DESC;
-- -----------------------------------------------------------------------------------
-- File Name : http://www.oracle-base.com/dba/monitoring/longops.sql
-- Author : DR Timothy S Hall
-- Description : Displays information on all long operations.
-- Requirements : Access to the V$ views.
-- Call Syntax : @longops
-- Last Modified: 05/07/2007
--
-- Thanks to Will Rose for noticing my mistake in using ROUND rather than TRUNC when
-- calculating seconds.
-- -----------------------------------------------------------------------------------
COLUMN sid FORMAT 999
COLUMN serial# FORMAT 9999999
COLUMN machine FORMAT A30
COLUMN progress_pct FORMAT 99999999.00
COLUMN elapsed FORMAT A10
COLUMN remaining FORMAT A10
SELECT s.sid,
s.serial#,
s.machine,
TRUNC(sl.elapsed_seconds/60) || ':' || MOD(sl.elapsed_seconds,60) elapsed,
TRUNC(sl.time_remaining/60) || ':' || MOD(sl.time_remaining,60) remaining,
ROUND(sl.sofar/sl.totalwork*100, 2) progress_pct
FROM v$session s,
v$session_longops sl
WHERE s.sid = sl.sid
AND s.serial# = sl.serial#;
-- -----------------------------------------------------------------------------------
-- File Name : http://www.oracle-base.com/dba/monitoring/locked_objects.sql
-- Author : DR Timothy S Hall
-- Description : Lists all locked objects.
-- Requirements : Access to the V$ views.
-- Call Syntax : @locked_objects
-- Last Modified: 15/07/2000
-- -----------------------------------------------------------------------------------
SET LINESIZE 500
SET PAGESIZE 1000
SET VERIFY OFF
COLUMN owner FORMAT A20
COLUMN username FORMAT A20
COLUMN object_owner FORMAT A20
COLUMN object_name FORMAT A30
COLUMN locked_mode FORMAT A15
SELECT b.session_id AS sid,
NVL(b.oracle_username, '(oracle)') AS username,
a.owner AS object_owner,
a.object_name,
Decode(b.locked_mode, 0, 'None',
1, 'Null (NULL)',
2, 'Row-S (SS)',
3, 'Row-X (SX)',
4, 'Share (S)',
5, 'S/Row-X (SSX)',
6, 'Exclusive (X)',
b.locked_mode) locked_mode,
b.os_user_name
FROM dba_objects a,
v$locked_object b
WHERE a.object_id = b.object_id
ORDER BY 1, 2, 3, 4;
SET PAGESIZE 14
SET VERIFY ON
-- -----------------------------------------------------------------------------------
-- File Name : http://www.oracle-base.com/dba/monitoring/invalid_objects.sql
-- Author : DR Timothy S Hall
-- Description : Lists all invalid objects in the database.
-- Call Syntax : @invalid_objects
-- Requirements : Access to the DBA views.
-- Last Modified: 18/12/2005
-- -----------------------------------------------------------------------------------
COLUMN object_name FORMAT A30
SELECT owner,
object_type,
object_name,
status
FROM dba_objects
WHERE status = 'INVALID'
ORDER BY owner, object_type, object_name;
其他:
-- 值得怀疑的SQL
select substr(to_char(s.pct, '99.00'), 2) || '%' load,
s.executions executes,
p.sql_text
from (select address,
disk_reads,
executions,
pct,
rank() over(order by disk_reads desc) ranking
from (select address,
disk_reads,
executions,
100 * ratio_to_report(disk_reads) over() pct
from sys.v_$sql
where command_type != 47)
where disk_reads > 50 * executions) s,
sys.v_$sqltext p
where s.ranking <= 5
and p.address = s.address
order by 1, s.address, p.piece;
-- 逻辑读多的SQL
select *
from (select buffer_gets, sql_text
from v$sqlarea
where buffer_gets > 500000
order by buffer_gets desc)
where rownum <= 30;
-- 执行次数多的SQL
select sql_text, executions
from (select sql_text, executions from v$sqlarea order by executions desc)
where rownum < 81;
-- 读硬盘多的SQL
select sql_text, disk_reads
from (select sql_text, disk_reads from v$sqlarea order by disk_reads desc)
where rownum < 21;
-- 排序多的SQL
select sql_text, sorts
from (select sql_text, sorts from v$sqlarea order by sorts desc)
where rownum < 21;
--分析的次数太多,执行的次数太少,要用绑变量的方法来写sql
set pagesize 600;
set linesize 120;
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;
-- 游标的观察
set pages 300;
select sum(a.value), b.name
from v$sesstat a, v$statname b
where a.statistic# = b.statistic#
and b.name = 'opened cursors current'
group by b.name;
select count(0) from v$open_cursor;
select user_name, sql_text, count(0)
from v$open_cursor
group by user_name, sql_text
having count(0) > 30;
--查看当前用户&username执行的SQL
select sql_text
from v$sqltext_with_newlines
where (hash_value, address) in
(select sql_hash_value, sql_address
from v$session
where username = '&username')
order by address, piece;
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/11976525/viewspace-659978/
,如需转载,请注明出处,否则将追究法律责任。