part of Pylons controller: """ def index(self): query = Session(FGM).filter(FGM.c.state == 'normal') ... query = query.filter(FGM.c.anotherstate.in_('read', 'write')) ... ids = getIds() ... query = query.filter(FGM.c.type.in_(ids)) """
query executed against eight shards, and the one query against master base some profiling: 580971 function calls (579337 primitive calls) in 3.088 CPU seconds Ordered by: internal time List reduced from 1045 to 30 due to restriction <30> ncalls tottime percall cumtime percall filename:lineno(function) 14 0.399 0.028 0.399 0.028 {method 'recv' of '_socket.socket' objects} 12040 0.218 0.000 1.246 0.000 /var/lib/python-support/ python2.5/sqlalchemy/orm/mapper.py:1280(_instance) 11 0.181 0.016 0.181 0.016 {method 'query' of '_mysql.connection' objects} 55745 0.179 0.000 0.303 0.000 /var/lib/python-support/ python2.5/sqlalchemy/engine/base.py:1340(__getitem__) 6543 0.136 0.000 0.447 0.000 /var/lib/python-support/ python2.5/sqlalchemy/orm/mapper.py:1412(populate_instance) 12040 0.130 0.000 0.369 0.000 /var/lib/python-support/ python2.5/sqlalchemy/orm/mapper.py:851(identity_key_from_row) 55745 0.123 0.000 0.124 0.000 /var/lib/python-support/ python2.5/sqlalchemy/engine/base.py:1590(_get_col) 12051 0.098 0.000 1.707 0.000 /var/lib/python-support/ python2.5/sqlalchemy/orm/query.py:850(iterate_instances) 19667 0.097 0.000 0.301 0.000 /var/lib/python-support/ python2.5/sqlalchemy/orm/query.py:1465(exec_with_path) 19633 0.090 0.000 0.203 0.000 /var/lib/python-support/ python2.5/sqlalchemy/orm/strategies.py:76(new_execute) 12040 0.078 0.000 0.188 0.000 /var/lib/python-support/ python2.5/sqlalchemy/util.py:904(append) 6543 0.061 0.000 0.061 0.000 /var/lib/python-support/ python2.5/sqlalchemy/orm/attributes.py:675(__init__) 18048 0.059 0.000 0.090 0.000 /var/lib/python-support/ python2.5/sqlalchemy/orm/attributes.py:41(__get__) 12040 0.058 0.000 1.304 0.000 /var/lib/python-support/ python2.5/sqlalchemy/orm/query.py:868(main) 12040 0.056 0.000 0.056 0.000 /var/lib/python-support/ python2.5/sqlalchemy/orm/attributes.py:898(__contains__) 1 0.050 0.050 3.044 3.044 /home/stromnov/......./ controllers/controller.py:113(index) And I just want to optimize this to something like this: _query = Query(FGM).filter(.......) def index(self): ids = getIds() ... query = _query.filter(FGM.c.type.in_(ids)) execute_query_using_session(query=query, session=Session) On 7 мар, 18:49, Michael Bayer <[EMAIL PROTECTED]> wrote: > On Mar 7, 2008, at 6:11 AM, Andrew Stromnov wrote: > > > > > I have only two complicated queries with in_() statements in my app > > (under Pylons). How to pregenerate common parts of this queries and > > assign Session (in my case ShardedSession) to them at runtime? > > this is a vague question. it depends on what effort you are looking > to minimize. If it were my app, and I was sick of typing a big long > query each time, I'd just build a helper function q = > generate_long_query(session) that does it for me. Alternatively you > can build up a Query object and reuse it, although a Query is attached > to a single session; we'd have to give you a method > q.with_session(somesession) to reassign a pre-built query with a > different session...but that doesn't seem very different from just > building your own function. > > If you're building a full select() statement, just build it somewhere, > then just use it as needed....query.from_statement(myselect). > > If I read your question completely literally, it seems like you'd like > the session to use a certain query "automatically", I don't exactly > understand that since the Session doesn't generate SQL queries. > > if its the "performance" of compiling a query you're concerned about, > I think query compilation performance is fairly negligible - I think > you should run some profiling to determine if that's actually a > bottleneck. even in that case i think query.from_statement() can > accept a Compiled object (or if not we can make that work). --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---