bulk_collect結合dbms_application_info監控資料處理進度

foreverlee發表於2006-11-16

bulk collect的效率極高,原因是批次操作大量減少了pl/sql引擎與sql引擎context的切換,降低cpu使用.而且透過合理設定limit,可以使得pga使用和undo空間使用非常節制

Tom說過單條資料處理不能完成,不能滿足需求的時候採用bulk collect是不錯的選擇.

下面的例子處理600多萬條記錄的插入操作,應用此技術在1分半鐘左右搞定.

Oracle 9.2.0.6 64bit For AIX

4C 6G

儲存一般

ps.這次做了一個450G的資料遷移,用的就是RMAN duplicate DB ,而 bulk collect 做schema的拆分

11小時搞定schema的拆分

[@more@]


儲存過程
CREATE OR REPLACE PROCEDURE p_migration_cfg
IS

l_array_size number default 15000;
l_cnt number default 1;
type tb_cfg_type is table of ecifupdate.CFG_ATTR_MODIFY_HISTORY%rowtype;
ref_cfg_bulk tb_cfg_type;
Cursor c_cfg_bulk is
select *
from ecifupdate.CFG_ATTR_MODIFY_HISTORY partition(part_00);
BEGIN
open c_cfg_bulk;
loop
dbms_application_info.set_client_info( 'Processing ' || l_cnt || ' thru ' || (l_cnt+l_array_size-1)||' ... Start at '||to_char(sysdate,'yyyy-mm-dd hh24:mi:ss')||' !!!');
fetch c_cfg_bulk bulk collect into ref_cfg_bulk limit l_array_size;
forall i in 1 .. ref_cfg_bulk.count
insert into CFG_ATTR_MODIFY_HISTORY values ref_cfg_bulk(i);
exit when c_cfg_bulk%notfound;
l_cnt := l_cnt + l_array_size;
end loop;
close c_cfg_bulk;
END;

我這裡每15000條記錄提交一次

所以430次左右提交就可以完陳.


在session A中執行
session A:
11:16:07 SQL> exec p_migration_cfg

PL/SQL procedure successfully completed.

Elapsed: 00:01:29.04
11:17:44 SQL>


session B:
在session B中透過查詢資料字典監控資料處理進度
col CLIENT_INFO for a100
set linesize 150
select sid,client_info from v$session where username='CS_USER'

11:16:17 SQL> /

SID CLIENT_INFO
---------- ----------------------------------------------------------------------------------------------------
16 Processing 210001 thru 225000 ... Start at 2006-11-16 11:16:18 !
41

Elapsed: 00:00:00.00
11:16:18 SQL> /

SID CLIENT_INFO
---------- ----------------------------------------------------------------------------------------------------
16 Processing 285001 thru 300000 ... Start at 2006-11-16 11:16:19 !
41

Elapsed: 00:00:00.01
11:16:20 SQL> /

SID CLIENT_INFO
---------- ----------------------------------------------------------------------------------------------------
16 Processing 360001 thru 375000 ... Start at 2006-11-16 11:16:20 !
41

Elapsed: 00:00:00.01
11:16:21 SQL> /

SID CLIENT_INFO
---------- ----------------------------------------------------------------------------------------------------
16 Processing 450001 thru 465000 ... Start at 2006-11-16 11:16:22 !
41

Elapsed: 00:00:00.00
11:16:22 SQL> /

SID CLIENT_INFO
---------- ----------------------------------------------------------------------------------------------------
16 Processing 525001 thru 540000 ... Start at 2006-11-16 11:16:23 !
41

Elapsed: 00:00:00.00
11:16:23 SQL> /

SID CLIENT_INFO
---------- ----------------------------------------------------------------------------------------------------
16 Processing 600001 thru 615000 ... Start at 2006-11-16 11:16:24 !
41

Elapsed: 00:00:00.01
11:16:24 SQL> /

SID CLIENT_INFO
---------- ----------------------------------------------------------------------------------------------------
16 Processing 660001 thru 675000 ... Start at 2006-11-16 11:16:24 !
41

Elapsed: 00:00:00.00
11:16:25 SQL> /

SID CLIENT_INFO
---------- ----------------------------------------------------------------------------------------------------
16 Processing 735001 thru 750000 ... Start at 2006-11-16 11:16:25 !
41

