对于我的django应用程序,有没有办法记录执行时间较长的DB查询?
发布于 2014-09-10 21:53:54
这是一个中间件,它记录执行时间超过1秒的查询:
from django.db import connection
import logging
LONG_QUERY_TIME_SEC = 1
class LongQueryLogMiddleware:
def process_response ( self, request, response ):
for q in connection.queries:
if float(q['time']) >= LONG_QUERY_TIME_SEC:
logging.warning("Found long query (%s sec): %s", q['time'], q['sql'])
return response请注意,因为connection.queries是进程范围的,所以查询可以被记录两次。这可以通过在函数结尾处运行以下命令来解决:
from django.db import reset_queries
reset_queries()https://stackoverflow.com/questions/25767298
复制相似问题