oracle update操作的優化一例
客戶的每小時redolog日誌量大,配合AWR和LOGMINER檢查發現是由一條update語句引起。這條語句大概每小時執行80次左右,不僅產生了大量的重做日誌,而且邏輯讀也很高。
語句類似update tb_test_log set object_id=1 where owner='SYS',是對錶tb_test_log按一定的頻率,把滿足條件owner='SYS'的記錄中的object_id修改為1,而且滿足條件的記錄佔了整個表的一半左右。但實際上在每次更新時,滿足條件owner='SYS'的記錄中絕大部分object_id已經是1.
以下嘗試優化:
DB Version:12.1.0.2.0
OS:centos 6.6
#建測試表
create table tb_test_log tablespace users as select * from dba_objects;
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;
#檢視測試表的大小,大概100MB
select bytes from dba_segments where segment_name=upper('tb_test_log');
/*
BYTES
109051904
*/
#滿足條件owner='SYS'的記錄大概佔了46%
select count(decode(owner,'SYS',1,null))/count(1) from tb_test_log;
/*
0.461732733062479
*/
#優化前SQL
update tb_test_log set object_id=1 where owner='SYS';
#新建會話統計資料記錄表,用於後面的重做日誌和邏輯讀的計算
declare
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:
#檢視會話1的會話ID
select sid from v$mystat where rownum<=1;
/*
SID
35
*/
會話2:
#插入會話1當前的重做日誌和邏輯讀的統計資料
insert into t_stat_temp
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;
#DIFF是會話1產生的重做日誌和邏輯讀的量
select name,min(value) begin_value,max(value) end_value,max(value)-min(value) diff
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
*/
#後續會話2都是執行上面相同的插入和查詢語句,省略語句,只顯示查詢結果
會話1:
#會話1執行優化前的更新語句
update tb_test_log set object_id=1 where owner='SYS';
commit;
會話2:
#會話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:
#會話1執行優化前的更新語句
update tb_test_log set object_id=1 where owner='SYS';
commit;
會話2:
#會話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:
#會話1執行優化前的更新語句
update tb_test_log set object_id=1 where owner='SYS';
commit;
會話2:
#會話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
*/
會話1:
#會話1執行優化前的更新語句
update tb_test_log set object_id=1 where owner='SYS';
commit;
會話2:
#會話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:
#會話1執行優化前的更新語句
update tb_test_log set object_id=1 where owner='SYS';
commit;
會話2:
#會話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
*/
小結:優化前,每次更新表中46%左右的資料,重做日誌產生量大概是100MB+,邏輯讀大概是700000+。
優化1:
根據SQL邏輯,增加過濾條件object_id!=1,原語句邏輯不變。
會話1:
#會話1執行優化1的更新語句
update tb_test_log set object_id=1 where owner='SYS' and object_id!=1;
commit;
會話2:
#會話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執行優化1的更新語句
update tb_test_log set object_id=1 where owner='SYS' and object_id!=1;
commit;
會話2:
#會話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執行優化1的更新語句
update tb_test_log set object_id=1 where owner='SYS' and object_id!=1;
commit;
會話2:
#會話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,每次基本上不更新表中資料,重做日誌產生量大概是300+,邏輯讀大概是10000+。
優化2:
根據SQL邏輯,增加過濾條件decode(object_id,1,null,'1')='1',並增加索引tb_test_log(owner,decode(object_id,1,null,'1')),原語句邏輯不變。
會話3:
#新建索引
create index idx_tb_test_log_01 on tb_test_log(owner,decode(object_id,1,null,'1')) tablespace users;
會話1:
#會話1執行優化2的更新語句
update tb_test_log set object_id=1 where owner='SYS' and decode(object_id,1,null,'1')='1';
commit;
會話2:
#會話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:
#會話1執行優化2的更新語句
update tb_test_log set object_id=1 where owner='SYS' and decode(object_id,1,null,'1')='1';
commit;
會話2:
#會話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:
#會話1執行優化2的更新語句
update tb_test_log set object_id=1 where owner='SYS' and decode(object_id,1,null,'1')='1';
commit;
會話2:
#會話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
*/
小結:優化2,每次基本上不更新表中資料,重做日誌產生量大概是300+,邏輯讀大概是5+。
總結:
1.根據SQL邏輯,增加過濾條件object_id!=1,原語句邏輯不變,大幅度降低了重做日誌的產生量。
2.根據SQL邏輯,增加過濾條件decode(object_id,1,null,'1')='1',並增加索引tb_test_log(owner,decode(object_id,1,null,'1')),原語句邏輯不變,大幅度降低了重做日誌的產生量和邏輯讀。
3.類似問題的DELETE語句也可以從此方法中受益。
語句類似update tb_test_log set object_id=1 where owner='SYS',是對錶tb_test_log按一定的頻率,把滿足條件owner='SYS'的記錄中的object_id修改為1,而且滿足條件的記錄佔了整個表的一半左右。但實際上在每次更新時,滿足條件owner='SYS'的記錄中絕大部分object_id已經是1.
以下嘗試優化:
DB Version:12.1.0.2.0
OS:centos 6.6
#建測試表
create table tb_test_log tablespace users as select * from dba_objects;
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;
#檢視測試表的大小,大概100MB
select bytes from dba_segments where segment_name=upper('tb_test_log');
/*
BYTES
109051904
*/
#滿足條件owner='SYS'的記錄大概佔了46%
select count(decode(owner,'SYS',1,null))/count(1) from tb_test_log;
/*
0.461732733062479
*/
#優化前SQL
update tb_test_log set object_id=1 where owner='SYS';
#新建會話統計資料記錄表,用於後面的重做日誌和邏輯讀的計算
declare
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:
#檢視會話1的會話ID
select sid from v$mystat where rownum<=1;
/*
SID
35
*/
會話2:
#插入會話1當前的重做日誌和邏輯讀的統計資料
insert into t_stat_temp
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;
#DIFF是會話1產生的重做日誌和邏輯讀的量
select name,min(value) begin_value,max(value) end_value,max(value)-min(value) diff
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
*/
#後續會話2都是執行上面相同的插入和查詢語句,省略語句,只顯示查詢結果
會話1:
#會話1執行優化前的更新語句
update tb_test_log set object_id=1 where owner='SYS';
commit;
會話2:
#會話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:
#會話1執行優化前的更新語句
update tb_test_log set object_id=1 where owner='SYS';
commit;
會話2:
#會話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:
#會話1執行優化前的更新語句
update tb_test_log set object_id=1 where owner='SYS';
commit;
會話2:
#會話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
*/
會話1:
#會話1執行優化前的更新語句
update tb_test_log set object_id=1 where owner='SYS';
commit;
會話2:
#會話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:
#會話1執行優化前的更新語句
update tb_test_log set object_id=1 where owner='SYS';
commit;
會話2:
#會話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
*/
小結:優化前,每次更新表中46%左右的資料,重做日誌產生量大概是100MB+,邏輯讀大概是700000+。
優化1:
根據SQL邏輯,增加過濾條件object_id!=1,原語句邏輯不變。
會話1:
#會話1執行優化1的更新語句
update tb_test_log set object_id=1 where owner='SYS' and object_id!=1;
commit;
會話2:
#會話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執行優化1的更新語句
update tb_test_log set object_id=1 where owner='SYS' and object_id!=1;
commit;
會話2:
#會話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執行優化1的更新語句
update tb_test_log set object_id=1 where owner='SYS' and object_id!=1;
commit;
會話2:
#會話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,每次基本上不更新表中資料,重做日誌產生量大概是300+,邏輯讀大概是10000+。
優化2:
根據SQL邏輯,增加過濾條件decode(object_id,1,null,'1')='1',並增加索引tb_test_log(owner,decode(object_id,1,null,'1')),原語句邏輯不變。
會話3:
#新建索引
create index idx_tb_test_log_01 on tb_test_log(owner,decode(object_id,1,null,'1')) tablespace users;
會話1:
#會話1執行優化2的更新語句
update tb_test_log set object_id=1 where owner='SYS' and decode(object_id,1,null,'1')='1';
commit;
會話2:
#會話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:
#會話1執行優化2的更新語句
update tb_test_log set object_id=1 where owner='SYS' and decode(object_id,1,null,'1')='1';
commit;
會話2:
#會話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:
#會話1執行優化2的更新語句
update tb_test_log set object_id=1 where owner='SYS' and decode(object_id,1,null,'1')='1';
commit;
會話2:
#會話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
*/
小結:優化2,每次基本上不更新表中資料,重做日誌產生量大概是300+,邏輯讀大概是5+。
總結:
1.根據SQL邏輯,增加過濾條件object_id!=1,原語句邏輯不變,大幅度降低了重做日誌的產生量。
2.根據SQL邏輯,增加過濾條件decode(object_id,1,null,'1')='1',並增加索引tb_test_log(owner,decode(object_id,1,null,'1')),原語句邏輯不變,大幅度降低了重做日誌的產生量和邏輯讀。
3.類似問題的DELETE語句也可以從此方法中受益。
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/28539951/viewspace-2127328/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- oracle update操作的最佳化一例Oracle
- oracle優化一例之sql優化Oracle優化SQL
- 藉助索引+非空優化distinct操作一例索引優化
- update語句的優化方式優化
- mysql update join優化update in查詢效率MySql優化
- 索引回表操作,ORACLE所作的優化索引Oracle優化
- PL/SQL優化一例SQL優化
- 一條update語句的優化探索優化
- SQL SERVER的UPDATE操作比ORACLE快得多SQLServerOracle
- outline優化一例優化
- ORACLE 9i資料庫優化案例(4) --- 索引改善UPDATEOracle資料庫優化索引
- sql優化一例(index_desc)SQL優化Index
- MySQL 常用的UPDATE操作MySql
- 高效的SQL(函式索引優化VIEW一例)SQL函式索引優化View
- For Update操作分析——不同Oracle版本之間的差異研究Oracle
- mysql常用的優化操作MySql優化
- 如何優化in操作優化
- MybatisPlus中的update操作MyBatis
- MongoDB更新(update)操作MongoDB
- UPDATE操作和UNDO
- MySQL 優化六(InnoDB 下 update 資料出現表鎖之優化)MySql優化
- 工作記錄-優化大表更新一例優化
- pl/sql儲存過程優化一例SQL儲存過程優化
- Oracle資料庫中Insert、Update、Delete操作速度Oracle資料庫delete
- Oracle優化的方法Oracle優化
- Oracle 索引的優化Oracle索引優化
- oracle 的優化器Oracle優化
- Oracle的優化器Oracle優化
- Oracle中的for update 和 for update nowaitOracleAI
- MySQL優化基本操作MySql優化
- 優化寫磁碟操作優化
- 限制訪問表的FOR UPDATE操作
- Update操作對索引的影響索引
- oracle優化Oracle優化
- 複合索引與函式索引優化一例索引函式優化
- 2008.07.01 sql優化一例SQL優化
- InnoDB update操作流程圖流程圖
- 對含distinct操作的SQL的優化SQL優化