]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
fix / modernize short_selects example
authorMike Bayer <mike_mp@zzzcomputing.com>
Fri, 5 Dec 2025 22:41:56 +0000 (17:41 -0500)
committerMike Bayer <mike_mp@zzzcomputing.com>
Fri, 5 Dec 2025 22:44:57 +0000 (17:44 -0500)
Fixed the "short_selects" performance example where the cache was being
used in all the examples, making it impossible to compare performance with
and without the cache.   Less important comparisons like "lambdas" and
"baked queries" have been removed.

Change-Id: Ia55391ba23e01d2ed136c84f9c34bb16689ce10e

doc/build/changelog/unreleased_20/fix_examples.rst [new file with mode: 0644]
examples/performance/short_selects.py

diff --git a/doc/build/changelog/unreleased_20/fix_examples.rst b/doc/build/changelog/unreleased_20/fix_examples.rst
new file mode 100644 (file)
index 0000000..bf77452
--- /dev/null
@@ -0,0 +1,8 @@
+.. change::
+    :tags: bug, examples
+
+    Fixed the "short_selects" performance example where the cache was being
+    used in all the examples, making it impossible to compare performance with
+    and without the cache.   Less important comparisons like "lambdas" and
+    "baked queries" have been removed.
+
index bc6a9c79ac471f48261a4dce14acd3e2a8f9389e..6ff187bf0f9efd0c7712c6f34fb21b887fa18c09 100644 (file)
@@ -13,12 +13,10 @@ from sqlalchemy import Identity
 from sqlalchemy import Integer
 from sqlalchemy import select
 from sqlalchemy import String
-from sqlalchemy.ext import baked
 from sqlalchemy.ext.declarative import declarative_base
 from sqlalchemy.future import select as future_select
 from sqlalchemy.orm import deferred
 from sqlalchemy.orm import Session
-from sqlalchemy.sql import lambdas
 from . import Profiler
 
 
@@ -46,7 +44,7 @@ Profiler.init("short_selects", num=10000)
 @Profiler.setup
 def setup_database(dburl, echo, num):
     global engine
-    engine = create_engine(dburl, echo=echo)
+    engine = create_engine(dburl, echo=echo, query_cache_size=0)
     Base.metadata.drop_all(engine)
     Base.metadata.create_all(engine)
     sess = Session(engine)
@@ -69,47 +67,45 @@ def setup_database(dburl, echo, num):
 
 @Profiler.profile
 def test_orm_query_classic_style(n):
-    """classic ORM query of the full entity."""
+    """classic ORM query of the full entity, no cache"""
     session = Session(bind=engine)
     for id_ in random.sample(ids, n):
         session.query(Customer).filter(Customer.id == id_).one()
 
 
 @Profiler.profile
-def test_orm_query_new_style(n):
-    """new style ORM select() of the full entity."""
-
-    session = Session(bind=engine)
+def test_orm_query_classic_style_w_cache(n):
+    """classic ORM query of the full entity, using cache"""
+    cache = {}
+    session = Session(bind=engine.execution_options(compiled_cache=cache))
     for id_ in random.sample(ids, n):
-        stmt = future_select(Customer).where(Customer.id == id_)
-        session.execute(stmt).scalar_one()
+        session.query(Customer).filter(Customer.id == id_).one()
 
 
 @Profiler.profile
-def test_orm_query_new_style_using_embedded_lambdas(n):
-    """new style ORM select() of the full entity w/ embedded lambdas."""
+def test_orm_query_new_style(n):
+    """new style ORM select() of the full entity, no cache."""
+
     session = Session(bind=engine)
     for id_ in random.sample(ids, n):
-        stmt = future_select(lambda: Customer).where(
-            lambda: Customer.id == id_
-        )
+        stmt = future_select(Customer).where(Customer.id == id_)
         session.execute(stmt).scalar_one()
 
 
 @Profiler.profile
-def test_orm_query_new_style_using_external_lambdas(n):
-    """new style ORM select() of the full entity w/ external lambdas."""
+def test_orm_query_new_style_cache(n):
+    """new style ORM select() of the full entity, using cache."""
 
