Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

124
Views
How to optimize where clause query that contains to_char

i need to optimize the where clause query to cut down the cost when i use explain plan.

Below is the original statement

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');

original

This is the explain plan that i get from the original query

But when i try to optimize it using this query

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');

new where optimization

it return no rows selected

How do i optimize the original where query and still return 4500 rows?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

You did not account for the OR properly. It would be:

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 )
;
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!