Elapsed: 00:00:00.00
11:16:26 SQL> /

SID CLIENT_INFO
---------- ----------------------------------------------------------------------------------------------------
16 Processing 795001 thru 810000 ... Start at 2006-11-16 11:16:26 !
41

Elapsed: 00:00:00.00
11:16:26 SQL> /

SID CLIENT_INFO
---------- ----------------------------------------------------------------------------------------------------
16 Processing 855001 thru 870000 ... Start at 2006-11-16 11:16:27 !
41

Elapsed: 00:00:00.00
11:16:27 SQL> /

SID CLIENT_INFO
---------- ----------------------------------------------------------------------------------------------------
16 Processing 915001 thru 930000 ... Start at 2006-11-16 11:16:28 !
41

Elapsed: 00:00:00.01
11:16:28 SQL> /

SID CLIENT_INFO
---------- ----------------------------------------------------------------------------------------------------
16 Processing 990001 thru 1005000 ... Start at 2006-11-16 11:16:29
41

Elapsed: 00:00:00.00
11:16:29 SQL> /

SID CLIENT_INFO
---------- ----------------------------------------------------------------------------------------------------
16 Processing 1050001 thru 1065000 ... Start at 2006-11-16 11:16:30
41

Elapsed: 00:00:00.00
11:16:30 SQL> /

SID CLIENT_INFO
---------- ----------------------------------------------------------------------------------------------------
16 Processing 1200001 thru 1215000 ... Start at 2006-11-16 11:16:32
41

Elapsed: 00:00:00.00
11:16:32 SQL> /

SID CLIENT_INFO
---------- ----------------------------------------------------------------------------------------------------
16 Processing 1335001 thru 1350000 ... Start at 2006-11-16 11:16:34
41

Elapsed: 00:00:00.00
11:16:34 SQL> /

SID CLIENT_INFO
---------- ----------------------------------------------------------------------------------------------------
16 Processing 1530001 thru 1545000 ... Start at 2006-11-16 11:16:37
41

Elapsed: 00:00:00.01
11:16:37 SQL> /

SID CLIENT_INFO
---------- ----------------------------------------------------------------------------------------------------
16 Processing 1620001 thru 1635000 ... Start at 2006-11-16 11:16:38
41

Elapsed: 00:00:00.00
11:16:38 SQL> /

SID CLIENT_INFO
---------- ----------------------------------------------------------------------------------------------------
16 Processing 1710001 thru 1725000 ... Start at 2006-11-16 11:16:39
41

Elapsed: 00:00:00.00
11:16:39 SQL> /

SID CLIENT_INFO
---------- ----------------------------------------------------------------------------------------------------
16 Processing 1965001 thru 1980000 ... Start at 2006-11-16 11:16:42
41

Elapsed: 00:00:00.00
11:16:43 SQL> /

SID CLIENT_INFO
---------- ----------------------------------------------------------------------------------------------------
16 Processing 2115001 thru 2130000 ... Start at 2006-11-16 11:16:44
41

Elapsed: 00:00:00.01
11:16:45 SQL> /

SID CLIENT_INFO
---------- ----------------------------------------------------------------------------------------------------
16 Processing 2190001 thru 2205000 ... Start at 2006-11-16 11:16:46
41

Elapsed: 00:00:00.00
11:16:46 SQL> /

SID CLIENT_INFO
---------- ----------------------------------------------------------------------------------------------------
16 Processing 2325001 thru 2340000 ... Start at 2006-11-16 11:16:47
41

Elapsed: 00:00:00.00
11:16:48 SQL> /

SID CLIENT_INFO
---------- ----------------------------------------------------------------------------------------------------
16 Processing 2475001 thru 2490000 ... Start at 2006-11-16 11:16:49
41

Elapsed: 00:00:00.00
11:16:49 SQL> /

SID CLIENT_INFO
---------- ----------------------------------------------------------------------------------------------------
16 Processing 2580001 thru 2595000 ... Start at 2006-11-16 11:16:51
41

Elapsed: 00:00:00.00
11:16:51 SQL> /

SID CLIENT_INFO
---------- ----------------------------------------------------------------------------------------------------
16 Processing 2685001 thru 2700000 ... Start at 2006-11-16 11:16:52
41

