如何获取查询执行时间的psycopg2日志记录?

时间:2015-09-18 21:43:35

标签: python postgresql

我正在尝试获取psycopg2执行的查询的性能统计信息,但文档/示例仍然显得模糊,并且不尽如人意。

我至少通过记录器进行调试。 我需要做什么才能访问查询的性能数据?我想获得查询执行时间的数字。

是否有我可以访问的方法,或者我需要初始化以输出查询执行时间的其他内容?

这是我到目前为止的拼凑摘录:

import psycopg2
import psycopg2.extensions
from psycopg2.extras import LoggingConnection
import logging

logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger(__name__)

# set higher up in script
db_settings = {
    "user": user,
    "password": password,
    "host": host,
    "database": dbname,
}

query_txt = "[query_txt_from file]"

conn = psycopg2.connect(connection_factory=LoggingConnection, **db_settings)
conn.initialize(logger)

cur = conn.cursor()
cur.execute(query_txt)

我得到了

DEBUG:__main__: [the query executed]

1 个答案:

答案 0 :(得分:1)

非常容易在执行开始时设置时间戳并在结束时计算持续时间。您将需要自己的LoggingConnection和LoggingCursor子类。查看我的示例代码。

这基于您可以在psycopg2/extras.py来源中找到的MinTimeLoggingConnection来源。

import time
import psycopg2
import psycopg2.extensions
from psycopg2.extras import LoggingConnection, LoggingCursor
import logging

logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger(__name__)

# MyLoggingCursor simply sets self.timestamp at start of each query                                                                 
class MyLoggingCursor(LoggingCursor):
    def execute(self, query, vars=None):
        self.timestamp = time.time()
        return super(MyLoggingCursor, self).execute(query, vars)

    def callproc(self, procname, vars=None):
        self.timestamp = time.time()
        return super(MyLoggingCursor, self).callproc(procname, vars)

# MyLogging Connection:                                                                                                             
#   a) calls MyLoggingCursor rather than the default                                                                                
#   b) adds resulting execution (+ transport) time via filter()                                                                     
class MyLoggingConnection(LoggingConnection):
    def filter(self, msg, curs):
        return msg + "   %d ms" % int((time.time() - curs.timestamp) * 1000)

    def cursor(self, *args, **kwargs):
        kwargs.setdefault('cursor_factory', MyLoggingCursor)
        return LoggingConnection.cursor(self, *args, **kwargs)

db_settings = {
    ....
}

query_txt = "[query_text_from file]"

conn = psycopg2.connect(connection_factory=MyLoggingConnection, **db_settings)
conn.initialize(logger)

cur = conn.cursor()
cur.execute(query_text)

您将获得:

DEBUG: __main__:[query]     3 ms

filter()中,您可以更改格式,或选择不显示(如果小于某个值)。