-    session = Session(bind=engine)
+    cache = {}
+    session = Session(bind=engine.execution_options(compiled_cache=cache))
     for id_ in random.sample(ids, n):
-        stmt = lambdas.lambda_stmt(lambda: future_select(Customer))
-        stmt += lambda s: s.where(Customer.id == id_)
+        stmt = future_select(Customer).where(Customer.id == id_)
         session.execute(stmt).scalar_one()
 
 
 @Profiler.profile
 def test_orm_query_classic_style_cols_only(n):
-    """classic ORM query against columns"""
+    """classic ORM query against columns, no cache"""
     session = Session(bind=engine)
     for id_ in random.sample(ids, n):
         session.query(Customer.id, Customer.name, Customer.description).filter(
@@ -118,45 +114,19 @@ def test_orm_query_classic_style_cols_only(n):
 
 
 @Profiler.profile
-def test_orm_query_new_style_ext_lambdas_cols_only(n):
-    """new style ORM query w/ external lambdas against columns."""
-    s = Session(bind=engine)
+def test_orm_query_classic_style_cols_only_cache(n):
+    """classic ORM query against columns, using cache"""
+    cache = {}
+    session = Session(bind=engine.execution_options(compiled_cache=cache))
     for id_ in random.sample(ids, n):
-        stmt = lambdas.lambda_stmt(
-            lambda: future_select(
-                Customer.id, Customer.name, Customer.description
-            )
-        ) + (lambda s: s.filter(Customer.id == id_))
-        s.execute(stmt).one()
-
-
-@Profiler.profile
-def test_baked_query(n):
-    """test a baked query of the full entity."""
-    bakery = baked.bakery()
-    s = Session(bind=engine)
-    for id_ in random.sample(ids, n):
-        q = bakery(lambda s: s.query(Customer))
-        q += lambda q: q.filter(Customer.id == bindparam("id"))
-        q(s).params(id=id_).one()
-
-
-@Profiler.profile
-def test_baked_query_cols_only(n):
-    """test a baked query of only the entity columns."""
-    bakery = baked.bakery()
-    s = Session(bind=engine)
-    for id_ in random.sample(ids, n):
-        q = bakery(
-            lambda s: s.query(Customer.id, Customer.name, Customer.description)
-        )
-        q += lambda q: q.filter(Customer.id == bindparam("id"))
-        q(s).params(id=id_).one()
+        session.query(Customer.id, Customer.name, Customer.description).filter(
+            Customer.id == id_
+        ).one()
 
 
 @Profiler.profile
 def test_core_new_stmt_each_time(n):
-    """test core, creating a new statement each time."""
+    """test core, creating a new statement each time, no cache"""
 
     with engine.connect() as conn:
         for id_ in random.sample(ids, n):
@@ -167,7 +137,7 @@ def test_core_new_stmt_each_time(n):
 
 @Profiler.profile
 def test_core_new_stmt_each_time_compiled_cache(n):
-    """test core, creating a new statement each time, but using the cache."""
+    """test core, creating a new statement each time, using cache"""
 
     compiled_cache = {}
     with engine.connect().execution_options(
@@ -181,7 +151,7 @@ def test_core_new_stmt_each_time_compiled_cache(n):
 
 @Profiler.profile
 def test_core_reuse_stmt(n):
-    """test core, reusing the same statement (but recompiling each time)."""
+    """test core, reusing the same statement, no cache"""
 
     stmt = select(Customer.__table__).where(Customer.id == bindparam("id"))
     with engine.connect() as conn:
@@ -192,7 +162,7 @@ def test_core_reuse_stmt(n):
 
 @Profiler.profile
 def test_core_reuse_stmt_compiled_cache(n):
-    """test core, reusing the same statement + compiled cache."""
+    """test core, reusing the same statement, using cache"""
 
     stmt = select(Customer.__table__).where(Customer.id == bindparam("id"))
     compiled_cache = {}