I am experiencing strange behavior out of MySQL 5.1.30. I have a table with (among others) a datetime field. The field has an index on it. My query does a join of two tables: image that contains time data, and image_change that is associated with image through a foreign key constraint. The image_change table contains about 2M records.
When I execute this query:
explain select ic.* from image_change ic, image iwhere ic.image_id=i.id and i.camera_id=2 and (i.time > ‘2009-03-01’ and i.time <‘2009-04-15’)
I get the following response:
1, ‘SIMPLE’, ‘i’, ‘range’, ‘PRIMARY,FK5FAA95B637C4FBA,Index_Time’, ‘Index_Time’, ‘8’, ‘’, 746, 'Using where’1, ‘SIMPLE’, ‘ic’, ‘ref’, ‘FK10B73B14D0FB3FBA,Index_Image’, ‘FK10B73B14D0FB3FBA’, ‘8’, ‘reboard.i.id’, 558, ‘’
Notice that ‘Index_Time’ is being used. When I run the same query with a slightly different date:
explain select ic.* from image_change ic, image iwhere ic.image_id=i.id and i.camera_id=2 and (i.time > ‘2009-03-01’ and i.time <‘2009-04-16’)
I get a response that indicates that the index is not being used.
1, ‘SIMPLE’, ‘ic’, ‘ALL’, ‘FK10B73B14D0FB3FBA,Index_Image’, ‘’, ‘’, ‘’, 2065041, ''1, ‘SIMPLE’, ‘i’, ‘eq_ref’, ‘PRIMARY,FK5FAA95B637C4FBA,Index_Time’, ‘PRIMARY’, ‘8’, ‘reboard.ic.image_id’, 1, ‘Using where’
If I actually run the query that doesn’t use the index, it takes about two orders of magnitude slower to run.
Any thoughts on what to check?
Thanks,
Gene