使用 Python 和 Sqlalchemy 将相同的值作为布尔值或整数存储在 sqlite 数据库中会产生以下结果。
Value stored as Boolean:
SqlAlchemy ORM: Total time for 40000 records 62.5009999275 secs
SqlAlchemy Core: Total time for 40000 records 56.0600001812 secs
Value stored as Integer:
SqlAlchemy ORM: Total time for 40000 records 5.72099995613 secs
SqlAlchemy Core: Total time for 40000 records 0.770999908447 secs
为什么在使用布尔类型时会出现这样的性能问题?
我知道 SQLite 没有布尔类型的概念,而是将它们存储为整数 1(真)或 0(假)。我会假设 SqlAlchemy 只是将 python bool 映射到 Sqlite 整数。
用于生成上述输出的脚本(从这个问题修改):
import time
import sqlite3
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, Integer, String, create_engine, Boolean
from sqlalchemy.orm import scoped_session, sessionmaker
Base = declarative_base()
DBSession = scoped_session(sessionmaker())
class CustomerInteger(Base):
__tablename__ = "customerInteger"
id = Column(Integer, primary_key=True)
name = Column(String(255))
value = Column(Integer)
class CustomerBoolean(Base):
__tablename__ = "customerBoolean"
id = Column(Integer, primary_key=True)
name = Column(String(255))
value = Column(Boolean)
def init_sqlalchemy(dbname = 'sqlite:///sqlalchemy.db'):
global engine
engine = create_engine(dbname, echo=False)
DBSession.remove()
DBSession.configure(bind=engine, autoflush=False, expire_on_commit=False)
Base.metadata.drop_all(engine)
Base.metadata.create_all(engine)
def test_sqlalchemy_orm(n, table):
init_sqlalchemy()
t0 = time.time()
for i in range(n):
customer = table()
customer.name = 'NAME ' + str(i)
customer.value = True
DBSession.add(customer)
if i % 1000 == 0:
DBSession.flush()
DBSession.commit()
print "SqlAlchemy ORM: Total time for " + str(n) + " records " + str(time.time() - t0) + " secs"
def test_sqlalchemy_core(n, table):
init_sqlalchemy()
t0 = time.time()
engine.execute(
table.__table__.insert(),
[{"name":'NAME ' + str(i), "value":True } for i in range(n)]
)
print "SqlAlchemy Core: Total time for " + str(n) + " records " + str(time.time() - t0) + " secs"
if __name__ == '__main__':
print "Value stored as Boolean:"
test_sqlalchemy_orm(40000, CustomerBoolean)
test_sqlalchemy_core(40000, CustomerBoolean)
print "Value stored as Integer:"
test_sqlalchemy_orm(40000, CustomerInteger)
test_sqlalchemy_core(40000, CustomerInteger)