我们在两个不同的服务器上运行两个不同版本的 Postgresql:
- 服务器A:Postgresql 9.3
- 服务器 B:Postgresql 15.3
服务器 B 比服务器 A 更强大:
- 服务器 A:1 个 Intel Xeon E3-1270 3.5GHz,2 个 8GB DDR3,RAID0
- 服务器 B:2 个 Intel Xeon Platinum 8260 2.4GHz,4 个 16GB DDR4,RAID1
当我们SELECT
在两台服务器上运行快速短查询(< 1 毫秒)时,服务器 A 始终比服务器 B 运行得更快,即使查询计划完全相同:
服务器A:
# EXPLAIN (ANALYZE, BUFFERS, VERBOSE) SELECT 1 AS "a" FROM "foobar" WHERE ("foobar"."id" = 1) LIMIT 1;
QUERY PLAN
------------------------------------------------------------------------------------------------------------------------------------------------------------
Limit (cost=0.42..8.44 rows=1 width=0) (actual time=0.008..0.008 rows=1 loops=1)
Output: (1)
Buffers: shared hit=5
-> Index Only Scan using foobar_pkey on public.foobar (cost=0.42..8.44 rows=1 width=0) (actual time=0.008..0.008 rows=1 loops=1)
Output: 1
Index Cond: (foobar.id = 1)
Heap Fetches: 1
Buffers: shared hit=5
Total runtime: 0.017 ms
(9 rows)
Time: 0.281 ms
服务器乙:
# EXPLAIN (ANALYZE, BUFFERS, VERBOSE) SELECT 1 AS "a" FROM "foobar" WHERE ("foobar"."id" = 1) LIMIT 1;
QUERY PLAN
------------------------------------------------------------------------------------------------------------------------------------------------------------
Limit (cost=0.00..1.11 rows=1 width=4) (actual time=0.019..0.021 rows=1 loops=1)
Output: 1
Buffers: shared hit=4
-> Index Only Scan using foobar_pkey on public.foobar (cost=0.00..1.11 rows=1 width=4) (actual time=0.018..0.018 rows=1 loops=1)
Output: 1
Index Cond: (foobar.id = 1)
Heap Fetches: 0
Buffers: shared hit=4
Planning Time: 0.110 ms
Execution Time: 0.045 ms
(10 rows)
Time: 0.635 ms
如果我们从磁盘读取,RAID1 可能会在服务器 B 上增加一些延迟,但我们已经确认这些查询正在命中缓冲区/缓存,因此从内存而不是磁盘读取数据。我们使用以下查询检查了命中率:
SELECT
'cache hit rate' AS name,
sum(heap_blks_hit) / (sum(heap_blks_hit) + sum(heap_blks_read)) AS ratio
FROM
pg_statio_user_tables;
两台服务器的延迟都非常低,但是当您同时运行一堆快速的短查询时,总的来说您会看到差异,服务器 A 平均比服务器 B 快 0.1-1.0 秒。
服务器 B 有 2 个 CPU,并且在 Linux 上使用 NUMA。也许这会增加一些开销/延迟?Transparent Huge Tables 在 Linux 上设置为“madvise”,在 Postgresql 上huge_pages
设置为“madvise” try
。
有任何想法吗?
回答我自己的问题,延迟是由于CPU governor造成的。它默认设置为“powersave”。将其更改为“性能”可使服务器响应更快。您在这里有进一步的说明。