You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PostgreSQL全表扫描为何如此缓慢?性能优化咨询

PostgreSQL无索引全表扫描性能优化疑问

我在AWS全新的db.m5.8xlarge实例上测试PostgreSQL无索引全表扫描的性能,发现速度出奇缓慢。测试执行的SQL如下:

CREATE TABLE test100m AS SELECT * FROM GENERATE_SERIES(1, 100000000) AS id;

SET max_parallel_workers_per_gather = 6;

EXPLAIN ANALYZE SELECT max(id) FROM test100m;

执行结果:

QUERY PLAN                                                                     
--------------------------------------------------------------------------------------------------------------------------------------------------
 Finalize Aggregate  (cost=651812.03..651812.04 rows=1 width=4) (actual time=1817.850..1819.931 rows=1 loops=1)
   ->  Gather  (cost=651811.40..651812.01 rows=6 width=4) (actual time=1817.788..1819.921 rows=7 loops=1)
         Workers Planned: 6
         Workers Launched: 6
         ->  Partial Aggregate  (cost=650811.40..650811.41 rows=1 width=4) (actual time=1814.193..1814.194 rows=1 loops=7)
               ->  Parallel Seq Scan on test100m  (cost=0.00..609144.72 rows=16666672 width=4) (actual time=0.003..902.986 rows=14285714 loops=7)
 Planning Time: 0.055 ms
 Execution Time: 1819.953 ms

扫描1亿条数据耗时约1800ms。而在性能相近的6核笔记本上,用Go语言扫描1亿个数组元素仅需38ms:

func TestTiming(t *testing.T) {
    {
        data := make([]int, 100000000)
        for i := 0; i < len(data); i++ {
            data[i] = i
        }
        start := time.Now()
        max := data[0]
        for i := 0; i < len(data); i++ {
            if max < data[i] {
                max = data[i]
            }
        }
        fmt.Printf("Timing: 100,000,000 %s\n", time.Since(start))
    }
}

两者性能相差约50倍。虽非完全对等的测试,但差距远超预期,且所有数据均可轻松放入内存。除调整max_parallel_workers_per_gather外,是否有方法显著提升PostgreSQL全表扫描性能?为何会有如此大的性能差距?


更新:附上更详细的查询计划:

> EXPLAIN (ANALYZE, BUFFERS, TIMING) SELECT max(id) FROM test100m;


                                                                    QUERY PLAN                                                                     
--------------------------------------------------------------------------------------------------------------------------------------------------
 Finalize Aggregate  (cost=651812.03..651812.04 rows=1 width=4) (actual time=1953.561..1955.891 rows=1 loops=1)
   Buffers: shared hit=442478
   ->  Gather  (cost=651811.40..651812.01 rows=6 width=4) (actual time=1953.505..1955.885 rows=7 loops=1)
         Workers Planned: 6
         Workers Launched: 6
         Buffers: shared hit=442478
         ->  Partial Aggregate  (cost=650811.40..650811.41 rows=1 width=4) (actual time=1950.497..1950.497 rows=1 loops=7)
               Buffers: shared hit=442478
               ->  Parallel Seq Scan on test100m  (cost=0.00..609144.72 rows=16666672 width=4) (actual time=0.004..916.197 rows=14285714 loops=7)
                     Buffers: shared hit=442478
 Planning Time: 0.059 ms
 Execution Time: 1955.916 ms

内容的提问来源于stack exchange,提问作者Arman

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.13 13:41:42