例行检查数据库awr报告,有一条update语句执行多次,每次执行时间30多秒,这条sql语句很简单,就是根据主键条件修改数据,主键个
例行检查数据库awr报告,有一条update语句执行多次,每次执行时间30多秒,这条sql语句很简单,就是根据主键条件修改数据,主键个数是1到100之间。这个问题由来已久,只是偶尔出现。主键是varchar2,,类似序列,由于之前有迁移过数据,特别在主键上为迁移的这部分数据加过标记,用肉眼看主键的分布是不均匀的。
第一次诊断:这个表有150万的数据,执行慢是因为update的时候没走到主键索引,于是去看了下直方图的分布,只有两个桶,于是重新收集了主键的直方图信息,有250个桶了。准备观察一天,第二天再看awr,发现反而越来越慢了。
第二次诊断:听开发人员说此表上有触发器,测试发现果然是触发器的问题,触发器消耗的资源统统记在update语句上,让人感到莫名其妙。修改方法是将触发器的业务通过sql实现,整个功能快了不少。下面对问题进行抽象、实验:
1.初始化数据及建立触发器
drop table test1 purge;
drop table test2 purge;
create table test1 as select * from dba_objects;
insert into test1 select * from dba_objects;
commit;
create table test2 as select * from dba_objects;
create index ind_t1_object_id on test1(object_id) nologging;
create index ind_t2_object_id on test2(object_id) nologging;
exec dbms_stats.gather_table_stats(user,'test1',cascade => true);
exec dbms_stats.gather_table_stats(user,'test2',cascade => true);
create or replace trigger t_trigger
before update on test1
for each row
begin
update test2 t
set t.object_name = :old.object_name
where t.object_id = :old.object_id;
end;
sql> set autotrace traceonly
sql> set timing on
2.执行update语句会触发触发器
sql> update test1 set object_name=''||object_name;
已更新140300行。
已用时间: 00: 00: 15.21
执行计划
----------------------------------------------------------
plan hash value: 160929213
----------------------------------------------------------------------------
| id | operation | name | rows | bytes | cost (%cpu)| time |
----------------------------------------------------------------------------
| 0 | update statement | | 140k| 4110k| 384 (1)| 00:00:06 |
| 1 | update | test1 | | | | |
| 2 | table access full| test1 | 140k| 4110k| 384 (1)| 00:00:06 |
----------------------------------------------------------------------------
统计信息
----------------------------------------------------------
140739 recursive calls
427013 db block gets
282079 consistent gets
0 physical reads
120365752 redo size
718 bytes sent via sql*net to client
498 bytes received via sql*net from client
4 sql*net roundtrips to/from client
3 sorts (memory)
0 sorts (disk)
140300 rows processed
sql> commit;
提交完成。
3.disable触发器
sql> alter trigger t_trigger disable;
4.执行update语句不会触发触发器
sql> update test1 set object_name=''||object_name;
已更新140300行。
已用时间: 00: 00: 01.67
执行计划
----------------------------------------------------------
plan hash value: 160929213
----------------------------------------------------------------------------
| id | operation | name | rows | bytes | cost (%cpu)| time |
----------------------------------------------------------------------------
| 0 | update statement | | 140k| 3425k| 384 (1)| 00:00:06 |
| 1 | update | test1 | | | | |
| 2 | table access full| test1 | 140k| 3425k| 384 (1)| 00:00:06 |
----------------------------------------------------------------------------
统计信息
----------------------------------------------------------
389 recursive calls
144840 db block gets
2216 consistent gets
0 physical reads
50003740 redo size
721 bytes sent via sql*net to client
498 bytes received via sql*net from client
4 sql*net roundtrips to/from client
8 sorts (memory)
0 sorts (disk)
140300 rows processed