【发布时间】:2016-06-08 17:34:03
【问题描述】:
当被诸如peewee 之类的 ORM 包装时,我正在尝试解决 SQLite 中固有的 999 变量限制。我正在尝试构建几十个表,每个表有 ~50k 行和 ~20 列。但是,由于 999 的限制,我必须将我的插入限制为每个插入语句约 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 变量,但我担心将这个变量增加到一个巨大的数字的影响。
【问题讨论】:
-
不管是什么问题,不是每个 INSERT 语句的行数太少。