我正在使用此查询对 MySQL 数据库执行全文搜索:
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
结果非常相关,但是搜索真的很慢,而且随着表的增长越来越慢。
表统计:
问题 - 400 条记录
索引
- 主 B 树 - id
- BTREE - 唯一代码
- BTREE - 问题日期
- BTREE - 用户 ID
- 全文 - 问题文本
- 全文 - 唯一代码
答案 - 3,635 条记录
索引
- 主要 - BTREE - id
- BTREE - 回答日期
- BTREE - questionid
- 全文 - 回答文本
answer_mapping - 4,228 条记录
索引
- 主要 - BTREE - id
- BTREE - answerid
- BTREE - questionid
- BTREE - 用户 ID
标签 - 1,847 条记录
索引
- 主要 - BTREE - id
- BTREE - 标签
- 全文 - 标签
标签映射 - 3,389 条记录
索引
- 主要 - BTREE - id
- BTREE - tagid
- BTREE - questionid
无论出于何种原因,当我删除 tagmapping 和 tags JOINS 时,搜索速度会大大加快。
关于如何加快查询速度,您有什么建议吗?
提前致谢!
最佳答案
您也可以尝试运行
优化表问题
它有助于加快我正在处理的项目中的类似查询。
参见引用资料:https://dev.mysql.com/doc/refman/5.7/en/fulltext-fine-tuning.html
关于php - 缓慢的 MySQL 全文搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3847996/