Oracle 学习总结 - 问题诊断

时间:2022-04-27 20:44:50

搜集常用诊断sql

https://blog.csdn.net/yangshangwei/article/details/52449489

lock相关:

1. 查看lock, 打开两个事物,事物1更新一行未提交,事物2更新同一行,互锁,查看得到结果

select a.USERNAME        登录Oracle用户名,
a.MACHINE 计算机名,
SQL_TEXT,
b.FIRST_LOAD_TIME,
b.SQL_FULLTEXT,
a.sid
from v$sqlarea b, v$session a
where a.sql_hash_value = b.hash_value
order by b.FIRST_LOAD_TIME desc;

Oracle 学习总结 - 问题诊断

2. 杀死session

alter system kill session 'sid'

3. 查询导致死锁的sql

SELECT s.sid, q.sql_text
FROM v$sqltext q, v$session s
WHERE q.address = s.sql_address AND s.sid = 52 -- 这个&sid 是第一步查询出来的
ORDER BY piece;

Oracle 学习总结 - 问题诊断

4. 查看锁会话的关系

SELECT s1.username || '@' || s1.machine || ' ( SID=' || s1.sid ||
' ) is blocking ' || s2.username || '@' || s2.machine || ' ( SID=' ||
s2.sid || ' ) ' AS blocking_status
FROM v$lock l1, v$session s1, v$lock l2, v$session s2
WHERE s1.sid = l1.sid
AND s2.sid = l2.sid
AND l1.BLOCK = 1
AND l2.request > 0
AND l1.id1 = l2.id1
AND l2.id2 = l2.id2;

Oracle 学习总结 - 问题诊断

5. 查看资源状态 select * from tt for update锁定资源之后

select object_name as 对象名称,s.sid,s.serial#,p.spid as 系统进程号
from v$locked_object l , dba_objects o , v$session s , v$process p
where l.object_id=o.object_id and l.session_id=s.sid and s.paddr=p.addr;

Oracle 学习总结 - 问题诊断

查看绑定变量使用:

 select bind.sql_id, sql.sql_text, bind.name, bind.datatype_string, bind.last_captured, bind.value_string
from v$sql_bind_capture bind, v$sql sql where bind.sql_id = sql.sql_id order by LAST_CAPTURED, POSITION;

Oracle 学习总结 - 问题诊断

等待/争用:

1. 监控实例等待

select event,sum(decode(wait_Time,0,0,1)) "Prev",
sum(decode(wait_Time,0,1,0)) "Curr",count(*) "Tot"
from v$session_Wait
group by event order by 4 ;

Oracle 学习总结 - 问题诊断

2. 回滚段争用

select name, waits, gets, waits / gets "Ratio"
from v$rollstat a, v$rollname b
where a.usn = b.usn;

Oracle 学习总结 - 问题诊断

3. 查看回滚段名称及大小

SELECT segment_name,
tablespace_name,
r.status,
(initial_extent / 1024) initialextent,
(next_extent / 1024) nextextent,
max_extents,
v.curext curextent
FROM dba_rollback_segs r, v$rollstat v
WHERE r.segment_id = v.usn(+)
ORDER BY segment_name;

Oracle 学习总结 - 问题诊断

数据表和表空间

1. 查看数据表空间大小

select segment_name, tablespace_name, bytes, blocks
from user_segments
where segment_type = 'TABLE' and segment_name='TT'
ORDER BY bytes DESC, blocks DESC;

Oracle 学习总结 - 问题诊断

2. 查询表空间碎片的大小

select tablespace_name,round(sqrt(max(blocks)/sum(blocks))*
(100/sqrt(sqrt(count(blocks)))),2) FSFI
from dba_free_space
group by tablespace_name order by 1;

Oracle 学习总结 - 问题诊断

3. 表空间占用磁盘的情况

select
b.file_id 文件ID号,
b.tablespace_name 表空间名,
b.bytes 字节数,
(b.bytes-sum(nvl(a.bytes,0))) 已使用,
sum(nvl(a.bytes,0)) 剩余空间,
sum(nvl(a.bytes,0))/(b.bytes)*100 剩余百分比
from dba_free_space a,dba_data_files b
where a.file_id=b.file_id
group by b.tablespace_name,b.file_id,b.bytes
order by b.file_id;

