Hi guys, I'm using this query to perform a full text search on a MySQL database:
SELECT DISTINCT
questions.id,
questions.uniquecode,
questions.spam,
questions.questiondate,
questions.userid,
questions.description,
users.login AS username,
questions.questiontext,
questions.totalvotes,
MATCH(questions.questiontext, questions.uniquecode)
AGAINST ('rock guitarist chick*' IN BOOLEAN MODE) AS relevance
FROM questions
LEFT JOIN users ON questions.userid = users.id
LEFT JOIN answer_mapping ON questions.id = answer_mapping.questionid
LEFT JOIN answers ON answer_mapping.answerid = answers.id
LEFT JOIN tagmapping ON questions.id = tagmapping.questionid
LEFT JOIN tags ON tagmapping.tagid = tags.id
WHERE questions.spam < 10
AND
(
MATCH(questions.questiontext, questions.uniquecode)
AGAINST ('rock guitarist chick*' IN BOOLEAN MODE)
OR MATCH(answers.answertext) AGAINST ('rock guitarist chick*' IN BOOLEAN MODE)
OR MATCH (tags.tag) AGAINST ('rock guitarist chick*' IN BOOLEAN MODE)
) GROUP BY questions.id ORDER BY relevance DESC
The results are very relevant, but the search is really slow and is getting slower and slower as the tables grow.
Table stats:
questions - 400 records
indexes
- PRIMARY BTREE - id
- BTREE - uniquecode
- BTREE - questiondate
- BTREE - userid
- FULLTEXT - questiontext
- FULLTEXT - uniquecode
answers - 3,635 records
indexes
- PRIMARY - BTREE - id
- BTREE - answerdate
- BTREE - questionid
- FULLTEXT - answertext
answer_mapping - 4,228 records
indexes
- PRIMARY - BTREE - id
- BTREE - answerid
- BTREE - questionid
- BTREE - userid
tags - 1,847 records
indexes
- PRIMARY - BTREE - id
- BTREE - tag
- FULLTEXT - tag
tagmapping - 3,389 records
indexes
- PRIMARY - BTREE - id
- BTREE - tagid
- BTREE - questionid
For whatever reason when I remove the tagmapping and tags JOINS the search speeds up considerably.
Do you have any tips on how to speed this query up?
Thanks in advance!