oracle 数据按主键删除慢问题的解决方法
问题描述:
根据表主键id删除一条数据,在pl/sql上执行commit后执行时间都大于5秒。!!!
问题分析:
需求是删除一个主表a,另有两个附表建有此表的主键id的外键。删除a表的数据级联删除另两个表的关联数据。增删改查使用hibernate实现。
一开始一直以为是hibernate的内部处理上有关联操作导致的删除和更新数据缓慢。所以将原先使用hibernate的saveorupdate方法,改查jdbc的
sql语句来处理update和delete数据操作。但是依然没效果!!!
怀疑数据库出问题了!~
于是拿sql语句在pl/sql客户端执行,查看执行计划。删除和更新都能使用到索引。但是commit后执行依然很慢! 因此可以判断出是数据库方面的问题。
任何数据库删除一条数据不可能耗费5秒以上的时间啊!那就要查看sql的执行过程了!
网上搜了一堆资料查看。最后确定查看sql执行跟踪文件。 sql执行是一次session,oracle数据库很好的支持sesion的跟踪,锁表情况等。考虑要操作生
产数据库。不能大量跟踪session。于是选择跟踪指定sesion的方式,只查看自己执行的sql执行计划! 方式如下:
alter session set events='10046 trace name context forever,level 12'; --- 固定语句
delete from t_table1 where id = 23242342; --- 你要跟踪的sql语句
alter session set events='10046 trace name context off';--- 固定语句
sql跟踪得到一个trace文件:
通过sql查找存储路径:
select pr.value || '\' || i.instance_name || '_ora_' || to_char(ps.spid) || '.trc' "trace file name" from v$session s, v$process ps, v$parameter pr, v$instance i where s.paddr = ps.addr and s.sid = userenv('sid') and pr.name = 'user_dump_dest';
/home/oracle/dbsoftware/diag/rdbms/ora11g/ora11g/trace\ora11g_ora_42990.trc
然后到服务器上取下trc文件。
打开查看到:
/* mv_refresh (del) */ delete from "inms31"."mv_band_port_rel_area"
还有:
4311 /* mv_refresh (del) */ delete from "inms31"."mv_band_port_rel_area" 4402/*mv_refresh (ins) */insert /*+ */ into "inms31"."mv_band_port_rel_area"("id","account_id","port_id","dev_ip","port_iden","area_name") select "pr"."id","pr"."account_id","pr"."port_id","d"."dev_ip","p"."port_iden","a"."area_name" from "tb_band_user_port_rel" "pr","tb_port" "p","tb_device" "d","tb_area" "a" where "pr"."port_id"="p"."id" and "p"."dev_id"="d"."id" and "d"."dev_main_area_id"="a"."id" 5309 /* mv_refresh (del) */ delete from "inms31"."mv_band_ftth_rel_area" 5482 /* mv_refresh (ins) */insert /*+ */ into "inms31"."mv_band_ftth_rel_area"("id","account_id","onu_info_id","dev_ip","onu_desc","area_name") select "prh"."id","prh"."account_id","prh"."onu_info_id","d"."dev_ip","o"."onu_desc","a"."area_name" from "tb_band_user_port_rel_ftth" "prh","tb_onu_info" "o","tb_device" "d","tb_area" "a" where "prh"."onu_info_id"="o"."id" and "o"."olt_id"="d"."id" and "d"."dev_main_area_id"="a"."id" 9984 /* mv_refresh (del) */ delete from "inms31"."mv_band_port_rel_area" 10061 /* mv_refresh (ins) */insert /*+ */ into "inms31"."mv_band_port_rel_area"("id","account_id","port_id","dev_ip","port_iden","area_name") select "pr"."id","pr"."account_id","pr"."port_id","d"."dev_ip","p"."port_iden","a"."area_name" from "tb_band_user_port_rel" "pr","tb_port" "p","tb_device" "d","tb_area" "a" where "pr"."port_id"="p"."id" and "p"."dev_id"="d"."id" and "d"."dev_main_area_id"="a"."id"
原来在删除之后都有个物化视图的刷新操作!!!
oh. 买噶! 想起在做这个主表的操作时有个物化视图随基表变化而立即刷新的操作!基表有10多万条数据,物化视图关联了多张表。单独刷新也要几秒时间!就是这样原因了!实际现在已经不需要这个物化视图了,所需查询数据已经改成别的方式获取!于是删掉物化视图。执行删除,更新,0.003秒!问题解决!
通过这次问题处理,总结以下教训:
1. 物化视图尽量不要做成立即刷新模式,这样如果基表更新频繁性能问题立马出现。如果确需做物化视图,做成job定时在基表使用闲时执行。
2. 在pl/sql等客户端执行sql查询基本的数据或删除更新很少数据量而时间超过一秒的就要想法跟踪下sql执行计划了。
3. sql执行计划跟踪采用如下几种方式:
1.首先查看sql的执行计划,执行计划正常,cost只有4,用到了主键索引
2. 查看等待事件,
3. select * from v$session_wait where sid = 507
4. 查看系统io,
--------------------------------------
1. 使用 autotrace 查看执行计划
set autotrace on | on explain | on statistics | traceonly | traceonly explain
set autotrace off
2. 启用 sql_trace 跟踪当前 session
开启会话跟踪:alter session set sql_trace=true;
关闭会话跟踪:alter session set sql_trace=false
3. 启用 10046 事件跟踪当前 session
开启会话跟踪:alter session set events '10046 trace name context forever, level 12';
关闭会话跟踪:alter session set events '10046 trace name context off';
对跟踪文件加标识:alter session set tracefile_identifier='dragon';
sql> host dir e:\oracle\product\10.2.0\admin\byisdb\udump\
驱动器 e 中的卷是 disk1_vol3
卷的序列号是 609e-62d9
e:\oracle\product\10.2.0\admin\byisdb\udump 的目录
2012-07-19 17:58 <dir> .
2012-07-19 17:58 <dir> ..
2012-07-19 17:58 3,057 byisdb_ora_704.trc
2012-07-19 17:58 169,447 byisdb_ora_704_dragon.trc
2 个文件 172,504 字节
2 个目录 22,060,634,112 可用字节
4. 启用 10046 事件跟踪全局 session
这将会对整个系统的性能产生严重的影响,所以一般不建议开启。
开启会话跟踪:alter system set events ‘10046 trace name context forever, level 12';
关闭会话跟踪:alter system set events ‘10046 trace name context off';
获取跟踪文件
sql> select pr.value || '\' || i.instance_name || '_ora_' || to_char(ps.spid) || '.trc' "trace file name" from v$session s, v$process ps, v$parameter pr, v$instance i where s.paddr = ps.addr and s.sid = userenv('sid') and pr.name = 'user_dump_dest'; trace file name -------------------------------------------------------------------------------- e:\oracle\product\10.2.0\admin\byisdb\udump\byisdb_ora_372.trc
5. 使用 oracle 系统包 dbms_system.set_ev 跟踪指定 session
procedure set_ev
参数名称 类型 输入/输出默认值?
------------------------------ ----------------------- ------ --------
si binary_integer in
se binary_integer in
ev binary_integer in
le binary_integer in
nm varchar2 in
参数说明:
si-指定session的sid;
se-指定session的se;
ev-事件id(如:10046);
le-表示trace的级别;
nm-指定session的username;
sql> select userenv('sid') sid from dual; sid ---------- 143 sql> select sid, serial#, username from v$session where sid=143; sid serial# username ---------- ---------- ------------------------------ 143 112 una_hr
开启会话跟踪:sql> exec dbms_system.set_ev(143, 112, 10046, 12, '');
关闭会话跟踪:sql> exec dbms_system.set_ev(143, 112, 10046, 0, '');
6. 使用 tkprof 工具格式化
tkprof tracefile outputfile [options]
e:\oracle\product\10.2.0\admin\byisdb\udump>tkprof byisdb_ora_704.trc 10046.txt sys=no sort=prsela, exeela, fchela
以上就是小编为大家带来的oracle 数据按主键删除慢问题的解决方法全部内容了,希望大家多多支持~