[英]How to insert 1 million records into table database Oracle using cross join
我想在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中的类似架构上执行相同的操作。 怎么做 ?
创建一个包含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 ...
设置一个序列并将其用于自动编号:
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;
您可以根据需要添加偏移量。
首先我插入了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;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.