Add cache for precompiled SA queries
How do you want to organise it without name collision? . I use model dict (name -> select) for custom selectors: I use bindparam() for arguments and store these dicts locally..
I think LRU cache for expressions may help. ClauseElement has method .compare(other) BTW.
So query like conn.execute(tbl.insert(), {'a': 1, 'b': 2}) will be cached fine, in the same cell as conn.execute(tbl.insert(), {'a': 3, 'b': 4}).
But conn.execute(tbl.insert().values(a=1, b=2)) is bad candidate to cache, LRU will kick it off quickly.
But I still have no evidence that SQLAlchemy query compiling is a bottleneck.
I've done benchmark to allow execute precompiled queries. I've got:
aiopg (sql): 182446 queries in 30 seconds aiopg_sa: 88508 queries in 30 seconds aiopg_sa_cache: 118051 queries in 30 seconds
So if this issue will be done, it can give about 25% to speedup.
Unlimited cache size bothers me. Maybe better to use lru_cache?
@functools.lru_cache(256)
def get_compiled(query):
return query.compile()