I often find myself wanting to do something like this: for address in user.addresses: if address.street == "123 Elm Street": # there's an address from this street, so do something break else: # do nothing; no addresses from that street
That could also be accomplished with SQL: address = Session.query(Address).filter(Address.user == user).filter(Address.street == "123 Elm Street") Or, in certain cases, I need a join: try: user = Session.query(User).join(Address).filter(Address.street == "123 Elm Street").one() # There was someone at that address, so do something except: # None Found; don't do anything pass So there are usually two ways to approach these problems: ORM-mappings or SQL queries. If I use the ORM-mapping approach, SQLAlchemy generally only needs to eagerload the collection once -- so if I have multiple functions that operate on mapped data, I only incur the database load once. If I do it with SQL querys, it queries the database multiple times (once for each query). What's the best approach for this type of thing? Is it best practice (performance-wise) to prefer SQL queries over looping through ORM- mapped properties, even if SQLAlchemy needs to issue a few more SQL queries instead? Obviously I'd need to do performance testing for specific cases. I've preferred the looping-on-ORM-relations approach because it's clearer in my mind, but if it'd be much more performant to have SQLAlchemy query when I need the data via SQL querying, I'll reevaluate. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---