日期:2014-05-16  浏览次数:20503 次

使用bulk collect insert实现大数据快速迁移(转)

(miki西游 @mikixiyou 原文链接: http://mikixiyou.iteye.com/blog/1705838 )

?

?

在数据库管理中,会出现限制某个IP访问数据库的连接数或某个用户访问数据库的连接数的需求。

对于用户访问数据库的连接数限制,我们可以从数据库的profile上着手,使用profile的特性实现该需求。

对于IP访问数据库的连接数限制,从数据库上可以使用logon on database触发器来实现。

?

每一次新会话登录,都将IP记录在vrsession的client_info中,然后count出所有符合条件的会话数目,如果超过了,就直接断开会话连接。

但这个会话连接数据库如果限制了,也只能对非dba角色的用户生效。dba用户只会在alert.log中写一个警告信息而已。

?

实现该功能的触发器代码如下:

?

create table  t_target
  (id         number,
    owner       varchar2(30),
    object_name varchar2(128),
    object_id   number,
    xx          date,
    yy          varchar2(10)
  ) 

?

?

源表为dba_objects ,表结构在数据库中可以查到。

?

需要将源表dba_objects 中两个字段object_name,owner 两个字段的值复制到t_target 表中。

?

declare

  type t_array is table of t_target%rowtype;

  t_data t_array;

  cursor c is

    select null id, owner, object_name, null object_id, null xx, null yy

      from dba_objects;

begin

  open c;

  loop

    fetch c bulk collect

      into t_data limit 100;

    forall i in 1 .. t_data.count

      insert into t_target values t_data (i);

    exit when c%notfound;

  end loop;

  close c;

  commit;

end; 

?

?

Fastest

  1. Explicit cursors doing a BULK COLLECT into a local collection (with an appropriate LIMIT) and using FORALL to write back to the database.
  2. Implicit cursors doing a BULK COLLECT for you behind the scenes along with single-row writes back to the datbase.
  3. Explicit cursors that are not doing a BULK COLLECT and not taking advantage of PL/SQL collections.

?

顺便说一下,这里开发人员多,如果碰到类似要求的,各位可以测试一下这个方法。试试在更大数据量的执行时间和成本会变的如何?

?

本人测试

?

我迁移了一个334W的表中两个字段,用了40秒,不知道这个速度怎么样,呵呵.

?

?