I have a couple tables, events and medias, and am having trouble with a query and a multiple column index.
Basically I want to only get events that have medias (images and videos) attached to them. So I have a query like this:
SELECT events.* FROM eventsLEFT OUTER JOIN medias ON medias.event_id = events.id WHERE start IS NOT NULL AND is_public = 1 AND start < CURDATE() AND medias.id IS NOT NULL ORDER BY events.start ASC
To optimize the query I have created a multiple column index for event.is_public and event.start. When I run examine, however, it only uses PRIMARY:
*************************** 1. row *************************** id: 1 select_type: SIMPLE table: medias type: indexpossible_keys: PRIMARY,index_medias_on_event_id key: index_medias_on_event_id key_len: 5 ref: NULL rows: 2753 Extra: Using where; Using index; Using temporary; Using filesort*************************** 2. row *************************** id: 1 select_type: SIMPLE table: events type: eq_refpossible_keys: PRIMARY,index_events_on_start_and_is_public key: PRIMARY key_len: 4 ref: mixer.medias.event_id rows: 1 Extra: Using where2 rows in set (0.00 sec)
When I remove “medias.id IS NOT NULL” from the query it will use the appropriate index.
Is there a way to fix this so that I don’t need to use index hinting?
Thanks.