Oracle 学习总结 - 问题诊断

4. 查看表大小

SELECT SEGMENT_NAME, SUM(BYTES) / 1024 / 1024 MBYTESE
FROM USER_SEGMENTS
WHERE SEGMENT_TYPE = 'TABLE'
GROUP BY SEGMENT_NAME
order by MBYTESE desc;

Oracle 学习总结 - 问题诊断

5. 查询表空间物理文件的大小

SELECT tablespace_name,
file_id,
file_name,
round(bytes / (1024 * 1024), 0) total_space
FROM dba_data_files
ORDER BY tablespace_name;

Oracle 学习总结 - 问题诊断

sql语句相关

1.性能最差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<100;

Oracle 学习总结 - 问题诊断

2. 读磁盘超过100次的sql

select * from sys.v_$sqlarea where disk_reads>100;

Oracle 学习总结 - 问题诊断

3.查看较耗资源的sql

Select se.username,
se.sid,
su.extents,
su.blocks * to_number(rtrim(p.value)) as Space,
tablespace,
segtype,
sql_text
from v$sort_usage su, v$parameter p, v$session se, v$sql s
where p.name = 'db_block_size'
and su.session_addr = se.saddr
and s.hash_value = su.sqlhash
and s.address = su.sqladdr
order by se.username, se.sid;

4.查看执行频繁的sql

select * from sys.v_$sqlarea where executions>100;

5.查看正在执行的sql及触发用户

SELECT b.sid oracleID,
b.username 登录Oracle用户名,
b.serial#,
spid 操作系统ID,
paddr,
sql_text 正在执行的SQL,
b.machine 计算机名
FROM v$process a, v$session b, v$sqlarea c
WHERE a.addr = b.paddr
AND b.sql_hash_value = c.hash_value
select a.username, a.sid,b.SQL_TEXT, b.SQL_FULLTEXT
from v$session a, v$sqlarea b
where a.sql_address = b.address

6.查看oracle执行过的语句及执行人

---执行过的
select a.USERNAME 登录Oracle用户名,
a.MACHINE 计算机名,
SQL_TEXT,
b.FIRST_LOAD_TIME,
b.SQL_FULLTEXT
from v$sqlarea b, v$session a
where a.sql_hash_value = b.hash_value
and b.FIRST_LOAD_TIME between '2016-11-01/09:24:47' and
'2016-11-31/09:24:47'
order by b.FIRST_LOAD_TIME desc;

7.查询缓存命中率

select sum(pins) "Executions",
sum(reloads) "Cache Misses",
sum(reloads) / sum(pins)
from v$librarycache;

Oracle 学习总结 - 问题诊断

SGA使用率相关

1. sga命中率

select a.value + b.value "logical_reads",
c.value "phys_reads",
round(100 * ((a.value+b.value)-c.value) / (a.value+b.value)) "BUFFER HIT RATIO"
from v$sysstat a, v$sysstat b, v$sysstat c
where a.statistic# = 38 and
b.statistic# = 39 and
c.statistic# = 40 ;

Oracle 学习总结 - 问题诊断

索引相关

1.查看表的索引

select index_name,table_name,num_rows From dba_indexes i Where i.table_name ='TT';

Oracle 学习总结 - 问题诊断

2.查看用户下所有索引

 create index itt on tt (id)

 select user_indexes.table_name,
user_indexes.index_name,
uniqueness,
column_name
from user_ind_columns, user_indexes
where user_ind_columns.index_name = user_indexes.index_name
and user_ind_columns.table_name = user_indexes.table_name and user_ind_columns.table_name='TT'
order by user_indexes.table_type,
user_indexes.table_name,
user_indexes.index_name,
column_position;

Oracle 学习总结 - 问题诊断

3.针对当前用户某个表的监控索引使用

execute dbms_stats.gather_table_stats(ownname => 'SYSTEM', tabname =>'TT', estimate_percent =>DBMS_STATS.AUTO_SAMPLE_SIZE, method_opt => 'FOR ALL COLUMNS SIZE AUTO');
ALTER INDEX ITT MONITORING USAGE;
select id from tt where id=1;

SELECT * FROM V$OBJECT_USAGE;

Oracle 学习总结 - 问题诊断