1

I find a lot of performance issues with SqlAlchemy creep into my project when database lookup happens at e.g. the template generation stage.

Is it possible to tell SqlAlchemy that 'data gathering' has finished, and that any further queries to a session (e.g. as a side effect of accessing an instrumented attribute) are to raise an exception?

What other strategies have people used to ensure that an ORM performs well?

4

1 回答 1

1

无论您是在模板生成期间还是在此之前进行数据库查找,性能都没有差异 - 关键是要避免用数百个查询锤击数据库,无论这些查询源自何处。

您需要跟踪在每个请求/响应周期中发出的查询数量 - 如果您看到许多查询,您知道您需要优化访问数据的方式。

许多 Web 框架都包含“调试工具栏”功能,例如 Pyramid 和 Flask 就有一个。如果您使用其中一个框架,只需启用它即可。

否则,自己动手并不难:

class QueryStats(object):
    def __init__(self):
        self.begin()

    def add_query(self, statement, parameters):
        self.queries += [(statement, parameters)]

    def begin(self):
        self.queries = []


def before_cursor_execute(conn, cursor, statement, parameters, context
    DBSession.stats.add_query(str(statement), str(parameters))


class SessionStatsBase(SessionBase):
    def __init__(self, *args, **kw):
        SessionBase.__init__(self, *args, **kw)
        self.stats = QueryStats()

DBSession = scoped_session(sessionmaker(class_=SessionStatsBase, ...)
event.listen(engine, "before_cursor_execute", before_cursor_execute)

然后,您只需在页面底部打印一长串查询,或者,如果您愿意,如果请求发出的查询超过 100 个,则引发异常。

于 2013-03-06T01:06:16.873 回答