我编写了一个脚本来对插入和删除进行基准测试。
import os, time
abspath = os.path.abspath(os.path.dirname(__file__))
dbname = 'test.sqlite'
# dbname = ':memory:'
databaseFileName = os.path.join(abspath, dbname)
if os.path.exists(databaseFileName):
os.remove(databaseFileName)
from sqlalchemy import \
Table, Column, MetaData, create_engine,\
Integer, DateTime
engine = create_engine('sqlite:///' + dbname)
metadata = MetaData()
test = Table ('test', metadata,
Column('id', Integer, primary_key=True)
)
metadata.create_all(engine)
conn = engine.connect()
numRecords = 100
start = time.clock()
for i in range(numRecords):
conn.execute(test.insert())
print 'It took %s seconds to insert %s records' % ((time.clock() - start), numRecords)
start = time.clock()
for i in range(1, numRecords+1):
conn.execute(test.delete().where(test.c.id == i))
print 'It took %s seconds to delete %s records' % ((time.clock() - start), numRecords)
在Windows上打印
It took 5.32831616059 seconds to insert 100 records
It took 6.76065831351 seconds to delete 100 records
在Mac上打印
It took 0.036788 seconds to insert 100 records
It took 0.041629 seconds to delete 100 records
为什么在Mac上这么快?是因为Mac使用HFS +而Windows使用NTFS吗?
答案 0 :(得分:2)
显然Mac和Win上的SQLite性能一直是个问题。请参阅此讨论from 2012。
一个关键功能似乎是同步参数。
来自讨论:
在Windows上,Sqlite默认使用真实/完整fsyncs到硬件 由操作系统提供,至少在OS / X上,默认情况下不是。 见http://sqlite.org/pragma.html#pragma_fullfsync
你可以尝试使用pragma sync = off吗?