使用peewee在postgres中迭代超过1k +行时的开销

时间:2017-11-15 17:34:20

标签: python python-3.x postgresql psycopg2 peewee

在迭代postgres表时,我看到了一个令人费解的巨大开销。

我分析了代码,并对SQLAlchemy进行了冒烟测试,以确保它不是慢速连接或底层驱动程序(psycopg2)。

在一个约1M记录的postgres表中运行它,但只获取其中的一小部分。

import time

import peewee
import sqlalchemy
from playhouse import postgres_ext
from sqlalchemy.dialects.postgresql import JSONB
from sqlalchemy.engine.url import URL as AlchemyURL
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker as alchemy_sessionmaker

user = 'XXX'
password = 'XXX'
database = 'XXX'
host = 'XXX'
port = 5432

table = 'person'
limit = 1000

peewee_db = postgres_ext.PostgresqlExtDatabase(
    database=database,
    host=host, port=port,
    user=user, password=password,
    use_speedups=True,
    server_side_cursors=True,
    register_hstore=False,
)

alchemy_engine = sqlalchemy.create_engine(AlchemyURL('postgresql', username=user, password=password,
                                                     database=database, host=host, port=port))
alchemy_session = alchemy_sessionmaker(bind=alchemy_engine)()


class PeeweePerson(peewee.Model):
    class Meta:
        database = peewee_db
        db_table = table

    id = peewee.CharField(primary_key=True, max_length=64)
    data = postgres_ext.BinaryJSONField(index=True, index_type='GIN')


class SQLAlchemyPerson(declarative_base()):
    __tablename__ = table

    id = sqlalchemy.Column(sqlalchemy.Integer, primary_key=True)
    data = sqlalchemy.Column(JSONB)


def run_raw_query():
    ids = list(peewee_db.execute_sql(f"SELECT id from {table} order by id desc limit {limit}"))
    return ids


def run_peewee_query():
    query = PeeweePerson.select(PeeweePerson.id).order_by(PeeweePerson.id.desc()).limit(limit)
    ids = list(query.tuples())
    return ids


def run_sqlalchemy_query():
    query = alchemy_session.query(SQLAlchemyPerson.id).order_by(sqlalchemy.desc(SQLAlchemyPerson.id)).limit(limit)
    ids = list(query)
    return ids


if __name__ == '__main__':
    t0 = time.time()
    raw_result = run_raw_query()
    t1 = time.time()
    print(f'Raw: {t1 - t0}')

    t2 = time.time()
    sqlalchemy_result = run_sqlalchemy_query()
    t3 = time.time()
    print(f'SQLAlchemy: {t3 - t2}')

    t4 = time.time()
    peewee_result = run_peewee_query()
    t5 = time.time()
    print(f'peewee: {t5 - t4}')

    assert raw_result == sqlalchemy_result == peewee_result
  • 限制= 1000:

    原始:0.02643609046936035
    SQLAlchemy:0.03697466850280762
    撒尿:1.0509874820709229

  • 限制= 10000

    原始:0.15931344032287598
    SQLAlchemy:0.07229042053222656
    撒尿:10.82826042175293

这两个示例都使用服务器端游标。

我简要介绍了这一点,看起来95%以上的时间用于调用cursor.fetchone https://github.com/coleifer/peewee/blob/d8e34b0682d87bd56c1a3636445d9c0fccf2b1e2/peewee.py#L2340

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

这似乎与Peewee 2.x中服务器端游标的实现效率低下有关。具体来说,我认为这是因为peewee的游标包装器使用.fetchone()db-api而不是获取许多行。 3.0a有一个新的实现应该更快:https://github.com/coleifer/peewee/commit/0ae17c519475c935d9db3c338f36ef058a3f879c

此外,在2.x中使用客户端游标没有这些效率问题,因此可以暂时用作解决方法。