【发布时间】:2013-06-07 02:41:00
【问题描述】:
查看 postgres 服务器日志,我发现从 Linux 客户端或 Windows 客户端调用时,在同一个 postgres 服务器上执行完全相同的查询需要更长的时间(大约长 10 倍)。
查询来自运行在具有 4GB RAM 的 Linux 机器和具有 8GB RAM 的 Windows 机器上的 Django 应用程序。两个 pyhon 环境都有 psycopg2 库版本 2.4.4 来向同一个 postgres 服务器发送请求。
以下是 postgres 服务器日志
windows查询(带时间):
2013-06-11 12:12:19 EEST [unknown] 10.1.3.152(56895) mferreiraLOG: duration: 3207.195 ms statement: SELECT "autotests_tracerperformance"."id", "autotests_tracerperformance"."date", "autotests_tracerperformance"."video_id", "autotests_tracerperformance"."revision_id", "autotests_tracerperformance"."computer_id", "autotests_tracerperformance"."probe", "autotests_tracerperformance"."time_tostart", "autotests_tracerperformance"."hang_atstart", "autotests_tracerperformance"."time_tohang", "autotests_tracerperformance"."hang", "autotests_tracerperformance"."crash", "autotests_tracerperformance"."stacktrace", "autotests_tracerperformance"."framemax", "autotests_tracerperformance"."maxtime", "autotests_tracerperformance"."avgtime" FROM "autotests_tracerperformance" INNER JOIN "revisions" ON ("autotests_tracerperformance"."revision_id" = "revisions"."id") WHERE ("autotests_tracerperformance"."computer_id" = 61 AND "revisions"."repo" = 'Trunk' )
linux 查询(更长):
2013-06-11 12:12:56 EEST [unknown] 10.1.3.154(35325) mferreiraLOG: duration: 22191.773 ms statement: SELECT "autotests_tracerperformance"."id", "autotests_tracerperformance"."date", "autotests_tracerperformance"."video_id", "autotests_tracerperformance"."revision_id", "autotests_tracerperformance"."computer_id", "autotests_tracerperformance"."probe", "autotests_tracerperformance"."time_tostart", "autotests_tracerperformance"."hang_atstart", "autotests_tracerperformance"."time_tohang", "autotests_tracerperformance"."hang", "autotests_tracerperformance"."crash", "autotests_tracerperformance"."stacktrace", "autotests_tracerperformance"."framemax", "autotests_tracerperformance"."maxtime", "autotests_tracerperformance"."avgtime" FROM "autotests_tracerperformance" INNER JOIN "revisions" ON ("autotests_tracerperformance"."revision_id" = "revisions"."id") WHERE ("autotests_tracerperformance"."computer_id" = 61 AND "revisions"."repo" = 'Trunk' )
直接从 psql 执行(最快):
2013-06-11 12:19:06 EEST psql [local] mferreiraLOG: duration: 1332.902 ms statement: SELECT "autotests_tracerperformance"."id", "autotests_tracerperformance"."date", "autotests_tracerperformance"."video_id", "autotests_tracerperformance"."revision_id", "autotests_tracerperformance"."computer_id", "autotests_tracerperformance"."probe", "autotests_tracerperformance"."time_tostart", "autotests_tracerperformance"."hang_atstart", "autotests_tracerperformance"."time_tohang", "autotests_tracerperformance"."hang", "autotests_tracerperformance"."crash", "autotests_tracerperformance"."stacktrace", "autotests_tracerperformance"."framemax", "autotests_tracerperformance"."maxtime", "autotests_tracerperformance"."avgtime" FROM "autotests_tracerperformance" INNER JOIN "revisions" ON ("autotests_tracerperformance"."revision_id" = "revisions"."id") WHERE ("autotests_tracerperformance"."computer_id" = 61 AND "revisions"."repo" = 'Trunk' );
不需要从数据库加载这么多项目的其他查询的执行几乎相同。
为什么这个查询的客户端之间的时间差异如此之大?
注意:传输时间不相关,因为所有机器都在同一个 Intranet 中。此外,当客户端请求来自运行 postgresql 服务器的同一台 Linux 机器时,会出现较慢的时间。
注意2:Psycopg2 在 Windows 和 Linux 中的安装方式不同。在 Windows 中,我从预打包的二进制文件中安装它,而在 Linux 中,我运行“pip install psycopg2”,它依赖于系统上可用的 postgresql 安装。这是否会导致影响客户端性能的参数值不同(例如“work_mem”参数)?
【问题讨论】:
-
只是在黑暗中拍摄:可能是 PostgreSQL 内部缓存问题?您是否尝试从 Linux 多次提交 SELECT 语句,也多次从 Windows 提交?我想那时平均时间应该是一样的。
-
to mawimawi:不,这些时间是一致的,我开始调试这个,因为我的生产 django 应用程序比开发(Windows)机器慢得多。如果你跑多次,时间是一样的。
-
可能与网络延迟有关。特别是如果您将大量数据从服务器传输到下一个服务器。在服务器级别记录查询,以查看在 Postgres 中实际花费了多少时间。哦,这也可能是python中的执行时间差异,例如创建对象等。
-
致丹尼斯:不是真的,我测试了在 postgresql 服务器所在的同一台机器上运行 django 应用程序 - 它比在具有 Windows 客户端的另一台机器上运行的 django 应用程序要多 10 倍。它似乎与客户端配置有关。所有服务器都是同一个本地 Intranet 的一部分,这并不重要 - 我认为 postgresql 日志显示在 postgres 服务器中运行的时间并且没有考虑数据传输(?)
-
我同意 Denis 的观点,它看起来像网络问题 - 或 Linux 站网卡问题。
标签: python django postgresql psycopg2