简体   繁体   English

SELECT 的 SQLAlchemy 性能比与 Oracle 的原始连接慢 2 倍

[英]SQLAlchemy performance of SELECT is 2x slower than a raw connection to Oracle

I am timing get_core and get_orm against get_raw with the following results:我正在针对 get_raw 计时 get_core 和 get_orm ,结果如下:

get_raw got 10000 rows, time: 0:00:04.201974
get_core got 10000 rows, time: 0:00:08.048311
get_orm got 10000 rows, time: 0:00:08.768862

Full code:完整代码:

from datetime import datetime
from sqlalchemy import Column, Integer, String
from sqlalchemy.orm import sessionmaker
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.sql import select
Base = declarative_base()

class TbPerformanceTest(Base):
    __tablename__ = 'tb_performance_test'
    i = Column(Integer, primary_key=True)
    s = Column(String(100))

def get_raw(engine):
    conn = engine.raw_connection()
    cursor = conn.cursor()
    cursor.execute('select * from tb_performance_test')
    return cursor.fetchall()

def get_core(engine):
    sel = select([TbPerformanceTest.__table__])
    return list(engine.execute(sel))

def get_orm(engine):
    session = sessionmaker(bind=engine)()
    return list(session.query(TbPerformanceTest))

def main():
    engine = create_engine('engine connection string')
    TbPerformanceTest.__table__.create(engine)
    session = sessionmaker(bind=engine)()
    for i in range(10000):
        session.add(TbPerformanceTest(i=i, s=str(i)))
    session.commit()

    for func in get_raw, get_core, get_orm:
        start = datetime.now()
        rows = len(func(engine))
        print(f'{func.__name__} got {rows} rows, time: {datetime.now() - start}')

sqlalchemy version 1.2.19 oracle 12c sqlalchemy 版本 1.2.19 oracle 12c

There is a performance page in SQLAlchemy documentation but I found it unhelpful. SQLAlchemy 文档中有一个性能页面,但我发现它没有帮助。

Please feel free to run this code and share your results.请随时运行此代码并分享您的结果。 Thank you.谢谢你。

Edit:编辑:

What is your question?你的问题是什么?

The question is what kind of defect leads to this and how to fix SQLAlchemy performance with Oracle.问题是哪种缺陷会导致这种情况以及如何使用 Oracle 修复 SQLAlchemy 性能。

The disparity is very large and cannot be explained by "sqlalchemy does more": I added code to wrap all rows received from raw connection into ORM classes and it only added a small fraction to the total time.差异非常大,不能用“sqlalchemy 做得更多”来解释:我添加了代码来将从原始连接接收到的所有行包装到 ORM 类中,它只增加了总时间的一小部分。

I observe it with all our tables and views.我用我们所有的表格和视图观察它。 With larger loads it scales up to stay at 2x.随着更大的负载,它可以放大以保持 2 倍。 Eg Raw connections fetches records in 12 seconds, while SQLAlchemy takes 24.例如,原始连接在 12 秒内获取记录,而 SQLAlchemy 需要 24 秒。

First off, SQLAlchemy 1.2.19 is pretty old (released 2019-04-15, over 3 years ago).首先,SQLAlchemy 1.2.19 已经很老了(2019-04-15 发布,3 年前)。 That said …那就是说……

Benchmarking can be tricky because the results may depend on many factors.基准测试可能很棘手,因为结果可能取决于许多因素。 I ran your test on a 10-year-old notebook using SQLAlchemy 1.4.36 and cx-Oracle 8.3.0 and I got我使用 SQLAlchemy 1.4.36 和 cx-Oracle 8.3.0 在一个有 10 年历史的笔记本上运行了你的测试,我得到了

output输出 rows/sec行/秒 remarks评论
get_raw got 10000 rows, time: 0:00:01.672070 get_raw 有 10000 行,时间:0:00:01.672070 5981 5981
get_core got 10000 rows, time: 0:00:02.065249 get_core 有 10000 行,时间:0:00:02.065249 4842 4842 19% less than raw比生的少 19%
get_orm got 10000 rows, time: 0:00:02.752992 get_orm 有 10000 行,时间:0:00:02.752992 3632 3632 25% less than core比核心少 25%

The raw DBAPI connection appears to be significantly faster, but remember that SQLAlchemy is doing more work.原始 DBAPI 连接似乎要快得多,但请记住 SQLAlchemy 正在做更多的工作。 cx-Oracle ("raw") returns a list of simple tuples. cx-Oracle ("raw") 返回一个简单元组的列表。 SQLAlchemy Core returns SQLAlchemy Row -like objects that offer additional features, and SQLAlchemy ORM goes even farther by converting each row into an ORM object. SQLAlchemy Core 返回提供附加功能的类似 SQLAlchemy Row的对象,而 SQLAlchemy ORM 通过将每一行转换为 ORM 对象更进一步。

Your post title claims that SQLAlchemy is twice as slow as cx-Oracle.您的帖子标题声称 SQLAlchemy 的速度是 cx-Oracle 的两倍。 My results show that not to be true, at least not always.我的结果表明这不是真的,至少并非总是如此。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM