【发布时间】:2020-10-04 09:39:26
【问题描述】:
我正在对 postgresql 12 上的表运行一个大计数(DISTINCT)查询组。该表大约为 32GB,300MM 行。它按年份划分。这些组或多或少完全分布:
EXPLAIN (ANALYZE,BUFFERS)
SELECT
date_trunc('month', condition_start_date::timestamp) as dt,
condition_source_value,
COUNT(DISTINCT person_id)
FROM synpuf5.condition_occurrence_yrpart
GROUP BY date_trunc('month', condition_start_date::timestamp), condition_source_value
ORDER BY COUNT(DISTINCT person_id) DESC LIMIT 10;
这是查询计划器的输出:
QUERY PLAN
Limit (cost=50052765961.82..50052765961.85 rows=10 width=21) (actual time=691022.306..691022.308 rows=10 loops=1)
Buffers: shared hit=3062256 read=222453
-> Sort (cost=50052765961.82..50052777188.87 rows=4490820 width=21) (actual time=690786.364..690786.364 rows=10 loops=1)
Sort Key: (count(DISTINCT condition_occurrence_yrpart_2007.person_id)) DESC
Sort Method: top-N heapsort Memory: 26kB
Buffers: shared hit=3062256 read=222453
-> GroupAggregate (cost=50049709699.80..50052668916.82 rows=4490820 width=21) (actual time=567099.326..690705.612 rows=360849 loops=1)
Group Key: (date_trunc('month'::text, (condition_occurrence_yrpart_2007.condition_start_date)::timestamp without time zone)), condition_occurrence_yrpart_2007.condition_source_value
Buffers: shared hit=3062253 read=222453
-> Sort (cost=50049709699.80..50050432663.48 rows=289185472 width=17) (actual time=567098.345..619461.044 rows=289182385 loops=1)
Sort Key: (date_trunc('month'::text, (condition_occurrence_yrpart_2007.condition_start_date)::timestamp without time zone)), condition_occurrence_yrpart_2007.condition_source_value
Sort Method: quicksort Memory: 30333184kB
Buffers: shared hit=3062246 read=222453
-> Append (cost=10000000000.00..50009068412.44 rows=289185472 width=17) (actual time=0.065..74222.771 rows=289182385 loops=1)
Buffers: shared hit=3062240 read=222453
-> Seq Scan on condition_occurrence_yrpart_2007 (cost=10000000000.00..10000001125.61 rows=42774 width=17) (actual time=0.064..13.756 rows=42774 loops=1)
Buffers: shared read=484
-> Seq Scan on condition_occurrence_yrpart_2008 (cost=10000000000.00..10002732063.72 rows=103678448 width=17) (actual time=0.039..21209.532 rows=103676930 loops=1)
Buffers: shared hit=954918 read=221969
-> Seq Scan on condition_occurrence_yrpart_2009 (cost=10000000000.00..10003024874.44 rows=114743696 width=17) (actual time=0.142..20191.131 rows=114743002 loops=1)
Buffers: shared hit=1303719
-> Seq Scan on condition_occurrence_yrpart_2010 (cost=10000000000.00..10001864406.36 rows=70720224 width=17) (actual time=0.050..12464.117 rows=70719679 loops=1)
Buffers: shared hit=803603
-> Seq Scan on condition_occurrence_yrpart_2011 (cost=10000000000.00..10000000014.95 rows=330 width=17) (actual time=0.022..0.022 rows=0 loops=1)
我还大量配置了我的 postgresql 以尝试将所有数据放入内存中,包括:
shared_buffers = 80GB
work_mem = 32GB
max_worker_processes = 32
max_parallel_workers_per_gather = 16
max_parallel_workers = 32
wal_compression = on
max_wal_size = 8GB
enable_seqscan = off
enable_partitionwise_join = on
enable_partitionwise_aggregate = on
parallel_tuple_cost = 0.01
parallel_setup_cost = 100.0
shared_preload_libraries = 'pg_prewarm'
effective_cache_size = 192GB
我正在运行的虚拟机非常庞大。 256 GB 内存,32 核。 SSD 是 postgres 目录所在的位置...
这里有几个问题:
- 为什么这么慢?
- 为什么不并行运行?
- 尽管有 pg_prewarm,为什么我再次运行时性能没有提高?
- 为什么我的会话结束时会释放内存?我正在使用预热?
【问题讨论】:
标签: postgresql performance explain