KingbaseES where 条件解析顺序

概述

KingbaseES 对于where 条件的解析严格遵守“从左到右”的原则,因此,对于选择性比较强的条件,进行最先过滤是有利于性能的。

一、KingbaseES

1、条件顺序影响执行效率

例子:

create table t1(id1 integer,id2 integer);
insert into t1 select generate_series(1,1000000),generate_series(1,1000000);

条件顺序影响性能:经 id1 过滤掉的数据更多,放在前面效率更优。

test=# explain analyze select * from t1 where id2>1000000 and id1>9999000;
                                                  QUERY PLAN
--------------------------------------------------------------------------------------------------------------
 Seq Scan on t1  (cost=0.00..194248.72 rows=1111116 width=8) (actual time=815.263..815.350 rows=1000 loops=1)
   Filter: ((id2 > 1000000) AND (id1 > 9999000))
   Rows Removed by Filter: 9999000
 Planning Time: 0.064 ms
 Execution Time: 815.391 ms
(5 rows)

test=# explain analyze select * from t1 where id1>9999000 and id2>1000000;
                                                QUERY PLAN
----------------------------------------------------------------------------------------------------------
 Seq Scan on t1  (cost=0.00..194247.65 rows=899 width=8) (actual time=747.644..747.733 rows=1000 loops=1)
   Filter: ((id1 > 9999000) AND (id2 > 1000000))
   Rows Removed by Filter: 9999000
 Planning Time: 0.069 ms
 Execution Time: 747.773 ms
(5 rows)

2、条件顺序影响SQL执行

例子:

create table t1(id1 varchar(10),id2 varchar(10));
insert into t1 values('123','abc');

test=# select * from t1 where id1=1 and id2=2;
 id1 | id2
-----+-----
(0 rows)

test=# select * from t1 where id2=2 and id1=1;
ERROR:  invalid input syntax for type integer: "abc"

问题分析:这里的语句实际 是:select * from t1 where to_number(id1)=1 and to_number(id2)=2 。语句在执行时,先根据 to_number(id1)=1 条件进行过滤,只有满足to_number(id1)=1条件,才 过来 to_number(id2)=2 条件。

语句1:to_number(123)=1 不满足,因此,没必要执行 to_number(id2)=2 ,不会报错

语句2:先执行 to_number(id2)=2 ,直接报错了。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值