首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PostgreSQL:在使用横向连接和聚合初始化查询计划时没有分区修剪

问PostgreSQL:在使用横向连接和聚合初始化查询计划时没有分区修剪
EN

Stack Overflow用户
提问于 2020-01-21 09:58:07
回答 1查看 275关注 0票数 1

我想知道为什么分区剪枝不适用于横向连接和聚合。下面是我在PostgreSQL 12.1 (Ubuntu12.1-1.pgdg18.04+1)上测试的示例:

代码语言:javascript
复制
CREATE TABLE demo(key BIGINT) PARTITION BY LIST (key);
CREATE TABLE demo_key_1 PARTITION OF demo FOR VALUES IN (1);
CREATE TABLE demo_key_2 PARTITION OF demo FOR VALUES IN (2);
INSERT INTO demo(key) VALUES (1), (2);
ANALYZE demo;



CREATE TABLE demo2(key BIGINT) PARTITION BY LIST (key);
CREATE TABLE demo2_key_1 PARTITION OF demo2 FOR VALUES IN (1);
CREATE TABLE demo2_key_2 PARTITION OF demo2 FOR VALUES IN (2);
INSERT INTO demo2(key) VALUES (1), (2);
ANALYZE demo2;

现在,如果在横向联接下SELECT中没有聚合,则所有操作都按预期进行--只扫描每个表的单个分区:

代码语言:javascript
复制
EXPLAIN ANALYZE
         SELECT * FROM demo
         JOIN LATERAL (
                  SELECT key AS key2
                  FROM demo2
                  WHERE demo2.key = demo.key
         ) d ON TRUE
         WHERE demo.key = 1;
                                                QUERY PLAN
-----------------------------------------------------------------------------------------------------------
 Nested Loop  (cost=0.00..2.03 rows=1 width=16) (actual time=0.007..0.008
rows=1 loops=1)
   ->  Seq Scan on demo_key_1  (cost=0.00..1.01 rows=1 width=8) (actual
time=0.004..0.005 rows=1 loops=1)
         Filter: (key = 1)
   ->  Seq Scan on demo2_key_1  (cost=0.00..1.01 rows=1 width=8) (actual
time=0.001..0.001 rows=1 loops=1)
         Filter: (key = 1)
 Planning Time: 0.191 ms
 Execution Time: 0.025 ms
(7 rows)

但是,当我尝试包含聚合函数的非常类似的查询时,demo2的分区不会从查询计划中被剪除:

代码语言:javascript
复制
EXPLAIN ANALYZE
         SELECT * FROM demo
         JOIN LATERAL (
                  SELECT sum(demo2.key) AS sum2
                  FROM demo2
                  WHERE demo2.key = demo.key
         ) d ON TRUE
         WHERE demo.key = 1;
                                                      QUERY PLAN
-----------------------------------------------------------------------------------------------------------------------
 Nested Loop  (cost=2.03..3.07 rows=1 width=40) (actual time=0.018..0.018
rows=1 loops=1)
   ->  Seq Scan on demo_key_1  (cost=0.00..1.01 rows=1 width=8) (actual
time=0.005..0.005 rows=1 loops=1)
         Filter: (key = 1)
   ->  Aggregate  (cost=2.03..2.04 rows=1 width=32) (actual
time=0.011..0.011 rows=1 loops=1)
         ->  Append  (cost=0.00..2.03 rows=2 width=8) (actual
time=0.004..0.005 rows=1 loops=1)
               ->  Seq Scan on demo2_key_1  (cost=0.00..1.01 rows=1
width=8) (actual time=0.002..0.002 rows=1 loops=1)
                     Filter: (key = demo_key_1.key)
               ->  Seq Scan on demo2_key_2  (cost=0.00..1.01 rows=1
width=8) (never executed)
                     Filter: (key = demo_key_1.key)
 Planning Time: 0.174 ms
 Execution Time: 0.082 ms
(11 rows)

当然,demo2_key_2上的Seq扫描从未被执行过,但是为什么它不被从查询计划中删除呢?具有数百个分区的更复杂的查询受此影响很大。

解决方法是在子查询中添加一个冗余条件:

代码语言:javascript
复制
EXPLAIN ANALYZE
         SELECT * FROM demo
         JOIN LATERAL (
                  SELECT sum(demo2.key) AS sum2
                  FROM demo2
                  WHERE demo2.key = 1 AND demo2.key = demo.key
         ) d ON TRUE
         WHERE demo.key = 1;
                                                      QUERY PLAN
-----------------------------------------------------------------------------------------------------------------------
 Nested Loop  (cost=1.01..2.05 rows=1 width=40) (actual time=0.011..0.011
rows=1 loops=1)
   ->  Seq Scan on demo_key_1  (cost=0.00..1.01 rows=1 width=8) (actual
time=0.004..0.004 rows=1 loops=1)
         Filter: (key = 1)
   ->  Aggregate  (cost=1.01..1.02 rows=1 width=32) (actual
time=0.006..0.006 rows=1 loops=1)
         ->  Result  (cost=0.00..1.01 rows=1 width=8) (actual
time=0.002..0.002 rows=1 loops=1)
               One-Time Filter: (demo_key_1.key = 1)
               ->  Seq Scan on demo2_key_1  (cost=0.00..1.01 rows=1
width=8) (actual time=0.001..0.002 rows=1 loops=1)
                     Filter: (key = 1)
 Planning Time: 0.079 ms
 Execution Time: 0.031 ms
(10 rows)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-01-21 10:12:28

优化器不够聪明,无法推断它可以跳过分区。但是,由于v12中存在运行时分区修剪,所以执行程序将跳过扫描分区。

这不应该对查询性能产生明显的负面影响。

优化器的智能性是有限度的。如果您想要删除分区,最好使用简单的条件。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59838508

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档