php - 缓慢的 MySQL 全文搜索

标签 php mysql full-text-search full-text-indexing

我正在使用此查询对 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

无论出于何种原因,当我删除 tagmappingtags JOINS 时,搜索速度会大大加快。

关于如何加快查询速度,您有什么建议吗?

提前致谢!

最佳答案

您也可以尝试运行 优化表问题

它有助于加快我正在处理的项目中的类似查询。

参见引用资料:https://dev.mysql.com/doc/refman/5.7/en/fulltext-fine-tuning.html

关于php - 缓慢的 MySQL 全文搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3847996/

相关文章:

mysql - 关于um​​l设计的意见

mysql - 使用 sum select 连接 mySQL 表

php - 从正则表达式得到错误 'nothing to repeat at offset 1'

javascript - 错误/bug PHP SIMPLE HTML DOM 解析器

PHP mysqli 打印数组问题

php - 在 CodeIgniter 中绕过 Controller 的方法?

javascript - d3从mysql数据库导入数据返回 "undefined"

php - 我无法访问 Laravel 5 中的原始 PDO 实例

iphone - Apple 核心数据框架中的全文搜索

sql - LIKE '%' 的全文搜索等效项是什么?