为什么 PostgreSQL 选择更昂贵的连接顺序?

Pau*_*per 13 postgresql join execution-plan postgresql-10

PostgreSQL 使用默认值,加上

default_statistics_target=1000
random_page_cost=1.5
Run Code Online (Sandbox Code Playgroud)

版本

PostgreSQL 10.4 on x86_64-pc-linux-musl, compiled by gcc (Alpine 6.4.0) 6.4.0, 64-bit
Run Code Online (Sandbox Code Playgroud)

我已经抽真空并分析过。查询非常简单:

SELECT r.price
FROM account_payer ap
  JOIN account_contract ac ON ap.id = ac.account_payer_id
  JOIN account_schedule "as" ON ac.id = "as".account_contract_id
  JOIN schedule s ON "as".id = s.account_schedule_id
  JOIN rate r ON s.id = r.schedule_id
WHERE ap.account_id = 8
Run Code Online (Sandbox Code Playgroud)

id列都是主键,所有被连接的都是外键关系,每个外键都有一个索引。加上一个索引account_payer.account_id

返回 76k 行需要 3.93s。

Merge Join  (cost=8.06..83114.08 rows=3458267 width=6) (actual time=0.228..3920.472 rows=75548 loops=1)
  Merge Cond: (s.account_schedule_id = "as".id)
  ->  Nested Loop  (cost=0.57..280520.54 rows=6602146 width=14) (actual time=0.163..3756.082 rows=448173 loops=1)
        ->  Index Scan using schedule_account_schedule_id_idx on schedule s  (cost=0.14..10.67 rows=441 width=16) (actual time=0.035..0.211 rows=89 loops=1)
        ->  Index Scan using rate_schedule_id_code_modifier_facility_idx on rate r  (cost=0.43..486.03 rows=15005 width=10) (actual time=0.025..39.903 rows=5036 loops=89)
              Index Cond: (schedule_id = s.id)
  ->  Materialize  (cost=0.43..49.46 rows=55 width=8) (actual time=0.060..12.984 rows=74697 loops=1)
        ->  Nested Loop  (cost=0.43..49.32 rows=55 width=8) (actual time=0.048..1.110 rows=66 loops=1)
              ->  Nested Loop  (cost=0.29..27.46 rows=105 width=16) (actual time=0.030..0.616 rows=105 loops=1)
                    ->  Index Scan using account_schedule_pkey on account_schedule "as"  (cost=0.14..6.22 rows=105 width=16) (actual time=0.014..0.098 rows=105 loops=1)
                    ->  Index Scan using account_contract_pkey on account_contract ac  (cost=0.14..0.20 rows=1 width=16) (actual time=0.003..0.003 rows=1 loops=105)
                          Index Cond: (id = "as".account_contract_id)
              ->  Index Scan using account_payer_pkey on account_payer ap  (cost=0.14..0.21 rows=1 width=8) (actual time=0.003..0.003 rows=1 loops=105)
                    Index Cond: (id = ac.account_payer_id)
                    Filter: (account_id = 8)
                    Rows Removed by Filter: 0
Planning time: 5.843 ms
Execution time: 3929.317 ms
Run Code Online (Sandbox Code Playgroud)

如果我设置为join_collapse_limit=1,则需要 0.16 秒,加速 25 倍。

