I am working with a fairly large MySQL database via the SQLAlchemy library, and I'd love to turn off MySQL's query caching to debug performance issues on a per-session basis. It's difficult to debug slow queries when repeating them results in much faster execution. With the CLI MySQL client, I can execute SET SESSION query_cache_type = OFF;
to achieve the result I'm looking for, and I would like to run this on every SQLAlchemy session (when I'm debugging).
But I can't figure out how to configure SQLAlchemy such that it runs SET SESSION query_cache_type = OFF
when it instantiates a new database session.
I've looked at the Engine and Connection docs, but can't seem to find anything.
Is there something obvious that I'm missing, or a better way of doing this?
Deprecated since version 1.4: SQLAlchemy 1.4 and 2.0 feature an all-new direct query caching system that removes the need for the BakedQuery system. Caching is now transparently active for all Core and ORM queries with no action taken by the user, using the system described at SQL Compilation Caching.
SQLAlchemy supports MySQL starting with version 5.0. 2 through modern releases, as well as all modern versions of MariaDB.
If you want to view your data in a more schema-centric view (as used in SQL), use Core. If you have data for which business objects are not needed, use Core. If you view your data as business objects, use ORM. If you are building a quick prototype, use ORM.
SQLAlchemy is very, very fast. It's just that users tend to be unaware of just how much functionality is being delivered, and confuse an ORM result set with that of a raw database cursor. They are quite different, and SQLAlchemy offers many options for controlling the mixture of "raw" vs.
Use an event hook immediately after you define your engine:
from sqlalchemy import event
def disable_query_cache(conn, record):
conn.cursor().execute("SET SESSION query_cache_type = OFF")
# this is probably in your Pyramid setup code
engine = create_engine(...)
if DEBUGGING:
event.listen(engine, 'connect', disable_query_cache)
You can do this globally by adding the hook to the Pool
class itself, but (a) you probably want the Pyramid settings available anyway so you can decide whether or not to add the hook, and (b) global state is bad :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With