Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to turn off MySQL query cache while using SQLAlchemy?

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?

like image 431
Kevin Collins Avatar asked Jan 17 '13 05:01

Kevin Collins


People also ask

Does SQLAlchemy cache queries?

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.

Can I use SQLAlchemy with MySQL?

SQLAlchemy supports MySQL starting with version 5.0. 2 through modern releases, as well as all modern versions of MariaDB.

Should I use SQLAlchemy core or ORM?

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.

Is SQLAlchemy slow?

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.


1 Answers

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 :)

like image 105
Eevee Avatar answered Oct 21 '22 03:10

Eevee