create table table1(id number primary key  not null, no number)
往表中插入1到1000W连续的记录,用哪种方法效率高些?--1
insert into table1(id, no)
       select level, level
         from dual
        connect by level <= 10000000;
--2
  forall i in 1..10000000 
      insert into table1(id, no) values(i,i);1还是2,或者说大家还有其他更好的方法吗?

解决方案 »

  1.   

    说实话,你的两个方法,效率 都不咋的。
    原因:
    1. 你的表上已经建了主键了
    2. 没有利用相关特性
    推荐 做法:
    1. 建的表不要主键:
    2. insert /*+append*/ into table1(id, no) select level, level
             from dual
            connect by level <= 10000000;3. 执行完之后再为表建主键
    这样可以尽量的减少日志并提高插入速度。
      

  2.   

    请教:--2
      forall i in 1..10000000 
          insert into table1(id, no) values(i,i);
    这个怎么实现?
      

  3.   

      
       insert /*+append*/ into table1(id, no) values(i,i);
      

  4.   

    两者没有太大的差异。建议
    1. 先插入在建index
    2. nologging的hint
    3. 及时commmit,释放资源和减小undo的开销
      

  5.   

    在2楼的基础上补充下,insert /*+append */ into table1 nologging ……另外,如果用循环的方法插数据的话,记得用动态sql绑定变量。begin
     for i in 1..1000000
     loop
      execute immediate 'insert into table1 values(:a,:b)' using i,i;
    end loop;
    commit;
    end;
      

  6.   

    批插入200W
    commmit
    批插入200W
    commmit
    批插入200W
    commmit
    批插入200W
    commmit
    批插入200W
    commmit
    可能会好些