微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

对于postgresql and的简单猜想

今天做了这样的实验,目的是验证where条件后的a_expr and a_expr的顺序。
准备:
sql语句为:
create table a1 (a int);
create table a2 (a int);
create table a3 (a int,b int);
create table a4 (a int,b int);

insert into a1 values (0),(1);//(被除数)(除数)
insert into a2 values (0),(0);// (被除数)(除数)
insert into a3 values (0,0),(1,1);// (被除数,测试数)(除数,测试数)
insert into a4 values (0,1),0);// (被除数,测试数)(除数,测试数)

需要知道的:
select 4/0;
ERROR: division by zero
select * from a1 where (4 / a > 2) and (a > 0);// (4 / a > 2) a有可能为0
返回1;
select * from a1 where (4 / a > 2) and (a > a-1);
ERROR: division by zero
select * from a2 where (4 / a > 2) and (a > 0);
无返回;
select * from a3 where (4 / a > 2) and (b > 0);
返回1;
select * from a4 where (4 / a > 2) and (b > 0);
ERROR: division by zero

由此猜想select查询应该是有一步优化,首先对a处理,这样会加快速度,减少重复运算,从而,没有进行/0操作。

当没有and时,执行计划会根据通过语法解析的节点复杂度进行重新排序,而有or的时候,就会按照手写的顺序。

原文地址:https://www.jb51.cc/postgresql/196199.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