You are on page 1of 10

/*一、db file sequential read 等待事件分析*/

1、通过 10046 查看 session 的等待事件


sqlplus aspen/aspen

create table test as select * from dba_objects;

create index idx_test on test(object_id);

exec dbms_stats.gather_table_stats(ownname=>'ASPEN',tabname=>'TEST',cascade=>true);

conn / as sysdba

alter system flush buffer_cache;

oradebug setmypid

oradebug event 10046 trace name context forever,level 12;

select object_name from aspen.test where object_id<50;

oradebug tracefile_name;

oradebug event 10046 trace name context off;

2、多执行几次查询,不 flush buffer_cache 的情况


select object_name from aspen.test where object_id<50;

select object_name from aspen.test where object_id<50;

conn / as sysdba

oradebug setmypid

oradebug event 10046 trace name context forever,level 12;

select object_name from aspen.test where object_id<50;


oradebug tracefile_name;

oradebug event 10046 trace name context off;

3、减少回表的情况,比较 buffer gets


set autotrace on
set lines 300
alter system flush buffer_cache;
select object_name from aspen.test where object_id<50;

set autotrace on
set lines 300
alter system flush buffer_cache;
select object_id from aspen.test where object_id<50;

/*二、db file scattered read 等待事件分析*/


1、通过 10046 查看 session 的等待事件
drop index aspen.idx_test;

exit;
sqlplus / as sysdba
oradebug setmypid

oradebug event 10046 trace name context forever,level 12;

select object_name from aspen.test where object_id<50;

oradebug tracefile_name;

oradebug event 10046 trace name context off;

show parameter DB_FILE_MULTIBLOCK_READ_COUNT;

2、多执行几次查询,不 flush buffer_cache 的情况


select object_name from aspen.test where object_id<50;

select object_name from aspen.test where object_id<50;

sqlplus / as sysdba

oradebug setmypid

oradebug event 10046 trace name context forever,level 12;

set autotrace on
set lines 300
select object_name from aspen.test where object_id<50;
oradebug tracefile_name;

oradebug event 10046 trace name context off;

/*三、direct path read 等待事件分析*/

select ksppinm,ksppstvl,ksppdesc from x$ksppi x,x$ksppcv y where x.indx = y.indx


AND ksppinm ='_small_table_threshold';
select ksppinm,ksppstvl,ksppdesc from x$ksppi x,x$ksppcv y where x.indx = y.indx
AND ksppinm ='_serial_direct_read';

_small_table_threshold 默认值为 buffer cache 的 2%

select bytes/1024/1024 from v$sgastat where name='buffer_cache';

1、通过 10046 查看 session 的等待事件


drop index aspen.idx_test;

select blocks from dba_segments where segment_name='TEST' and owner='ASPEN';

insert into aspen.test select * from aspen.test


create table aspen.test2 as select * from dba_tables;

exit;

sqlplus / as sysdba
alter system flush buffer_cache;

alter session set "_serial_direct_read"=true;


oradebug setmypid

oradebug event 10046 trace name context forever,level 12;

select distinct a.object_name,b.num_rows from aspen.test a ,aspen.test2 b where


a.object_name=b.table_name and a.object_id<50;

oradebug tracefile_name;

oradebug event 10046 trace name context off;

2、如果表的块数小于_small_table_threshold
create table aspen.test3 as select * from dba_objects where object_id<1000;

select blocks from dba_segments where segment_name='TEST3' and owner='ASPEN';

alter system flush buffer_cache;

oradebug setmypid

oradebug event 10046 trace name context forever,level 12;

select distinct a.object_name,b.num_rows from aspen.test3 a ,aspen.test2 b where


a.object_name=b.table_name and a.object_id<50;

oradebug tracefile_name;

oradebug event 10046 trace name context off;

/*四、log file sync 等待事件分析*/

select ksppinm,ksppstvl,ksppdesc from x$ksppi x,x$ksppcv y where x.indx = y.indx


AND ksppinm like '%log%';
select ksppinm,ksppstvl,ksppdesc from x$ksppi x,x$ksppcv y where x.indx = y.indx
AND ksppinm ='_use_single_log_writer';

1、通过 10046 查看 session 的等待事件,insert 一次 commit 一次


sqlplus / as sysdba
oradebug setmypid

oradebug event 10046 trace name context forever,level 12;

insert into aspen.test select * from dba_objects where rownum<2;

commit;

insert into aspen.test select * from dba_objects where rownum<2;

commit;
insert into aspen.test select * from dba_objects where rownum<2;

commit;

oradebug tracefile_name;

