I have this MySQL query that I'd like to optimize:
SELECT min(`topic_id`) FROM `gallery_topics` where `topic_id` > 11 AND `topic_poster` = 5 AND `topic_status` = 0
I wanted to add an index with multiple fields to optimize this query, so I added a key I named previous_by_author with these three keys in this order:
- topic_id
- topic_poster
- topic_status
I did an EXPLAIN of this query, with the expectation that the EXPLAIN query would tell me that the query uses my new previous_by_author index. However, EXPLAIN reported that the query uses the user_start_time index, which uses the following two fields: 1. topic_poster and 2. topic_start_time.
I don't get it, topic_start_time is a datimetime field that isn't even in my query!
Here's the full result of the EXPLAIN query:
id select_type table type possible_keys 1 SIMPLE gallery_topics ref PRIMARY,user_start_time,previous_by_author (rows continued) key key_len ref rows Extra user_start_time 8 const 4 Using where
What fields should I use in my multiple-field index so that my query can take advantage of the best index?