SQL,如何为引用连接表创建大量记录?

时间:2012-02-22 17:14:51

标签: sql sqlite

目前,我正在使用它,但它有点慢,我最终只有1331条记录。我认为必须有更快的方法来制作更多的记录?

CREATE TABLE IF NOT EXISTS util_nums (n integer primary key 
  autoincrement not null);

insert into util_nums values (0);

insert into util_nums(n) select null from (select 0 as n union select 1 
union select 2 union select 3 union select 4 union select 5 union select 6 
union select 7 union select 8 union select 9 union select 10) 
a cross join (select 0 as n union select 1 union select 2 union select 3 
union select 4 union select 5 union select 6 union select 7 union select 8 
union select 9 union select 10) b cross join (select 0 as n union select 1 
union select 2 union select 3 union select 4 union select 5 union select 6 
union select 7 union select 8 union select 9 union select 10) c;

我正在使用sqlite

2 个答案:

答案 0 :(得分:0)

创建包含所有数据的文件,然后从文件中批量插入表中。

See this

CREATE TABLE tmp_util_nums
(
 n int NOT NULL,

)
go

DECLARE @SQL varchar(max)
Declare @PathFileName varchar(max) 
SET @SQL = "BULK INSERT tmp_util_nums FROM '"+@PathFileName+"' WITH (FIELDTERMINATOR = ',') "

- 步骤2:执行BULK INSERT语句     EXEC(@SQL)

- 步骤3:将数据插入到最终表中     INSERT到util_nums     SELECT * FROM tmp_util_nums

TRUNCATE TABLE  tmp_util_nums
go

答案 1 :(得分:0)

我不确定它是否更快,但你可以尝试

--assuming table is empty, insert 2 records
insert into util_nums values (null);
insert into util_nums values (null); 

insert into util_nums 
select null from     
util_nums,util_nums,util_nums,util_nums,util_nums,
util_nums,util_nums,util_nums,util_nums,util_nums,
util_nums,util_nums,util_nums,util_nums,util_nums,
util_nums,util_nums,util_nums,util_nums,util_nums

它会插入2 ^ 20(如果您需要更多,只需将另一个util_nums添加到from)+ 2个记录,速度非常快。