I have a table with this schema -
CREATE TABLE `ox_data_summary_device_hourly` ( `data_summary_device_hourly_id` bigint(20) NOT NULL AUTO_INCREMENT, `date_time` datetime NOT NULL DEFAULT '0000-00-00 00:00:00', ... ... PRIMARY KEY (`data_summary_device_hourly_id`), KEY `ox_data_summary_device_daily_date_time` (`date_time`), KEY `ox_data_summary_device_daily_ad_country_date_time` (`ad_id`,`date_time`), KEY `ox_data_summary_device_daily_zone_id_date_time` (`zone_id`,`date_time`) ) ENGINE=InnoDB AUTO_INCREMENT=3478837682 DEFAULT CHARSET=latin1 So, the following indexes are set on a table -
KEY `ox_data_summary_device_daily_date_time` (`date_time`), KEY `ox_data_summary_device_daily_ad_country_date_time` (`ad_id`,`date_time`), KEY `ox_data_summary_device_daily_zone_id_date_time` (`zone_id`,`date_time`) I have this explain query on this table with filter on date_time -
explain SELECT * FROM ox_data_summary_device_hourly data_hourly WHERE data_hourly.date_time >= '2012-09-01 00:00:00' AND data_hourly.date_time < '2012-09-15 13:00:00' GROUP BY data_hourly.type ORDER BY data_hourly.type DESC Here is the output showing that the ox_data_summary_device_daily_date_time index is not applying -

However, if I change the date filter in the above mentioned query to the following -
data_hourly.date_time >= '2012-09-01 00:00:00' AND data_hourly.date_time < '2012-09-14 13:00:00'
i.e. if I query for 1st Sep to 14 sep instead of 1st to 15 sep, the ox_data_summary_device_daily_date_time index applies. I further checked that if the end date of the range is anything after 15 sep, the index does not apply.
I am puzzled. I checked that there is data in the table for date range >= 15th Sep.
Any ideas?