简体   繁体   English

PDO与Azure PostgreSQL建立连接非常慢

[英]PDO establishing connection to Azure PostgreSQL is very slow

I have PHP application running as a Docker container based on Alpine Linux inside Kubernetes. 我在Kubernetes内将PHP应用程序作为基于Alpine Linux的Docker容器运行。 Everything went well until I tried removing container with test database and replacing it with Azure PostgreSQL . 一切顺利,直到我尝试使用测试数据库删除容器并将其替换为Azure PostgreSQL This led to significant latency increase from under 250ms to above 1500ms. 这导致等待时间从250ms以下显着增加到1500ms以上。

According to profiler most time is spent in PDO constructor which is establishing connection to database. 根据探查器,大多数时间都花在PDO构造函数上 ,该构造函数正在建立与数据库的连接。 The SQL queries themselves, after connection was established, then run in about 20ms. 建立连接后,SQL会自行查询,然后在大约20毫秒内运行。

  • I tried using IP instead of address and it was still slow. 我尝试使用IP代替地址,但速度仍然很慢。
  • I tried connecting from container using psql and it was slow (see full command below) 我尝试使用psql从容器连接,速度很慢(请参阅下面的完整命令)
  • I tried DNS resolution using bind-tools and it was fast. 我尝试使用绑定工具进行 DNS解析,而且速度很快。
  • Database runs in same region as Kubernetes nodes, tried even same resource group, different network settings and nothing helped. 数据库在与Kubernetes节点相同的区域中运行,甚至尝试了相同的资源组,不同的网络设置,也无济于事。
  • I tried requiring/disabling SSL mode on both client and server 我尝试在客户端和服务器上要求/禁用SSL模式
  • I tried repeatedly running 'select 1' inside an already established connection and it was fast (average 1.2ms, median 0.9ms) (see full query below) 我尝试在已经建立的连接中反复运行“选择1”,它运行很快(平均1.2毫秒,中值0.9毫秒)(请参阅下面的完整查询)

What can cause such a latency? 什么会导致这种延迟? How can I further debug/investigate this issue? 如何进一步调试/调查此问题?


psql command used to try connection: 用于尝试连接的psql命令:

psql "sslmode=disable host=host dbname=postgres user=user@host.postgres.database.azure.com password=password" -c "select 1"

Query speed 查询速度

\timing 
SELECT;
\watch 1

As far as I can tell it is caused by Azure specific authentication on top of PostgreSQL. 据我所知,这是由PostgreSQL之上的Azure特定身份验证引起的。 Unfortunately Azure support was not able to help from their side. 不幸的是,Azure支持无法从他们这方面提供帮助。

Using connection pool ( PgBouncer ) solves this problem. 使用连接池( PgBouncer )解决了此问题。 It is another piece of infrastructure we have to maintain (docker file, config/secret management, etc.), which we hoped to outsource to cloud provider. 这是我们必须维护的另一基础架构(docker文件,配置/秘密管理等),我们希望将其外包给云提供商。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM