> On Oct 27, 2014, at 1:17 PM, Peter Waller <pe...@scraperwiki.com> wrote:
> 
> I observe that the query for a lazy load is generated here:
> 
> https://bitbucket.org/zzzeek/sqlalchemy/src/e1d1d999c9a688f4c8dbbe885438c63d6ef494c6/lib/sqlalchemy/orm/strategies.py?at=master#cl-551
>  
> <https://bitbucket.org/zzzeek/sqlalchemy/src/e1d1d999c9a688f4c8dbbe885438c63d6ef494c6/lib/sqlalchemy/orm/strategies.py?at=master#cl-551>
> 
> I would like to run queries through an EXPLAIN QUERY PLAN or an ANALYZE 
> programmatically for many properties. Is there an easy way to achieve that?

Here’s a recipe to convert those queries to EXPLAIN: 
https://bitbucket.org/zzzeek/sqlalchemy/wiki/UsageRecipes/Explain 
<https://bitbucket.org/zzzeek/sqlalchemy/wiki/UsageRecipes/Explain>  and then 
to observe them as they occur, one quick and easy way is to grab them with a 
cursor event 
http://docs.sqlalchemy.org/en/rel_0_9/core/events.html?highlight=before_cursor_execute#sqlalchemy.events.ConnectionEvents.before_cursor_execute.

> It looks not since it doesn't like the query is exposed. The only way I'm 
> aware of to get it is to cause a lazy load and have logging enabled (or add 
> an event handler which observes the executed statement), but I'd like to know 
> if there is a way to avoid doing that.

OK you don’t like the event.  Well the event is a way to inject functionality 
into the existing system, you can apply this to the specific connection to 
which the session is bound, then run the lazy load (and then remove the event 
afterwards).    If you’re looking for something fully imperative like 
lazy_load_and_grab_explain(myobject, “some_attribute”) that doesn’t actually 
invoke the load, it’s only the “don’t actually do the load” part that would be 
tricky, as that indicates a specific feature in the ORM that’s essentially, 
give_me_the_lazyload_query(myobject, “some_attribute”), which is not built in 
at this time.   It’s not a given that this load is even a single query, as 
“some_attribute” could then refer to a graph that contains additional loading 
instructions.   So an API like this isn’t exactly straightforward, whereas 
grabbing the queries with the event and just sending them into a collection, 
fully straightforward.

-- 
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 sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

Reply via email to