Skip to content

Timing of last query

Luckydonald edited this page Dec 22, 2016 · 3 revisions
# Execute a query
db.User.get(id=123)

# Get statistics
stats = db.local_stats[db.last_sql]

# Print it
logger.debug(
    "QUERY STATS:\nTook {avg_time} second(s), Executed {db_count} time(s).".format(
       avg_time=stats.avg_time, db_count=stats.db_count
    )
)

db.last_sql returns last SQL query.
db.local_stats returns a dictionary of QueryStat Objects, the keys are the SQL queries.

QUERY STATS:
Took 1.4071071147918701 second(s), Executed 2 time(s).

As one-liner this would be equivalent:

logger.debug("QUERY STATS:\nTook {s.avg_time}, Executed {s.db_count} time(s).".format(s=db.local_stats[db.last_sql]))
Clone this wiki locally