在MySQL中,你可以像这样插入多行:
INSERT INTO 'tablename' ('column1', 'column2') VALUES
('data1', 'data2'),
('data1', 'data2'),
('data1', 'data2'),
('data1', 'data2');
然而,当我尝试这样做时,我得到了一个错误。是否可以在SQLite数据库中一次插入多行?这样做的语法是什么?
在MySQL中,你可以像这样插入多行:
INSERT INTO 'tablename' ('column1', 'column2') VALUES
('data1', 'data2'),
('data1', 'data2'),
('data1', 'data2'),
('data1', 'data2');
然而,当我尝试这样做时,我得到了一个错误。是否可以在SQLite数据库中一次插入多行?这样做的语法是什么?
当前回答
如果你正在使用bash shell,你可以使用这个:
time bash -c $'
FILE=/dev/shm/test.db
sqlite3 $FILE "create table if not exists tab(id int);"
sqlite3 $FILE "insert into tab values (1),(2)"
for i in 1 2 3 4; do sqlite3 $FILE "INSERT INTO tab (id) select (a.id+b.id+c.id)*abs(random()%1e7) from tab a, tab b, tab c limit 5e5"; done;
sqlite3 $FILE "select count(*) from tab;"'
或者如果你在sqlite CLI中,那么你需要这样做:
create table if not exists tab(id int);"
insert into tab values (1),(2);
INSERT INTO tab (id) select (a.id+b.id+c.id)*abs(random()%1e7) from tab a, tab b, tab c limit 5e5;
INSERT INTO tab (id) select (a.id+b.id+c.id)*abs(random()%1e7) from tab a, tab b, tab c limit 5e5;
INSERT INTO tab (id) select (a.id+b.id+c.id)*abs(random()%1e7) from tab a, tab b, tab c limit 5e5;
INSERT INTO tab (id) select (a.id+b.id+c.id)*abs(random()%1e7) from tab a, tab b, tab c limit 5e5;
select count(*) from tab;
它是如何工作的? 它使用if表选项卡:
id int
------
1
2
然后从TAB a中选择a.id, b.id, TAB b返回
a.id int | b.id int
------------------
1 | 1
2 | 1
1 | 2
2 | 2
等等。第一次执行后,插入2行,然后2^3=8。(三个,因为我们有TAB a TAB b TAB c)
在第二次执行之后,我们插入额外的(2+8)^3=1000行
第三次之后,我们插入大约max(1000^ 3,5e5)=500000行,依此类推……
这是我所知道的填充SQLite数据库的最快方法。
其他回答
正如其他人所说,SQLite不支持这种语法。我不知道复合insert是否是SQL标准的一部分,但根据我的经验,它们在许多产品中都没有实现。
顺便说一句,您应该意识到,如果在显式事务中包装多个INSERT, SQLite中的INSERT性能将大大提高。
是的,这是可能的,但不是通常的逗号分隔的插入值。
试试这个…
insert into myTable (col1,col2)
select aValue as col1,anotherValue as col2
union select moreValue,evenMoreValue
union...
是的,这有点丑陋,但是从一组值自动生成语句足够简单。而且,似乎只需要在第一个选择中声明列名。
如果你正在使用bash shell,你可以使用这个:
time bash -c $'
FILE=/dev/shm/test.db
sqlite3 $FILE "create table if not exists tab(id int);"
sqlite3 $FILE "insert into tab values (1),(2)"
for i in 1 2 3 4; do sqlite3 $FILE "INSERT INTO tab (id) select (a.id+b.id+c.id)*abs(random()%1e7) from tab a, tab b, tab c limit 5e5"; done;
sqlite3 $FILE "select count(*) from tab;"'
或者如果你在sqlite CLI中,那么你需要这样做:
create table if not exists tab(id int);"
insert into tab values (1),(2);
INSERT INTO tab (id) select (a.id+b.id+c.id)*abs(random()%1e7) from tab a, tab b, tab c limit 5e5;
INSERT INTO tab (id) select (a.id+b.id+c.id)*abs(random()%1e7) from tab a, tab b, tab c limit 5e5;
INSERT INTO tab (id) select (a.id+b.id+c.id)*abs(random()%1e7) from tab a, tab b, tab c limit 5e5;
INSERT INTO tab (id) select (a.id+b.id+c.id)*abs(random()%1e7) from tab a, tab b, tab c limit 5e5;
select count(*) from tab;
它是如何工作的? 它使用if表选项卡:
id int
------
1
2
然后从TAB a中选择a.id, b.id, TAB b返回
a.id int | b.id int
------------------
1 | 1
2 | 1
1 | 2
2 | 2
等等。第一次执行后,插入2行,然后2^3=8。(三个,因为我们有TAB a TAB b TAB c)
在第二次执行之后,我们插入额外的(2+8)^3=1000行
第三次之后,我们插入大约max(1000^ 3,5e5)=500000行,依此类推……
这是我所知道的填充SQLite数据库的最快方法。
INSERT INTO TABLE_NAME
(DATA1, DATA2)
VALUES (VAL1, VAL2),
(VAL1, VAL2),
(VAL1, VAL2),
(VAL1, VAL2),
(VAL1, VAL2),
(VAL1, VAL2),
(VAL1, VAL2),
(VAL1, VAL2);
你不能,但我不认为你错过了什么。
因为sqlite总是在进程中调用,所以执行1条插入语句还是100条插入语句对性能几乎没有影响。然而,提交需要花费很多时间,所以将这100个插入放在事务中。
当你使用参数化查询时,Sqlite要快得多(需要的解析要少得多),所以我不会像这样串联大语句:
insert into mytable (col1, col2)
select 'a','b'
union
select 'c','d'
union ...
它们需要一遍又一遍地解析,因为每个连接的语句都是不同的。