【问题标题】:sqlite - rapidly constructing table with primary key [duplicate]sqlite - 使用主键快速构建表[重复]
【发布时间】: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 语句的行数太少。

标签: python sqlite


【解决方案1】:

确保您的insert 语句在transaction 内执行,否则将为每个语句打开一个隐式事务,这相当耗时。很多连续语句的执行应该会快得多。

【讨论】:

    猜你喜欢
    • 2014-12-03
    • 1970-01-01
    • 2014-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多