Here is the query which needs to be optimized:
Scans: 527676 records...
Returns: 496 records...
SELECT * FROM customers c
WHERE DATE_ADD(c.`custdate` ,INTERVAL -5 HOUR) BETWEEN '2011-09-14 00:00:00' AND '2011-09-14 23:59:59'
AND c.`custtype`='L'
This is the same without using DATE_ADD:
Scans: 801 records...
Returns: 481 records...
SELECT * FROM customers c
WHERE c.`custdate` BETWEEN '2011-09-14 00:00:00' AND '2011-09-14 23:59:59'
Question is how should I optimize this query using DATE_ADD() function? Without using DATE_ADD it picks up my indexes and query is fine.
Awaiting help on this.
Thanks,
Raheel
Since you’re just adding a constant time interval to
c.custdate, what should work is instead to subtract that time interval from theBETWEENvalues, to obtain a query that’s logically equivalent but permits MySQL to use the index. In general, if the query requires evaluation of a function of the index values, a range scan can’t be used.That is: instead of the
WHEREclause beingmake it