There's a recipe at https://github.com/sqlalchemy/sqlalchemy/wiki/Explain
that should give you all you need.

On Sat, Jan 19, 2019, 2:55 PM Zsolt Ero <[email protected] wrote:

> Hi, I'm using the following snippet to run explain analyize on raw sql
> (PostgreSQL, psycopg2).
>
> def print_explain(engine, raw_sql, values):
>     sql_text = text('EXPLAIN (ANALYZE, BUFFERS) ' + raw_sql)
>     q = engine.execute(sql_text, values)
>     lines = q.fetchall()
>     print('--- EXPLAIN ANALYZE ---')
>     for l in lines:
>         print(l[0])
>     print('--- EXPLAIN END     ---')
>
>
> How can I make it work with Core statements? So far my best idea seems to
> be:
>
> def print_explain_stmt(stmt):
>     raw_sql = str(
>         stmt.compile(dialect=postgresql.dialect(), compile_kwargs={
> "literal_binds": True})
>     )
>
>     sql_text = text('EXPLAIN (ANALYZE, BUFFERS) ' + raw_sql)
>     q = pg_engine.execute(sql_text)
>     lines = q.fetchall()
>     print('--- EXPLAIN ANALYZE ---')
>     for l in lines:
>         print(l[0])
>     print('--- EXPLAIN END     ---')
>
> But is this OK? I mean compiling the statement isn't really reliable for
> some column types. Can you make this function to use the stmt directly?
>
> --
> SQLAlchemy -
> The Python SQL Toolkit and Object Relational Mapper
>
> http://www.sqlalchemy.org/
>
> To post example code, please provide an MCVE: Minimal, Complete, and
> Verifiable Example. See http://stackoverflow.com/help/mcve for a full
> description.
> ---
> You received this message because you are subscribed to the Google Groups
> "sqlalchemy" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To post to this group, send email to [email protected].
> Visit this group at https://groups.google.com/group/sqlalchemy.
> For more options, visit https://groups.google.com/d/optout.
>

-- 
SQLAlchemy - 
The Python SQL Toolkit and Object Relational Mapper

http://www.sqlalchemy.org/

To post example code, please provide an MCVE: Minimal, Complete, and Verifiable 
Example.  See  http://stackoverflow.com/help/mcve for a full description.
--- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

Reply via email to