You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PostgreSQL:除PgAdmin/DBeaver外查询均缓慢(psycopg2异常)

问题:psycopg2查询PostgreSQL速度极慢,远超DBeaver

使用Python的psycopg2查询PostgreSQL数据库时,所有查询速度都异常缓慢。同一查询在DBeaver中仅耗时47ms,但在Python中执行却超过3分钟。之前尝试用Oracle客户端替代DBeaver时,也遇到过类似的慢查询问题,因此一直用DBeaver操作,但项目要求必须通过脚本执行查询。

示例表bex结构及数据

IDNamecodecode_acr
1ParisPARPAR
2DijonDIJDIJ
3BrestBRSBRT
4ToulonTLNTLN

所用Python代码

import psycopg2

try:
    conn = psycopg2.connect(
            host="xxxxx.sogate-pacy.xxxxxx.fr",
            dbname="xxxxxx",
            user="xxxxxxx",
            password="<xxxxx>",
            port="5432",
            options="-c search_path=xxx",
            sslmode = "disable"
    )

    cursor = conn.cursor()
    postgreSQL_select_Query = "SELECT * FROM bex"
    
    cursor.execute(postgreSQL_select_Query)
    ouvrage = cursor.fetchone()
    
    print("Print each row and it's columns values")
    print(cursor.fetchone())

except (Exception, psycopg2.Error) as error:
    print("Error while fetching data from PostgreSQL", error)

finally:
    # closing database connection.
    if conn:
        cursor.close()
        conn.close()
        print("PostgreSQL connection is closed")

需要说明的是,该bex表总计仅10行数据,即便仅查询单行也会出现这种慢查询情况。

内容的提问来源于stack exchange,提问作者Umair10

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.01 16:37:13