answering myself, well... for the record.

> i'm back into my hierachy/graph of objects, which has many paths
> reaching from one end to another - A-Z, A-B-Z, A-B-C-Z etc. Using
> hierarchical OR (AND (OR ...))) works but gets very slow as all the
> 20+ tables involved go in same FROM clause.

> so i have these questions:
>  - (theoretical) can the alternative made by many ORs over
> subexpressions at same level be represented with UNION of things
> made from those subexpressions?
yes. the difference is in magnitudes, and moreover, union' space grows 
slowly/linearly while the other grows exponentialy. it can be done at 
more levels if needed; each OR-ing is a candidate for it.
Further tweaking can be made, including not using a union but doing 
several queries for the 1st-level alternatives (but losing overall 
order_by etc).
>  - can i use query().join() to form the expression and stick it
> into the union somehow? should i take query.criterion or something 
else?
yes
q = query(A).whatever-joins-filters()
s = q._compile_context().statement 
use s for the union's elements. do not forget to add to each q all 
relevant joins and filters that have been taken outside the 
(or/and/or/..) parenthesis in the initial big query.
then, myunion = union( *allthese)

> - can then the union be put into the query of say root 
> object?
yes. use query(A).select_from( myunion)
other ways:
 - query(A).from_statement( myunion) works but does not allow further 
joins/filters. Although the union is just different As, it will not 
be treated as such.
 - one can make a secondary mapper if the query structure is static:
   m = sa.mapper( A, table=myunion, non_primary=True )
   r = session.query( m).whatever ...

btw this is going towards query arithmetics that i've been talking 
since long time. i hope one day things like this will be in SA as a 
sort of 3rd layer over sql and plain orm. 

ciao
svilen
www.svilendobrev.com / dbcook.sf.net

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to sqlalchemy@googlegroups.com
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to