Elapsed: 00:00:00.00
11:16:52 SQL> /

SID CLIENT_INFO
---------- ----------------------------------------------------------------------------------------------------
16 Processing 2895001 thru 2910000 ... Start at 2006-11-16 11:16:55
41

Elapsed: 00:00:00.01
11:16:55 SQL> /

SID CLIENT_INFO
---------- ----------------------------------------------------------------------------------------------------
16 Processing 3105001 thru 3120000 ... Start at 2006-11-16 11:16:58
41

Elapsed: 00:00:00.01
11:16:58 SQL> /

SID CLIENT_INFO
---------- ----------------------------------------------------------------------------------------------------
16 Processing 3240001 thru 3255000 ... Start at 2006-11-16 11:17:00
41

Elapsed: 00:00:00.00
11:17:00 SQL> /

SID CLIENT_INFO
---------- ----------------------------------------------------------------------------------------------------
16 Processing 3450001 thru 3465000 ... Start at 2006-11-16 11:17:02
41

Elapsed: 00:00:00.01
11:17:03 SQL> /

SID CLIENT_INFO
---------- ----------------------------------------------------------------------------------------------------
16 Processing 3705001 thru 3720000 ... Start at 2006-11-16 11:17:06
41

Elapsed: 00:00:00.00
11:17:06 SQL> /

SID CLIENT_INFO
---------- ----------------------------------------------------------------------------------------------------
16 Processing 3810001 thru 3825000 ... Start at 2006-11-16 11:17:07
41

Elapsed: 00:00:00.00
11:17:07 SQL> /

SID CLIENT_INFO
---------- ----------------------------------------------------------------------------------------------------
16 Processing 3990001 thru 4005000 ... Start at 2006-11-16 11:17:10
41

Elapsed: 00:00:00.00
11:17:10 SQL> /

SID CLIENT_INFO
---------- ----------------------------------------------------------------------------------------------------
16 Processing 4275001 thru 4290000 ... Start at 2006-11-16 11:17:14
41

Elapsed: 00:00:00.01
11:17:14 SQL> /

SID CLIENT_INFO
---------- ----------------------------------------------------------------------------------------------------
16 Processing 4350001 thru 4365000 ... Start at 2006-11-16 11:17:15
41

Elapsed: 00:00:00.00
11:17:15 SQL> /

SID CLIENT_INFO
---------- ----------------------------------------------------------------------------------------------------
16 Processing 4590001 thru 4605000 ... Start at 2006-11-16 11:17:18
41

Elapsed: 00:00:00.00
11:17:18 SQL> /

SID CLIENT_INFO
---------- ----------------------------------------------------------------------------------------------------
16 Processing 4815001 thru 4830000 ... Start at 2006-11-16 11:17:21
41

Elapsed: 00:00:00.00
11:17:21 SQL> /

SID CLIENT_INFO
---------- ----------------------------------------------------------------------------------------------------
16 Processing 4920001 thru 4935000 ... Start at 2006-11-16 11:17:22
41

Elapsed: 00:00:00.00
11:17:23 SQL> /

SID CLIENT_INFO
---------- ----------------------------------------------------------------------------------------------------
16 Processing 5025001 thru 5040000 ... Start at 2006-11-16 11:17:24
41

Elapsed: 00:00:00.00
11:17:24 SQL> /

SID CLIENT_INFO
---------- ----------------------------------------------------------------------------------------------------
16 Processing 5175001 thru 5190000 ... Start at 2006-11-16 11:17:26
41

Elapsed: 00:00:00.00
11:17:26 SQL> /

SID CLIENT_INFO
---------- ----------------------------------------------------------------------------------------------------
16 Processing 5340001 thru 5355000 ... Start at 2006-11-16 11:17:28
41

Elapsed: 00:00:00.01
11:17:28 SQL> /

SID CLIENT_INFO
---------- ----------------------------------------------------------------------------------------------------
16 Processing 5460001 thru 5475000 ... Start at 2006-11-16 11:17:30
41

Elapsed: 00:00:00.00
11:17:30 SQL> /

SID CLIENT_INFO
---------- ----------------------------------------------------------------------------------------------------
16 Processing 5565001 thru 5580000 ... Start at 2006-11-16 11:17:32
41

