ITPub博客

首页 > 数据库 > Oracle > ORACLE批量操作

ORACLE批量操作

原创 Oracle 作者:csbin 时间:2012-03-31 18:25:41 0 删除 编辑

ORACLE批量操作

---------------------------------------------------------------------------
-- 创建表
----------------------------------------------------------------------------
drop table demo;
create table demo (id number, name varchar2(255));
----------------------------------------------------------------------------
-- 批量操作
----------------------------------------------------------------------------

declare
type id_table_type is table of number index by binary_integer;
type name_table_type is table of varchar2(255) index by binary_integer;
id_table id_table_type;
name_table name_table_type;
start_time number;
end_time number;
v_count number := 100000;
begin
--------------------------------------------------------------------------
-- 1、不使用批量插入数据
--------------------------------------------------------------------------
for i in 1 .. v_count loop
id_table(i) := i;
name_table(i) := 'name' || i;
end loop;

start_time := dbms_utility.get_time;
for i in 1 .. v_count loop
insert /*+append*/
into demo
values
(id_table(i), name_table(i));
end loop;
end_time := dbms_utility.get_time;

dbms_output.put_line('不用批量插入' || v_count || '条:' ||
lpad(to_char((end_time - start_time) / 100), 5, ' '));
--------------------------------------------------------------------------
-- 2、使用批量插入数据
--------------------------------------------------------------------------
for i in v_count + 1 .. v_count * 2 loop
id_table(i) := i;
name_table(i) := 'name' || i;
end loop;

start_time := dbms_utility.get_time;
forall i in v_count + 1 .. v_count * 2
insert /*+append*/
into demo
values
(id_table(i), name_table(i));
end_time := dbms_utility.get_time;

dbms_output.put_line(' 用批量插入' || v_count || '条:' ||
lpad(to_char((end_time - start_time) / 100), 5, ' '));
--------------------------------------------------------------------------
-- 3、使用批量更新数据
--------------------------------------------------------------------------
for i in 1 .. v_count loop
id_table(i) := i;
name_table(i) := 'NB' || i;
end loop;

start_time := dbms_utility.get_time;
for i in 1 .. v_count loop
update demo set name = name_table(i) where id = id_table(i);
end loop;
end_time := dbms_utility.get_time;

dbms_output.put_line('不用批量更新' || v_count || '条:' ||
lpad(to_char((end_time - start_time) / 100), 5, ' '));

start_time := dbms_utility.get_time;
forall i in v_count + 1 .. v_count * 2
update demo set name = name_table(i) where id = id_table(i);
end_time := dbms_utility.get_time;

dbms_output.put_line(' 用批量更新' || v_count || '条:' ||
lpad(to_char((end_time - start_time) / 100), 5, ' '));
--------------------------------------------------------------------------
-- 4、使用批量删除数据
--------------------------------------------------------------------------
for i in 1 .. v_count loop
id_table(i) := i;
end loop;

start_time := dbms_utility.get_time;
forall i in 1 .. id_table.count
delete from demo where id = id_table(i);
end_time := dbms_utility.get_time;

dbms_output.put_line(' 用批量删除' || v_count || '条:' ||
lpad(to_char((end_time - start_time) / 100), 5, ' '));
end;
/

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/720091/viewspace-1057796/ ,如需转载,请注明出处,否则将追究法律责任。

下一篇: informix子查询
请登录后发表评论 登录
全部评论

注册时间:2009-01-12

  • 博文量
    69
  • 访问量
    1479823