RFE: add per-query option for logging, with EXPLAIN

Issue #1114 resolved
Former user created an issue

I'm often finding myself trying to optimize a particular query, but finding it getting lost in noise if I enable logging globally.

I'd like an extension to sqlalchemy to support enabling logging on a per-query basis, and potentially show query plans.

The API might look something like this: session.query(Foo).filter_by(various_things).options(log=True, explain=True) i.e. provide a way to easily enable logging on just one query using options. There may be a better way of doing this; I haven't dived deep into the code yet.

With explain enabled, the query could be run twice: first with EXPLAIN prepended, logging the query plan from the db, then the regular query run. Unfortunately, there's wild inconsistency in what the output of EXPLAIN looks like from db to db.

Comments (3)

  1. Mike Bayer repo owner

    this sounds like a wholly separate application to me. EXPLAIN output is most nicely represented in some kind of graphical format.

    Theres also no SQLAlchemy integration required. The tool would take any kind of SA construct (i.e. Query, select()) and just run its generated SQL, which is available by calling str() on either of those constructs.

    So not entirely convinced this is even a SQLAlchemy ticket.

  2. Mike Bayer repo owner

    Any feature used for EXPLAIN I would like to live as a SQLAlchemy extension, a usage recipe, or a third party library - I don't think EXPLAIN constructs belong in the core since they are way varied on different DBs and are only used as diagnostic tools.

    I'd welcome more detail on a possible API for this.

  3. Log in to comment