Elapsed: 00:00:00.00
11:17:32 SQL> /

SID CLIENT_INFO
---------- ----------------------------------------------------------------------------------------------------
16 Processing 5685001 thru 5700000 ... Start at 2006-11-16 11:17:33
41

Elapsed: 00:00:00.00
11:17:33 SQL> /

SID CLIENT_INFO
---------- ----------------------------------------------------------------------------------------------------
16 Processing 5745001 thru 5760000 ... Start at 2006-11-16 11:17:34
41

Elapsed: 00:00:00.00
11:17:34 SQL> /

SID CLIENT_INFO
---------- ----------------------------------------------------------------------------------------------------
16 Processing 5820001 thru 5835000 ... Start at 2006-11-16 11:17:35
41

Elapsed: 00:00:00.01
11:17:35 SQL> /

SID CLIENT_INFO
---------- ----------------------------------------------------------------------------------------------------
16 Processing 5910001 thru 5925000 ... Start at 2006-11-16 11:17:36
41

Elapsed: 00:00:00.00
11:17:37 SQL> /

SID CLIENT_INFO
---------- ----------------------------------------------------------------------------------------------------
16 Processing 6000001 thru 6015000 ... Start at 2006-11-16 11:17:38
41

Elapsed: 00:00:00.00
11:17:38 SQL> /

SID CLIENT_INFO
---------- ----------------------------------------------------------------------------------------------------
16 Processing 6195001 thru 6210000 ... Start at 2006-11-16 11:17:40
41

Elapsed: 00:00:00.00
11:17:41 SQL> /

SID CLIENT_INFO
---------- ----------------------------------------------------------------------------------------------------
16 Processing 6300001 thru 6315000 ... Start at 2006-11-16 11:17:42
41

Elapsed: 00:00:00.00
11:17:42 SQL> /

SID CLIENT_INFO
---------- ----------------------------------------------------------------------------------------------------
16 Processing 6420001 thru 6435000 ... Start at 2006-11-16 11:17:44
41

Elapsed: 00:00:00.00
11:17:44 SQL> /

SID CLIENT_INFO
---------- ----------------------------------------------------------------------------------------------------
16 Processing 6435001 thru 6450000 ... Start at 2006-11-16 11:17:44
41

Elapsed: 00:00:00.00
11:17:45 SQL> /

SID CLIENT_INFO
---------- ----------------------------------------------------------------------------------------------------
16 Processing 6435001 thru 6450000 ... Start at 2006-11-16 11:17:44
41

Elapsed: 00:00:00.00
11:17:47 SQL> l
1* select sid,client_info from v$session where username='CS_USER'
11:21:36 SQL>

tkprof分析報告
********************************************************************************

SELECT *
FROM
ECIFUPDATE.CFG_ATTR_MODIFY_HISTORY PARTITION(PART_00)


call count cpu elapsed disk query current rows
------- ------ -------- ---------- ---------- ---------- ---------- ----------
Parse 0 0.00 0.00 0 0 0 0
Execute 1 0.00 0.00 0 0 0 0
Fetch 430 35.31 41.15 59558 60003 0 6449169
------- ------ -------- ---------- ---------- ---------- ---------- ----------
total 431 35.31 41.15 59558 60003 0 6449169

Misses in library cache during parse: 0
Optimizer goal: CHOOSE
Parsing user id: 67 (recursive depth: 1)
********************************************************************************

INSERT INTO CFG_ATTR_MODIFY_HISTORY
VALUES
(:B1 ,:B2 ,:B3 ,:B4 ,:B5 ,:B6 )


call count cpu elapsed disk query current rows
------- ------ -------- ---------- ---------- ---------- ---------- ----------
Parse 0 0.00 0.00 0 0 0 0
Execute 430 39.30 39.54 2 102529 524989 6449169
Fetch 0 0.00 0.00 0 0 0 0
------- ------ -------- ---------- ---------- ---------- ---------- ----------
total 430 39.30 39.54 2 102529 524989 6449169

Misses in library cache during parse: 0
Misses in library cache during execute: 1
Optimizer goal: CHOOSE
Parsing user id: 67 (recursive depth: 1)

********************************************************************************

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/76065/viewspace-877753/,如需轉載,請註明出處,否則將追究法律責任。

相關文章