简体   繁体   中英

MySQL: Add index on column with condition and operation

In a query like this

SELECT * FROM myTable WHERE date = LEAST(maxDate, '2013-12-31')

I am looking for an index that will be used during execution. date and maxDate are Date types.

Any suggestions?

Use of function(UDF or built-in) in WHERE clause don't take advantage of existing indexes but you can modify your query like below which will be using the already existing indexes on date or maxdate (if there is any) column like

SELECT * FROM myTable 
WHERE date = case when maxDate > '2013-12-31' then maxDate else '2013-12-31' end 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM