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

pyodbc连接SQL Server耗时超6秒,如何优化提速?

问题:Python使用pyodbc连接本地SQL Server初始连接耗时超6秒

我用Python脚本从本地Windows电脑连接SQL Server获取数据,发现pyodbc.connect()调用耗时超过6秒。以下是测试脚本及输出:

测试脚本

#!/usr/bin/env python

import pyodbc
import time

def main():
    print("start")
    start = time.time()

    db_conn = pyodbc.connect(
        f"Driver=SQL Server;Server=NA-LT-1196;Database=A341633;Trusted_Connection=yes;")
    print(f"{time.time()-start: 6.4f} db_conn created")

    cur = db_conn.cursor()
    print(f"{time.time()-start: 6.4f} cur created")

    cur.execute("SELECT Version FROM Version")
    print(f"{time.time()-start: 6.4f} after cur.execute")

    for row in cur:
        print(row)
    print(f"{time.time()-start: 6.4f} rows printed")

    cur.close()
    print(f"{time.time()-start: 6.4f} cur closed")

if __name__ == '__main__':
    main()

脚本输出

C:\TestProject> python .\sql_demo.py
start
 6.1292 db_conn created
 6.1292 cur created
 6.1302 after cur.execute
('25.250.0011.0115',)
 6.1312 rows printed
 6.1312 cur closed

从输出可见,连接创建阶段耗时超6秒,请问有什么提速方法?


问题已解决

将连接字符串中的驱动从SQL Server更换为ODBC Driver 17 for SQL Server后,连接耗时问题得到解决。修改后的连接代码如下:

db_conn = pyodbc.connect(
    f"Driver=ODBC Driver 17 for SQL Server;Server=NA-LT-1196;Database=A341633;Trusted_Connection=yes;")

感谢所有提供建议的人!


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.13 10:25:04