分享
 
 
 

如何给Large Delete操作提速近千倍?(六)

王朝厨房·作者佚名  2007-01-04
窄屏简体版  字體: |||超大  

摘要:如何给Large Delete操作提速近千倍?(六)

本文已经发表在ITPUB优化技术丛书,未经许可,不得转载。

本文已经发表在ITPUB优化技术丛书,未经许可,不得转载。

1.1. 第五次优化——使用FORALL + 原子级操作

这一次的优化过程已经很清晰了,主要是综合了前面几次优化的思想,将他们的优点集中使用,即:

1) 分段处理:将整个大的事务分割为以10000行为单位的小事务,其中,10000这个基数来自于上面使用批量测试时的结果(从删除10000条记录到删除100000条记录,所需要的操作时间已经出现了比较大的倍数关系)。

2) 拆分操作:避开庞大的IN-LIST条件,而是采用原子级的处理。

3) 批量绑定:使用FORALL进行批量绑定,以数组方式处理,大大减少了PL/SQL引擎和SQL引擎的上下文切换,从而提高了处理速度。

现在,首先来构造以10000为单位的FORALL处理过程:

create or replace procedure del_hubei_SSF_forall

as

type ridArray is table of rowid index by binary_integer;

type dtArray is table of varchar2(50) index by binary_integer;

v_rowid ridArray;

v_mid_to_delete dtArray;

begin

select mid,rowid bulk collect into v_mid_to_delete,v_rowid from temp_mid_hubei_bak where rownum<10001;

forall i in 1 .. v_mid_to_delete.COUNT

delete from SSF where mid = v_mid_to_delete(i);

-- DBMS_OUTPUT.PUT_LINE(to_char(v_mid_to_delete.COUNT)||’ records deleted from hubei_SSF !!!’);

forall i in 1 .. v_rowid.COUNT

delete from temp_mid_hubei_bak where rowid = v_rowid(i);

-- DBMS_OUTPUT.PUT_LINE(to_char(v_rowid.COUNT)||’ records deleted from temp_mid_hubei_bak !!!’);

end;

/

然后构造始终按照10000条循环批量删除的过程:

create or replace procedure exec_forall

(

p_RowCount in number, -- Total need to delete rows count

p_ExeCount in number -- Every times need to delete rows count

)

as

n_RowCount number:=0; -- Yet needed to delete rows count

n_ExeTimes number:=0; -- execute times(loop times)

n_delete number:=0; -- really delete rows count

begin

n_RowCount := p_RowCount;

while n_RowCount >0 loop

EXECUTE IMMEDIATE ’begin del_hubei_SUBSREGINFO_forall; end;’;

commit;

if n_RowCount>p_ExeCount then

n_RowCount:= n_RowCount-p_ExeCount;

n_ExeTimes := n_ExeTimes + 1;

else

n_ExeTimes := n_ExeTimes + 1;

n_delete := n_RowCount;

n_RowCount:= n_RowCount-p_ExeCount;

end if;

DBMS_OUTPUT.PUT_LINE(’---------’||to_char(n_ExeTimes)||’-----------’);

n_delete := n_delete+p_ExeCount*(n_ExeTimes-1);

end loop;

DBMS_OUTPUT.PUT_LINE(’Full Finished!!!’);

DBMS_OUTPUT.PUT_LINE(’Totally ’||to_char(n_delete)||’ records deleted. !!!’);

end;

/

好了,现在来测试一下,这次优化的成果吧:

首先,看一下临时表的记录数目:

SQL> select count(*) temp_mid_hubei from temp_mid_hubei_bak;

TEMP_MID_HUBEI

--------------

1444661

Elapsed: 00:00:02.41

SQL>

再检查一下业务大表的记录数目;

SQL> SELECT COUNT(*) SSF FROM SSF;

SSF

-----------

83446270

ELAPSED: 00:04:53.27

SQL>

首先,我们删除500000行记录,看看需要多少时间完成:

SQL> exec exec_forall(500010,10001);

10000 records deleted from hubei_SSF !!!

10000 records deleted from temp_mid_hubei_bak !!!

---------1-----------

10000 records deleted from hubei_SSF !!!

10000 records deleted from temp_mid_hubei_bak !!!

---------2-----------

10000 records deleted from hubei_SSF !!!

10000 records deleted from temp_mid_hubei_bak !!!

---------3-----------

10000 records deleted from hubei_SSF !!!

10000 records deleted from temp_mid_hubei_bak !!!

---------4-----------

10000 records deleted from hubei_SSF !!!

10000 records deleted from temp_mid_hubei_bak !!!

---------5-----------

10000 records deleted from hubei_SSF !!!

10000 records deleted from temp_mid_hubei_bak !!!

---------6-----------

。。。。。。。。。。。(这里省略掉了一些雷同的输入)

---------50-----------

Full Finished!!!

Totally 500010 records deleted. !!!

PL/SQL procedure successfully completed.

Elapsed: 00:02:57.78

SQL>

这是个惊人的数字,2分57秒!!

于是再来删除90万试试看:

SQL> exec exec_forall(900010,10001);

10000 records deleted from hubei_SSF !!!

10000 records deleted from temp_mid_hubei_bak !!!

---------1-----------

10000 records deleted from hubei_SSF !!!

10000 records deleted from temp_mid_hubei_bak !!!

---------2-----------

10000 records deleted from hubei_SSF !!!

10000 records deleted from temp_mid_hubei_bak !!!

---------3-----------

10000 records deleted from hubei_SSF !!!

10000 records deleted from temp_mid_hubei_bak !!!

---------4-----------

10000 records deleted from hubei_SSF !!!

10000 records deleted from temp_mid_hubei_bak !!!

---------5-----------

10000 records deleted from hubei_SSF !!!

10000 records deleted from temp_mid_hubei_bak !!!

---------6-----------

。。。。。。。。。

---------86-----------

10000 records deleted from hubei_SSF !!!

10000 records deleted from temp_mid_hubei_bak !!!

---------87-----------

10000 records deleted from hubei_SSF !!!

10000 records deleted from temp_mid_hubei_bak !!!

---------88-----------

10000 records deleted from hubei_SSF !!!

10000 records deleted from temp_mid_hubei_bak !!!

---------89-----------

10000 records deleted from hubei_SSF !!!

10000 records deleted from temp_mid_hubei_bak !!!

---------90-----------

Full Finished!!!

Totally 900010 records deleted. !!!

PL/SQL procedure successfully completed.

Elapsed: 00:02:33.47

SQL>

SQL> select count(*) temp_mid_hubei from temp_mid_hubei_bak;

TEMP_MID_HUBEI

--------------

34661

Elapsed: 00:00:00.46

SQL>

删除90万的数据只需要2分33秒!!和删除50万数据量相比,使用的操作时间差不多。

到此,此次任务已经可以圆满完成了(将所有的操作使用这种优化思想写成脚本,很快就可以完成任务)。

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有