当被诸如peewee
之类的ORM包装时,我正在尝试解决SQLite固有的999变量限制。我正在尝试构建几十个表,每个表有大约50k行和大约20列。但是,由于999的限制,我必须将每个insert语句的插入限制为~50行。这是非常慢的。
我怎么才能让它更快呢?如果我没有主键约束,那么这个要求就不存在了,因为我可以只使用pandas
直接转储到SQL,但是稍后修改以获得主键是一件很痛苦的事情。
下面是一个示例:
from peewee import *
database = SqliteDatabase(None)
class Base(Model):
class Meta:
database = database
colnames = ["A", "B", "C", "D", "E", "F", "G", "H"]
cols = {x: TextField() for x in colnames}
table = type('mytable', (Base,), cols)
database.init('test.db')
database.create_tables([table])
data = []
for x in range(150): # if this number is any higher this crashes
data.append({x: 1 for x in colnames})
with database.atomic() as txn:
table.insert_many(data).execute()
我怎样才能绕过这个限制呢?在peewee
文档中,他们提到使用apsw
,它能够修改SQLite max_variables变量,但我担心将此变量增加到某个巨大数字的效果。
发布于 2016-02-26 20:56:33
确保您的insert
语句在transaction中执行,否则将为每个语句打开一个隐式事务,这相当耗时。许多连续语句的执行速度应该会快得多。
https://stackoverflow.com/questions/35635850
复制相似问题