KES union all如何快速查询处理

union all操作

UNION ALL 运算符可合并多个 SELECT 命令的输出,而不会消除重复的行。

UNION ALL运算符涉及每个 SELECT命令,以在相同数据类型的输出中具有相等数量的字段。

这里注意union和union all的区别在于union是去重的,而union all不去重。

KES union all语法

KES union all的语法如下:

SELECT expression_1, expression_2, ... expression_n
FROM tables
[WHERE condition(s)]
UNION ALL
SELECT expression_1, expression_2, ... expression_n
FROM tables
[WHERE condition(s)];

注意: 在以上语法中,两个表达式必须具有相等数量的表达式。

各参数说明如下:

参数说明
expression_1,expression_2,... expression_n要检索的target列
tables要查询的表
where条件过滤条件

我们可以通过下图了解 UNION ALL运算符的工作方式:

 KES对union all的优化

KES对顶层的union all进行扁平化处理,将union all集合操作转换成AppendRelInfo的形式。例如下面的例子中:

explain select * from tab_01 where id < 100 union all select * from tab_02 where id < 50;

                             QUERY PLAN
---------------------------------------------------------------------
 Append  (cost=0.00..596067.72 rows=3158 width=18)
   ->  Seq Scan on tab_01  (cost=0.00..298007.66 rows=1579 width=18)
         Filter: (id < 100)
   ->  Seq Scan on tab_02  (cost=0.00..298012.69 rows=1579 width=18)
         Filter: (id < 50)
(5 rows)
 

通过计划可以看到KES将两个查询转换成Append节点进行处理。

KES未做优化前union all的查询树结构如下:

 优化前,每个范围表是一个子查询,集合操作的左右参数是两个子查询。

KES将union all优化后的查询结构如下:

 KES优化后增加Append节点进行处理。

开启并行后的执行计划如下:

explain select * from tab_01 where id < 100 union all select * from tab_02 where id < 50;

                                    QUERY PLAN
-----------------------------------------------------------------------------------
 Gather  (cost=1000.00..281519.24 rows=3158 width=18)
   Workers Planned: 5
   ->  Parallel Append  (cost=0.00..280203.44 rows=3158 width=18)
         ->  Parallel Seq Scan on tab_02  (cost=0.00..140078.54 rows=316 width=18)
               Filter: (id < 50)
         ->  Parallel Seq Scan on tab_01  (cost=0.00..140077.53 rows=316 width=18)
               Filter: (id < 100)
(7 rows)
 

【更多人大金仓数据库信息,详见https://help.kingbase.com.cn/

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

helenbi

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值