first get your query to work properly without any eager loading, meaning, you get the correct results. then, if your query happens to JOIN to the table such that the returned rows can be used to populate the collection or many-to-one reference, use the contains_eager() option as described in the doc. This option allows you to hand construct exactly the query you want without SQLA adding anything to it, but you can still take advantage of eager loading.
If the JOIN doesnt really represent the correct reference in all cases, then you'd use automatic eager loading, which will make its own OUTER JOIN that retrieves the correct data (thats what the anon_1 thing is). I would advise only enabling this on a per-query basis, using query.options(eagerload(MyClass.someproperty)). If you dont want any joins at all, then the collections/references load themselves through lazy loading. The subtlety here is that filtering results on multiple tables does not necessarily represent the same kind of join that would properly populate object references, which is why SQLA keeps these separate in the default case. The query you're doing below is using an EXISTS on a subquery so I dont see any opportunities for using contains_eager() as it is. On Oct 16, 2008, at 12:45 PM, g00fy wrote: > > So in simple words, how do I speed this up? > > On 16 Paź, 18:42, Michael Bayer <[EMAIL PROTECTED]> wrote: >> On Oct 16, 2008, at 12:31 PM, g00fy wrote: >> >> >> >> >> >>> I have loads of related fields in my model. >>> I set up lazy = False where i had to and i don't know why SA keeps >>> "duplicating" (aliasing) my tables, so i fetch my data twice (2 >>> times >>> more column that i realy need) >> >>> i send my model and mappings and the sql code SA is querying. >> >>> models: >>> http://dpaste.com/84873/ >>> tables: >>> http://dpaste.com/84874/ >>> mappings: >>> http://dpaste.com/84875/ >>> sql: >>> http://dpaste.com/84876/ >> >>> notice in sql that anon_1 is realy the same as w_warehouse. >>> this way i am stuck with 1000 objects having 2 languages and 2 >>> currencies. >> >>> the code I do is : >>> meta >>> .Session >>> .query(model.Warehouse).order_by(model.Warehouse.area_total.asc()) >>> [0:10] >>> .filter( >>> model.Warehouse.price.has( >>> model.Price.total<=price_total_max >>> ) >>> ) >> >>> how can I get rid of that nasty anon_1, and speed up this thing? >> >> look >> intohttp://www.sqlalchemy.org/trac/wiki/FAQ#ImusinglazyFalsetocreateaJOIN >> ... >> . an eager load of "Price" is not related to the filter >> criterion >> using "Price" - you need to join explicitly. >> >> To "dual purpose" your explicit join as an eager load as well, look >> into : >> http://www.sqlalchemy.org/docs/05/mappers.html#advdatamapping_relatio >> ... >> >> >> >> > > --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "sqlalchemy" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en -~----------~----~----~----~------~----~------~--~---
