[英]How to optimize where clause query that contains to_char
當我使用解釋計划時,我需要優化 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');
它不返回選擇的行
如何優化原始 where 查詢並仍然返回 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 )
;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.