如何解决如何优化包含to_char的where子句查询
我需要优化where子句查询以降低使用解释计划时的成本。
下面是原始语句
create index idx on orders(o_orderdate)
select *
from orders
where (to_char(o_orderdate,'dd-mon-yy') = '23-MAR-97' and o_totalprice > 2) or (not o_custkey > 3 and to_char(o_orderdate,'dd-mon-yyyy') = '23-MAR-1997');
这是我从原始查询中获得的解释计划
但是当我尝试使用此查询对其进行优化
select *
from orders
where o_totalprice > 2
and o_custkey < 3
and o_orderdate >= to_date('23MAR97','dd-mon-yy')
and o_orderdate <= to_date('24MAR1997','dd-mon-yyyy');
返回未选择的行
我如何优化原始查询位置并仍然返回4500行?
解决方法
您没有正确解释OR
。会是:
select *
from orders
where o_orderdate between to_date('23-MAR-1997','DD-MON-YYYY')
and to_date('24-MAR-1997','DD-MON-YYYY') - INTERVAL '1' SECOND
and ( o_totalprice > 2
or not o_custkey > 3 )
;
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。