I'm developing a forum/guestbook thingy and I'm having some problems getting MySQL to use the appropriate indices in the following query:
SELECT t.*, SUM(MATCH(message) AGAINST('test')) AS score
FROM topics AS t
INNER JOIN posts AS p
ON p.topicid = t.id
WHERE t.ghost IS NULL AND MATCH (message) AGAINST ('test') GROUP BY t.id
ORDER BY score DESC
LIMIT 0, 35
The indices are
topics: PRIMARY( id )
posts: PRIMARY( id ), KEY( topicid ), FULLTEXT( message )
I guess the best thing would be for it to use the fulltext index in the posts table (message) for "the search" and then the topicid index in topics table for the join. It doesn't appear to be doing that though. The query takes forever.
EXPLAIN gives me this:
+-------+--------+---------------+---------+---------+-----------+--------+---------------------------------------------+
| table | type | possible_keys | key | key_len | ref | rows | Extra |
+-------+--------+---------------+---------+---------+-----------+--------+---------------------------------------------+
| p | ALL | topicid | NULL | NULL | NULL | 201455 | where used; Using temporary; Using filesort |
| t | eq_ref | PRIMARY | PRIMARY | 4 | p.topicid | 1 | where used |
+-------+--------+---------------+---------+---------+-----------+--------+---------------------------------------------+
Does anyone have any idea?
/Kennel