def test_render_explain_analyze(self, session, Article):
     assert str(
     ).startswith('EXPLAIN (ANALYZE true) SELECT')
Beispiel #2
def analyze(conn, query):
    Analyze query using given connection and return :class:`QueryAnalysis`
    object. Analysis is performed using database specific EXPLAIN ANALYZE
    construct and then examining the results into structured format. Currently
    only PostgreSQL is supported.

    Getting query runtime (in database level) ::

        from sqlalchemy_utils import analyze

        analysis = analyze(conn, 'SELECT * FROM article')
        analysis.runtime  # runtime as milliseconds

    Analyze can be very useful when testing that query doesn't issue a
    sequential scan (scanning all rows in table). You can for example write
    simple performance tests this way.::

        query = (
        analysis = analyze(self.connection, query)
        analysis.node_types  # [u'Limit', u'Index Only Scan']

        assert 'Seq Scan' not in analysis.node_types

    .. versionadded: 0.26.17

    :param conn: SQLAlchemy Connection object
    :param query: SQLAlchemy Query object or query as a string
    return QueryAnalysis(
            explain_analyze(query, buffers=True, format='json')