Postgresql: 在不同客户端查询10倍慢

2024-05-23 21:06:15 发布

您现在位置:Python中文网/ 问答频道 /正文

查看postgres服务器日志,我发现从Linux客户机或Windows客户机调用同一个postgres服务器上的完全相同的查询需要更长的时间(大约10倍的时间)。在

这些查询来自一个Django应用程序,该应用程序运行在具有4GB RAM的Linux机器和8GB RAM的Windows机器上。这两个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查询(更长):

^{pr2}$

直接从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' );

其他不需要从数据库加载这么多项的查询的执行情况几乎相同。在

为什么这个查询的客户机之间存在如此大的时间差?在

注意:传输时间不相关,因为所有机器都在同一内部网中。另外,当客户机请求来自运行postgresql服务器的同一台Linux机器时,速度会慢一些。在

注意2:Psycopg2在Windows和Linux中的安装方式不同。而在Windows中,我是从预打包的二进制文件安装它的,而在Linux中,我运行的是pip install psycopg2,它依赖于系统上可用的postgresql安装。这是否会导致影响客户端性能的参数值不同(例如“work-mem”参数)?在


Tags: 服务器机器id客户机timelinuxwindows时间
1条回答
网友
1楼 · 发布于 2024-05-23 21:06:15

您可能需要检查慢速客户端是否执行SSL加密。默认情况下,当在服务器上设置它并且客户端已使用SSL支持进行编译时,会发生这种情况。在

对于检索大量数据的查询,时间差非常大。 另外,一些Linux发行版,比如Debian/Ubuntu,默认情况下都启用SSL,甚至对于通过localhost的TCP连接也是如此。在

作为一个示例,下面是一个查询的时差,该查询使用热缓存检索总计64MB的1.5M行。在

不加密:

$ psql "host=localhost dbname=mlists sslmode=disable"
Password: 
psql (9.1.7, server 9.1.9)
Type "help" for help.

mlists=> \timing
Timing is on.
mlists=> \o /dev/null
mlists=> select subject from mail;
Time: 1672.258 ms

使用加密:

^{pr2}$

要全局关闭它,可以在postgresql.conf中设置SSL=off。在

要对特定范围的客户机地址关闭它,请在第一个字段中添加pg_hba.conf中的条目,并在更通用的host之前的第一个字段中添加{}。在

要关闭if客户端,这取决于驱动程序如何公开sslmode连接参数。如果没有,如果驱动程序是在libpq之上实现的,则可以使用PGSSLMODE环境变量。在

对于通过Unix域套接字(local)的连接,SSL永远不会与它们一起使用。在

相关问题 更多 >