I’m having a little trouble getting MySQL to put an INDEX() on the right fields. I have a query that would look, for example, something like this:
SELECT m.id as mid,p.id as pid,po.* FROM m
LEFT OUTER JOIN p ON p.mid = m.id AND p.uid = 2
LEFT OUTER JOIN po ON po.mid = m.id AND po.uid = 2
ORDER BY m.date,m.time
And I have the following indexes:
- m.date
- m.time
- p.mid
- p.uid
- po.mid
- po.uid
However, when I run an EXPLAIN query, none of the keys are being picked up. I tried to put FORCE INDEX statements in, but MySQL keeps rejecting them (saying there is a syntax error in the query). Any ideas?
EDIT
Here’s the EXPLAIN:
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE m ALL NULL NULL NULL NULL 31 Using filesort
1 SIMPLE p ref mid,uid mid 5 db.m.id 1
1 SIMPLE po ref uid,mid uid 5 const 1
Note that p and po are being handle fine now, but m still isn’t. I just added an INDEX(date, time) for m, and still got the same.
FORCE INDEX is usually used after the table name, i.e.
But I don’t recommend using it. If you’d like it to be ordered using index, you may create compound index IDX_date_time on (m.
date, m.time).