oradebug event 10046 trace name context off;

cat /u01/app/oracle/diag/rdbms/noncdb/noncdb/trace/noncdb_ora_65319.trc |grep "log


file sync"

2、通过 10046 查看 session 的等待事件,多次 insert 之后再 commit


sqlplus / as sysdba
oradebug setmypid

oradebug event 10046 trace name context forever,level 12;

insert into aspen.test select * from dba_objects where rownum<2;

insert into aspen.test select * from dba_objects where rownum<2;

insert into aspen.test select * from dba_objects where rownum<2;

commit;

oradebug tracefile_name;

oradebug event 10046 trace name context off;

more /u01/app/oracle/diag/rdbms/noncdb/noncdb/trace/noncdb_ora_26760.trc |grep "log


file sync"

/*五、log file parallel write 等待事件分析*/


ps -ef |grep lgwr

oradebug setospid 54375


oradebug event 10046 trace name context forever,level 12;

insert into aspen.test select * from dba_objects where rownum<2;

commit;
oradebug tracefile_name;

oradebug event 10046 trace name context off;

/*六、buffer busy waits 等待事件分析*/

create table aspen.testbbw (vid int,vname varchar2(20));

insert into aspen.testbbw values(1,'BBW');


insert into aspen.testbbw values(2,'BBWBBW');
commit;

session 1:
select distinct sid from v$mystat;

begin
for i in 1..10000 loop
update aspen.testbbw set vname='BUSY' where vid=1;
commit;
end loop;
end;
/

session 2:
select distinct sid from v$mystat;

begin
for i in 1..10000 loop
update aspen.testbbw set vname='WAIT' where vid=2;
commit;
end loop;
end;
/

set lines 300


col event for a40
select event,sid,p1,p2,p3,WAIT_TIME from v$session_wait_history where sid
in(9,440);

select distinct dbms_rowid.rowid_relative_fno(rowid) file#,


dbms_rowid.rowid_block_number(rowid) block# from aspen.testbbw ;

select vid from (select vid, dbms_rowid.rowid_block_number(rowid) block# from


aspen.testbbw) where block# = 311159;

/*七、free buffer waits 等待事件分析*/

/*八、latch: cache buffers chains 等待事件分析*/

1.表热块争用
cat test_cbc.sh

#!/bin/ksh
for ((i=1; i<=50; ))
do
sqlplus aspen/aspen << EOF
alter system flush buffer_cache;
select distinct a.object_name,b.num_rows from aspen.test a ,aspen.test2 b where
a.object_name=b.table_name and a.object_id<50;
exit
EOF
((i++))
done

alter system flush buffer_cache;

dbms_workload_repository.create_snapshot();

nohup sh test_cbc.sh &


nohup sh test_cbc.sh &
nohup sh test_cbc.sh &
nohup sh test_cbc.sh &
nohup sh test_cbc.sh &
nohup sh test_cbc.sh &
nohup sh test_cbc.sh &
nohup sh test_cbc.sh &
nohup sh test_cbc.sh &

dbms_workload_repository.create_snapshot();

select sql_id, event,p1,p2,p3 from v$session where username='ASPEN' ;

/*九、latch: share pool 等待事件分析*/

cat test_sharepool.sh

#!/bin/ksh
for ((i=1; i<=50; ))
do
$id=$1+$i
sqlplus aspen/aspen << EOF
select count(*) from test where object_id=$id;
exit
EOF
((i++))
done

alter system flush shared_pool;

exec dbms_workload_repository.create_snapshot();

nohup sh test_sharepool.sh 2 &


nohup sh test_sharepool.sh 53 &
nohup sh test_sharepool.sh 104 &
nohup sh test_sharepool.sh 155 &
nohup sh test_sharepool.sh 1116 &
nohup sh test_sharepool.sh 1237 &
nohup sh test_sharepool.sh 1288 &
nohup sh test_sharepool.sh 1388 &
nohup sh test_sharepool.sh 1488 &
nohup sh test_sharepool.sh 1588 &
nohup sh test_sharepool.sh 1688 &
nohup sh test_sharepool.sh 1788 &
nohup sh test_sharepool.sh 1888 &
nohup sh test_sharepool.sh 1988 &
dbms_workload_repository.create_snapshot();

ps -ef |grep sharepool|grep -v grep |awk '{print $2}'|xargs kill -9

/*十、enq: TM - contention 等待事件分析*/

sqlplus aspen/aspen

CREATE TABLE test_tm_pri


