oracle update操作的优化一例
Posted ITPUB
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了oracle update操作的优化一例相关的知识,希望对你有一定的参考价值。
OS:centos 6.6
insert into tb_test_log select * from tb_test_log;
commit;
insert into tb_test_log select * from tb_test_log;
commit;
insert into tb_test_log select * from tb_test_log;
commit;
/*
BYTES
109051904
*/
/*
0.461732733062479
*/
v_count number;
begin
select count(1) into v_count from dba_tables where table_name='T_STAT_TEMP';
if v_count=1 then
execute immediate 'truncate table t_stat_temp';
else
execute immediate 'create table t_stat_temp(snap_date date,name varchar2(100),value int)';
end if;
end;
#查看会话1的会话ID
/*
SID
35
*/
#插入会话1当前的重做日志和逻辑读的统计数据
select sysdate,a.name,b.value
from v$statname a,v$sesstat b
where a.statistic#=b.statistic# and b.sid=35
and a.name in ('redo size','session logical reads');
commit;
from (select * from t_stat_temp order by snap_date desc)
where rownum<=4
group by name;
/*
NAME BEGIN_VALUE END_VALUE DIFF
redo size 736 736 0
session logical reads 1463 1463 0
*/
会话1:
#会话1执行优化前的更新语句
commit;
#会话1此次执行更新语句后,redo size产生168611404,session logical reads消耗1057915
NAME BEGIN_VALUE END_VALUE DIFF
redo size 736 168612140 168611404
session logical reads 1463 1059378 1057915
*/
#会话1执行优化前的更新语句
commit;
#会话1此次执行更新语句后,redo size产生108994644,session logical reads消耗718610
NAME BEGIN_VALUE END_VALUE DIFF
redo size 168612140 277606784 108994644
session logical reads 1059378 1777988 718610
*/
#会话1执行优化前的更新语句
commit;
#会话1此次执行更新语句后,redo size产生112071424,session logical reads消耗731397
NAME BEGIN_VALUE END_VALUE DIFF
redo size 277606784 389678208 112071424
session logical reads 1777988 2509385 731397
*/ctrl+home可以快速回到编辑区顶部
#会话1执行优化前的更新语句
commit;
#会话1此次执行更新语句后,redo size产生131894432,session logical reads消耗759343
NAME BEGIN_VALUE END_VALUE DIFF
redo size 389678208 521572640 131894432
session logical reads 2509385 3268728 759343
*/
#会话1执行优化前的更新语句
commit;
#会话1此次执行更新语句后,redo size产生133580596,session logical reads消耗762190
NAME BEGIN_VALUE END_VALUE DIFF
redo size 521572640 655153236 133580596
session logical reads 3268728 4030918 762190
*/
#会话1执行优化1的更新语句
commit;
#会话1此次执行更新语句后,redo size产生827112,session logical reads消耗22835
NAME BEGIN_VALUE END_VALUE DIFF
redo size 655153236 655980348 827112
session logical reads 4030918 4053753 22835
*/
#会话1执行优化1的更新语句
commit;
#会话1此次执行更新语句后,redo size产生340,session logical reads消耗12413
NAME BEGIN_VALUE END_VALUE DIFF
redo size 655980348 655980688 340
session logical reads 4053753 4066166 12413
*/
#会话1执行优化1的更新语句
commit;
#会话1此次执行更新语句后,redo size产生340,session logical reads消耗12413
NAME BEGIN_VALUE END_VALUE DIFF
redo size 655980688 655981028 340
session logical reads 4066166 4078579 12413
*/
#新建索引
#会话1执行优化2的更新语句
commit;
#会话1此次执行更新语句后,redo size产生384,session logical reads消耗11214
NAME BEGIN_VALUE END_VALUE DIFF
redo size 655981028 655981412 384
session logical reads 4078579 4089793 11214
*/
#会话1执行优化2的更新语句
commit;
#会话1此次执行更新语句后,redo size产生384,session logical reads消耗6
NAME BEGIN_VALUE END_VALUE DIFF
redo size 655981412 655981796 384
session logical reads 4089793 4089799 6
*/
#会话1执行优化2的更新语句
commit;
#会话1此次执行更新语句后,redo size产生384,session logical reads消耗5
NAME BEGIN_VALUE END_VALUE DIFF
redo size 655981796 655982180 384
session logical reads 4089799 4089804 5
*/
-
根据SQL逻辑,增加过滤条件object_id!=1,原语句逻辑不变,大幅度降低了重做日志的产生量。
-
根据SQL逻辑,增加过滤条件decode(object_id,1,null,'1')='1',并增加索引tb_test_log(owner,decode(object_id,1,null,'1')),原语句逻辑不变,大幅度降低了重做日志的产生量和逻辑读。
-
类似问题的DELETE语句也可以从此方法中受益。
以上是关于oracle update操作的优化一例的主要内容,如果未能解决你的问题,请参考以下文章
oracle的update .... where id in ('','','')优化问题
又一例 select for update 的悲观锁使用所引发的血案