2016-02-25 29 views
1

我正在尝试解决由ORMs封装的SQLite固有的999变量限制,例如peewee。我正在尝试构建几行〜50k行和〜20列的表格。但是,由于999的限制,我必须将插入限制为每插入语句大约50行。这非常慢。sqlite - 快速构建主键的表

我该如何让这个更快?如果我没有主键约束,那么这个要求就会消失,因为我可以直接使用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变量,但我担心将此变量增加到某个庞大数字的影响。

+0

无论问题是什么,每个INSERT语句的行数太少不是它。 –

回答

0

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