【问题标题】:How to insert 1 million records into table database Oracle using cross join如何使用交叉连接将 100 万条记录插入表数据库 Oracle
【发布时间】:2015-05-17 20:08:57
【问题描述】:

我想在 oracle DB 表中插入一百万条记录。我使用交叉连接在 mysql 中完成了类似的任务,如下所示:

1) 先插入 10 条记录。

insert into spltest_sampleapl2 values (10001, 'aaaa');
insert into spltest_sampleapl2 values (10002, 'bbbbb');
insert into spltest_sampleapl2 values (10003, 'ccccc');
insert into spltest_sampleapl2 values (10004, 'dddddd');
insert into spltest_sampleapl2 values (10005, 'eeeeeeeee');
insert into spltest_sampleapl2 values (10006, 'ffffff');
insert into spltest_sampleapl2 values (10007, 'gggggggg');
insert into spltest_sampleapl2 values (10008, 'hhhhhh');
insert into spltest_sampleapl2 values (10009, 'iiiiii');
insert into spltest_sampleapl2 values (10010, 'jjjjjj');
commit;

2) 使用用户变量

set @num := 10010;

3) 使用单连接插入记录

insert into apl2 (id, data) select (@num := @num + 1) ,s1.data from apl2 s1, apl2 s2, apl2 s3, apl2 s4,apl2 s5, apl2 s6;
commit;

现在我想在 Oracle 中的类似架构上做同样的事情。怎么办?

【问题讨论】:

    标签: mysql database oracle cross-join


    【解决方案1】:

    设置一个序列并将其用于自动编号:

    create sequence seq_apl2;
    
    insert into apl2(id, data)
        select seq_apl2.nextval, s1.data
        from apl2 s1 cross join apl2 s2 cross join
             apl2 s3 cross join apl2 s4 cross join
             apl2 s5 cross join apl2 s6;
    

    编辑:

    如果您没有使用序列的能力,请使用row_number()

    insert into apl2(id, data)
        select row_number() over (order by NULL), s1.data
        from apl2 s1 cross join apl2 s2 cross join
             apl2 s3 cross join apl2 s4 cross join
             apl2 s5 cross join apl2 s6;
    

    您可以根据需要添加偏移量。

    【讨论】:

    • 这样我怎么能确定主键范围?
    • @SubhamTripathi 。 . .您可以从您喜欢的任何值开始序列,或者将其更改为从另一个值开始 (docs.oracle.com/cd/B28359_01/server.111/b28310/…)。
    • 我的用户没有创建序列的权限,有没有其他解决方案。
    • 你可以使用Oracle 12,它支持在create table语句中自动增加id。
    • 当我在上面使用(编辑的解决方案)时,它会出现某种错误。但是当我使用这个 :: insert into apl2(id, data) select rownum ,s1.data from apl2 s1, apl2 s2, apl2 s3, apl2 s4, apl2 s5, apl2 s6 where rownum < 1000001; 它运行正常
    【解决方案2】:

    创建一个包含 10 条记录的表,编号为 0 到 10:

    INSERT INTO t (n) VALUES (0);
    INSERT INTO t (n) VALUES (1);
    ...
    INSERT INTO t (n) VALUES (9);
    

    现在select 交叉连接,使用尽可能多的别名 10^n 计数:

    对于 100 条记录:

    INSERT INTO X 
    SELECT t2.n*10 + t1.n FROM t t1, t t2
    

    对于 1000 条记录:

    INSERT INTO X 
    SELECT t3.n*100 + t2.n*10 + t1.n FROM t t1, t t2, t t3
    

    对于 1,000,000 条记录:

    INSERT INTO X 
    SELECT t6.n*100000 + t5.n * 10000 + t4.n*1000 + t3.n*100 + t2.n*10 + t1.n FROM t t1, t t2, t t3
    

    我很确定这是适用于任何平台的普通 SQL...

    【讨论】:

      【解决方案3】:

      首先我插入了 id 从 1000000 到 1000010 的记录(10 条记录),然后我使用了以下命令。

      insert into apl2(id, data) select rownum ,s1.data from apl2 s1, apl2 s2, apl2 s3, apl2 s4, apl2 s5, apl2 s6 where rownum < 1000001;
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-09-04
        • 2017-01-31
        • 1970-01-01
        • 2022-11-12
        • 1970-01-01
        相关资源
        最近更新 更多