Oracle 学习总结 - 隔离级别以及问题诊断

Posted it-worker365

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Oracle 学习总结 - 隔离级别以及问题诊断相关的知识,希望对你有一定的参考价值。

事物隔离级别

感觉这个例子比较通俗https://blog.csdn.net/qq_33290787/article/details/51924963

名称   解释-t1 解释-t2
READ UNCOMMITTED

读取未提交内容,脏读

begin transaction

update

 

rollback

select data
READ COMMITTED 读取提交内容

begin transaction

update

commit

 

 

begin transaction

update

commit

 

 

 

 

select data

 

 

 

select data

REPEATABLE READ 可重读

begin transaction

insert value into range

commit

 

 

begin transaction

insert value into range

commit

 

 

 

 

select data where range

 

 

 

 

select data where range

SERIALIZABLE 可串行化,为每个读数据建立共享锁   完全串行,未提交再操作等待

 

查看oracle数据库隔离级别

首先需要先在事物中,然后用如下sql查看事物隔离级别

select sid,serial#,flag,
  CASE WHEN bitand(FLAG,268435456) = 0 THEN SERIALIZABLE
                                       ELSE READ COMMITTED
                                       END AS ISOLATIONLEVEL
  from V$transaction t,v$session s
  where t.addr=s.taddr
  AND   audsid = USERENV(SESSIONID);
       SID    SERIAL#   FLAG ISOLATIONLEVEL
---------- ------------- -------- --------------
    52        3            3587 SERIALIZABLE

设置oracle数据库隔离级别

 SET TRANSACTION ISOLATION LEVEL***

 

搜集常用诊断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;

技术分享图片

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;

技术分享图片

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;

技术分享图片

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;

技术分享图片

 

查看绑定变量使用:

 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;

技术分享图片

等待/争用:

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 ;

技术分享图片

2. 回滚段争用

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

技术分享图片

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;

技术分享图片

数据表和表空间

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;

技术分享图片

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;

技术分享图片

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;

技术分享图片

4. 查看表大小

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

技术分享图片

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

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

技术分享图片

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;

技术分享图片

2. 读磁盘超过100次的sql

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

技术分享图片

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;

技术分享图片

 

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 ;

技术分享图片

 索引相关

1.查看表的索引

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

技术分享图片

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;

技术分享图片

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 学习总结 - 隔离级别以及问题诊断的主要内容,如果未能解决你的问题,请参考以下文章

事务的隔离级别以及oracle中的锁

MySQL学习总结-演示不同隔离级别下的并发问题

数据库事务事务隔离级别以及锁机制详解

数据库事务,事务并发问题,锁的隔离级别,锁的粒度

Mysql—4种隔离级别以及MVCC一致性视图的实现原理

DB | MySQL核心知识学习之路