我的 Postgres 数据库表中有大约 10 亿行和 6 列(id、col1、col2..col5)。我有 id 作为我的 PRIMARY_KEY。我将其用作只读数据库,每次我只需要根据 10000 个 ID 列表检索/读取 10000 行。查询需要 40-60 秒才能检索 10000 行,这非常慢。以下是我的查询:
SELECT *
FROM table_1b
WHERE id IN (18792, 8718, 15010, 16671,.....*list of 10000 ids*....., 290224379, 192494270)
EXPLAIN ANALYZE 查询:
Index Scan using table_1b_pkey on table_1b
(cost=0.57..46197.28 rows=10000 width=239)
(actual time=13.305..55927.060 rows=10000 loops=1)
Index Cond: (id = ANY ('{18792, 8718, 15010, 16671,............,290224379,192494270}'::bigint[]))
Planning Time: 13.380 ms
Execution Time: 55935.614 ms
我想显着加快 10000 行的检索速度。我对 Postgres 相当陌生。我已阅读其他帖子并尝试了一些方法,包括将 work_mem 从 4 MB 修改为 256 MB,但没有帮助。Postgres 服务器在具有 384 GB RAM 的机器上运行。
提前感谢任何帮助。
编辑 1:解释 10 条记录的分析
Index Scan using table_1b_pkey on table_1b
(cost=0.57..49.95 rows=10 width=239)
(actual time=17.204..123.489 rows=10 loops=1)
Planning Time: 0.378 ms
Execution Time: 123.540 ms
编辑 2:track_io_timing = ON;解释(分析,缓冲区)
Index Scan using table_1b_pkey on table_1b
(cost=0.57..17243.33 rows=10000 width=239)
(actual time=17.304..55371.511 rows=10000 loops=1)
Buffers: shared hit=29891 read=20108
I/O Timings: read=55032.422
Planning Time: 13.113 ms
Execution Time: 55380.644 ms
编辑 3:更新的服务器配置
max_connections = 20
shared_buffers = 25GB
effective_cache_size = 75GB
maintenance_work_mem = 2GB
checkpoint_completion_target = 0.7
wal_buffers = 16MB
default_statistics_target = 100
random_page_cost = 1.1
effective_io_concurrency = 200
work_mem = 640MB
min_wal_size = 1GB
max_wal_size = 4GB