3

我有 Azure Database for PostgreSQL 服务 (PaaS)。当我尝试使用它进行查询时,psql即使是来自一个表的简单SELECT查询也需要大约 1.5 秒。当我在 postgres 控制台中时,没有问题,查询在 100 毫秒内执行。什么会导致连接时间延迟?

for I in {1..15} ; do /usr/bin/time -f "%es" psql -U postgres@user  -h  my-server.postgres.database.azure.com database_name -c "SELECT count(*) FROM table_name" > /dev/null ; done
1.86s
1.95s
1.60s
1.03s
1.59s
1.58s
1.60s
1.68s
1.60s
1.60s
1.62s
1.63s
1.61s
1.83s
1.70s

仅供参考:表中有 11 个条目

更新 下面的测试证明这是连接延迟而不是在 postgres 上执行查询本身

$ psql -t -U postgres@resource-group  -h  postgresql-project.postgres.database.azure.com stage_table <<EOF
SELECT count(*), clock_timestamp() FROM my_table;
SELECT count(*), clock_timestamp() FROM my_table;
SELECT count(*), clock_timestamp() FROM my_table;
SELECT count(*), clock_timestamp() FROM my_table;
SELECT count(*), clock_timestamp() FROM my_table;
SELECT count(*), clock_timestamp() FROM my_table;
SELECT count(*), clock_timestamp() FROM my_table;
SELECT count(*), clock_timestamp() FROM my_table;
SELECT count(*), clock_timestamp() FROM my_table;
SELECT count(*), clock_timestamp() FROM my_table;
SELECT count(*), clock_timestamp() FROM my_table;
SELECT count(*), clock_timestamp() FROM my_table;
SELECT count(*), clock_timestamp() FROM my_table;
SELECT count(*), clock_timestamp() FROM my_table;
SELECT count(*), clock_timestamp() FROM my_table;
EOF
    17 | 2019-07-23 13:26:11.475655+00
    17 | 2019-07-23 13:26:11.506887+00
    17 | 2019-07-23 13:26:11.553769+00
    17 | 2019-07-23 13:26:11.585022+00
    17 | 2019-07-23 13:26:11.616273+00
    17 | 2019-07-23 13:26:11.64752+00
    17 | 2019-07-23 13:26:11.678787+00
    17 | 2019-07-23 13:26:11.710046+00
    17 | 2019-07-23 13:26:11.74129+00
    17 | 2019-07-23 13:26:11.772524+00
    17 | 2019-07-23 13:26:11.803794+00
    17 | 2019-07-23 13:26:11.835042+00
    17 | 2019-07-23 13:26:11.86629+00
    17 | 2019-07-23 13:26:11.897517+00
    17 | 2019-07-23 13:26:11.944397+00
$ 

我也在 azure 上读到过accelerated networking,但这看起来只适用于 VM 本身。我可以在 PostgreSQL 服务上启用此加速吗?https://docs.microsoft.com/en-us/azure/virtual-network/create-vm-accelerated-networking-cli

4

1 回答 1

3

延迟是建立数据库连接所花费的时间。

1.5秒相当蹩脚。但它不一定是杀手,因为您应该使用连接池并保持数据库会话打开。

可以肯定的是,您可以将结果与

psql -U postgres@user -h  my-server.postgres.database.azure.com database_name <<EOF
SELECT count(*), clock_timestamp() FROM table_name;
SELECT count(*), clock_timestamp() FROM table_name;
SELECT count(*), clock_timestamp() FROM table_name;
SELECT count(*), clock_timestamp() FROM table_name;
SELECT count(*), clock_timestamp() FROM table_name;
SELECT count(*), clock_timestamp() FROM table_name;
SELECT count(*), clock_timestamp() FROM table_name;
SELECT count(*), clock_timestamp() FROM table_name;
SELECT count(*), clock_timestamp() FROM table_name;
SELECT count(*), clock_timestamp() FROM table_name;
SELECT count(*), clock_timestamp() FROM table_name;
SELECT count(*), clock_timestamp() FROM table_name;
SELECT count(*), clock_timestamp() FROM table_name;
SELECT count(*), clock_timestamp() FROM table_name;
SELECT count(*), clock_timestamp() FROM table_name;
EOF

那应该更快。不同之处在于连接时间。

于 2019-07-18T15:05:06.870 回答