(pri_id number(10) not null,
pri_name varchar2(50) not null,
contact_name varchar2(50),
CONSTRAINT pri_pk PRIMARY KEY (pri_id)
);
INSERT INTO test_tm_pri VALUES (1, 'pri 1', 'Contact 1');
INSERT INTO test_tm_pri VALUES (2, 'pri 2', 'Contact 2');
COMMIT;

CREATE TABLE test_tm_sec


(sec_id number(10) not null,
sec_name varchar2(50) not null,
pri_id number(10) not null,
CONSTRAINT fk_pri
FOREIGN KEY (pri_id)
REFERENCES test_tm_pri(pri_id)
ON DELETE CASCADE );
INSERT INTO test_tm_sec VALUES (1, 'Product 1', 1);
INSERT INTO test_tm_sec VALUES (2, 'Product 2', 1);
INSERT INTO test_tm_sec VALUES (3, 'Product 3', 2);
COMMIT;

session 1:
DELETE test_tm_pri WHERE pri_id = 1;

session 2:
DELETE test_tm_pri WHERE pri_id = 2;

SELECT l.sid, s.blocking_session blocker, s.event, l.type, l.lmode, l.request,


o.object_name, o.object_type
FROM v$lock l, dba_objects o, v$session s
WHERE UPPER(s.username) = UPPER('ASPEN')
AND l.id1 = o.object_id (+)
AND l.sid = s.sid
ORDER BY sid, type;

---查找外键未创建索引
SELECT * FROM (
SELECT c.table_name, cc.column_name, cc.position column_position
FROM user_constraints c, user_cons_columns cc
WHERE c.constraint_name = cc.constraint_name
AND c.constraint_type = 'R'
MINUS
SELECT i.table_name, ic.column_name, ic.column_position
FROM user_indexes i, user_ind_columns ic
WHERE i.index_name = ic.index_name
)
ORDER BY table_name, column_position;

/*十一、enq: TX - row lock contention 等待事件分析*/

sqlplus aspen/aspen

session 1:
DELETE test WHERE object_id = 50;

session 2:
DELETE test WHERE object_id = 50;

SELECT l.sid, s.blocking_session blocker, s.event, l.type, l.lmode, l.request,


o.object_name, o.object_type
FROM v$lock l, dba_objects o, v$session s
WHERE UPPER(s.username) = UPPER('ASPEN')
AND l.id1 = o.object_id (+)
AND l.sid = s.sid
ORDER BY sid, type;

/*十二、enq: TX - index contention 等待事件分析*/

conn aspen/aspen

truncate table aspen.testbbw;

create index idx_id on aspen.testbbw(vid) pctfree 0;

create or replace procedure proc_arg


as
begin
for i in 1..100 loop
insert into aspen.testbbw values (1,'INDEX contention');
commit;
end loop;
end;
/

declare
v_job pls_integer;
begin
for i in 1..100 loop
dbms_job.submit(v_job,what => 'proc_arg;',next_date => to_date('2022-04-28
08:52:00','yyyy-mm-dd hh24:mi:ss'),interval=>'sysdate+1');
commit;
end loop;
end;
/

/*十三、enq: TX - allocate ITL entry 等待事件分析*/

conn aspen/aspen
create table test_itl (vid int,vname varchar2(2000)) initrans 1 pctfree 0;
insert into test_itl select object_id,lpad(object_name,2000,'x') from dba_objects
where rownum<50;

commit;

select distinct dbms_rowid.rowid_relative_fno(rowid) file#,


dbms_rowid.rowid_block_number(rowid) block# from test_itl ;

select vid from (select vid, dbms_rowid.rowid_block_number(rowid) block# from


test_itl) where block# = 117536;

alter system dump datafile 7 block 117536;

并发修改同一个数据库上的数据

session 1:

update test_itl set vname=vname where vid=9;

session 2:

update test_itl set vname=vname where vid=53;

session 3:

update test_itl set vname=vname where vid=55;

session 4:

update test_itl set vname=vname where vid=57;

session 5:
select sql_id, event,p1,p2,p3 from v$session where username='ASPEN' ;

/*十四、enq: SQ - contention 等待事件分析*/

create sequence seq_id start with 1 maxvalue 99999999999999 order ;

session 1:

declare
nextval int;
begin
for i in 1..1000000 loop
execute immediate 'select seq_id.nextval from dual' into nextval;
end loop;
end;
/

session 2:

declare
nextval int;
begin
for i in 1..1000000 loop
execute immediate 'select seq_id.nextval from dual' into nextval;
end loop;
end;
/

session 3:

set lines 300


col event for a50
select sql_id, event,p1,p2,p3 from v$session where username='ASPEN' ;

You might also like