Nested Loop  (cost=6.32..147323.97 rows=3458267 width=6) (actual time=8.908..151.860 rows=75548 loops=1)
  ->  Nested Loop  (cost=5.89..390.23 rows=231 width=8) (actual time=8.730..11.655 rows=66 loops=1)
        Join Filter: ("as".id = s.account_schedule_id)
        Rows Removed by Join Filter: 29040
        ->  Index Scan using schedule_pkey on schedule s  (cost=0.27..17.65 rows=441 width=16) (actual time=0.014..0.314 rows=441 loops=1)
        ->  Materialize  (cost=5.62..8.88 rows=55 width=8) (actual time=0.001..0.011 rows=66 loops=441)
              ->  Hash Join  (cost=5.62..8.61 rows=55 width=8) (actual time=0.240..0.309 rows=66 loops=1)
                    Hash Cond: ("as".account_contract_id = ac.id)
                    ->  Seq Scan on account_schedule "as"  (cost=0.00..2.05 rows=105 width=16) (actual time=0.010..0.028 rows=105 loops=1)
                    ->  Hash  (cost=5.02..5.02 rows=48 width=8) (actual time=0.178..0.178 rows=61 loops=1)
                          Buckets: 1024  Batches: 1  Memory Usage: 11kB
                          ->  Hash Join  (cost=1.98..5.02 rows=48 width=8) (actual time=0.082..0.143 rows=61 loops=1)
                                Hash Cond: (ac.account_payer_id = ap.id)
                                ->  Seq Scan on account_contract ac  (cost=0.00..1.91 rows=91 width=16) (actual time=0.007..0.023 rows=91 loops=1)
                                ->  Hash  (cost=1.64..1.64 rows=27 width=8) (actual time=0.048..0.048 rows=27 loops=1)
                                      Buckets: 1024  Batches: 1  Memory Usage: 10kB
                                      ->  Seq Scan on account_payer ap  (cost=0.00..1.64 rows=27 width=8) (actual time=0.009..0.023 rows=27 loops=1)
                                            Filter: (account_id = 8)
                                            Rows Removed by Filter: 24
  ->  Index Scan using rate_schedule_id_code_modifier_facility_idx on rate r  (cost=0.43..486.03 rows=15005 width=10) (actual time=0.018..1.685 rows=1145 loops=66)
        Index Cond: (schedule_id = s.id)
Planning time: 4.692 ms
Execution time: 160.585 ms
Run Code Online (Sandbox Code Playgroud)

这些输出对我来说毫无意义。第一个(非常高)成本为 280,500,用于调度和速率索引的嵌套循环连接。为什么 PostgreSQL 故意先选择那个非常昂贵的连接?

通过评论请求的其他信息

rate_schedule_id_code_modifier_facility_idx复合指数吗?

它是schedule_id第一列。我已将其设为专用索引,由查询规划器选择,但不会影响性能或以其他方式影响计划。

Ark*_*ena 4

看来您的统计数据不准确(运行真空分析来刷新它们),或者您的模型中有相关列(因此您需要执行以create statistics告知刨床这一事实)。

join_collapse参数允许规划器重新排列连接,以便它首先执行获取较少数据的连接。但是,为了性能,我们不能让规划器在具有大量连接的查询上执行此操作。默认情况下,它设置为最多 8 个连接。通过将其设置为 1,您只需禁用该功能即可。

那么 postgres 如何预测这个查询应该获取多少行呢?它使用统计数据来估计行数。

我们在您的解释计划中可以看到,有几个不准确的行数估计(第一个值是估计值,第二个值是实际值)。

例如,这里:

Materialize  (cost=0.43..49.46 rows=55 width=8) (actual time=0.060..12.984 rows=74697 loops=1)
Run Code Online (Sandbox Code Playgroud)

规划者估计得到 55 行,而实际得到了 74697 行。

我会做的(如果我处于你的立场)是:

  • analyze涉及刷新统计信息的五个表
  • 重播explain analyze
  • 查看估计行数和实际行数之间的差异
  • 如果估计的行数是正确的,那么计划可能会改变并且效率更高。如果一切正常,您可以考虑更改 autovacuum 设置,以便更频繁地执行分析(和真空)
  • 如果估计行数仍然错误,则似乎您的表中存在关联数据(第三范式违规)。您可以考虑使用CREATE STATISTICS此处的文档)来声明它

如果您需要有关行估计及其计算的更多信息,您可以在 Tomas Vondra 的会议演讲“创建统计数据 - 它的用途是什么?”中找到所需的一切。(幻灯片在这里