Oracle 逐條和批量插入資料方式對比

來源:互聯網
上載者:User

標籤:

建立測試表

create table base_users

(

  userid         varchar2(16),

  username  varchar2(32), 

  passwd      varchar2(16)

)tablespace cat_data;

採用一條一條插入的方式

create or replace procedure insert_data_one_by_one(n in number)

as

    currentId number(16) := 0;

    l_userid varchar2(16);

    l_username varchar2(32);


    sqltext varchar2(256);


begin
    dbms_output.put_line(‘begin ...‘ || to_char(current_timestamp, ‘HH24:MI:SSxFF‘));

    sqltext := ‘insert into base_users(userid, username, passwd) values(:userid, :username,‘ || ‘111111 )‘;


    loop
        currentId:=currentId + 1;
        l_userid:= to_char(currentId);
        l_username:= to_char(18600000000 + currentId);

        execute immediate sqltext using l_userid, l_username;
        exit when currentId >= n;
    end loop;
    commit;
    dbms_output.put_line(‘end commit ...‘ || to_char(current_timestamp, ‘HH24:MI:SSxFF‘));
end insert_data_one_by_one;
/

採用批量插入的方式

create or replace procedure insert_data_bulk(n in number)

as

  i int;

  tmp_userid         number;

  tmp_username  number;

  type useridArray is table of varchar2(16) index by binary_integer;

  type usernameArray is table of varchar2(32) index by binary_integer;

  puserid useridArray;

  pusername usernameArray;

begin

  dbms_output.put_line(‘begin ...‘ || to_char(current_timestamp, ‘HH24:MI:SSxFF‘));

  tmp_userid := 1;

  tmp_username := 18600000000;

  for i in 1 .. n loop

    puserid(i) := tmp_userid;

    pusername(i) := tmp_username;

    tmp_userid := tmp_userid + 1;

    tmp_username := tmp_username + 1;

  end loop;

  forall i in 1 ..n

    insert into base_users(userid, username, passwd)

                 values(puserid(i), pusername(i), ‘111111‘);

  commit;

  dbms_output.put_line(‘end ...‘ || to_char(current_timestamp, ‘HH24:MI:SSxFF‘));

endinsert_data_bulk;

/

測試1千萬條資料的插入

SQL>set serveroutput on

SQL>begin

insert_data_one_by_one(10000000);

end;

/

begin ...22:14:01.572928000
end commit ...22:20:43.911104000

 

 

SQL>truncate table base_users;

 


SQL>begin

insert_data_bulk(10000000);

end;

/

begin ...22:25:31.497810000
end ...22:27:23.801515000

Oracle 逐條和批量插入資料方式對比

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.