我有以下 MySQL 查询:
SELECT p.*, MATCH (p.description) AGAINST ('random text that you can use in sample web pages or typography samples') AS score
FROM posts p
WHERE p.post_id <> 23
AND MATCH (p.description) AGAINST ('random text that you can use in sample web pages or typography samples') > 0
ORDER BY score DESC LIMIT 1
对于 108,000 行,它需要 ~200 毫秒。对于 265,000 行,它需要 ~500 毫秒。
在性能测试(约 80 个并发用户)下,它显示 ~18 秒 平均延迟。
有什么方法可以提高这个查询的性能吗?
解释输出:
已更新
我们添加了一个新的镜像 MyISAM 表,其中包含 post_id
、description
,并通过触发器将其与 posts
表同步。现在,在这个新的 MyISAM 表上进行全文搜索 ~400ms(与 InnoDB 显示的性能负载相同 ~18sec.. 这是一个巨大的性能提升)看起来 MyISAM 是MySQL 中的全文比 InnoDB 快得多。你能解释一下吗?
MySQL 探查器结果:
在 AWS RDS db.t2.small 实例上测试
原始 InnoDB posts
表:
带 post_id 的 MyISAM 镜像表,仅描述:
最佳答案
Here为了最大限度地提高 InnoDB 此类查询的速度,需要寻找一些提示:
Avoid redundant sorting. Since InnoDB already sorted the result according to ranking. MySQL Query Processing layer does not need to sort to get top matching results.
Avoid row by row fetching to get the matching count. InnoDB provides all the matching records. All those not in the result list should all have ranking of 0, and no need to be retrieved. And InnoDB has a count of total matching records on hand. No need to recount.
Covered index scan. InnoDB results always contains the matching records' Document ID and their ranking. So if only the Document ID and ranking is needed, there is no need to go to user table to fetch the record itself.
Narrow the search result early, reduce the user table access. If the user wants to get top N matching records, we do not need to fetch all matching records from user table. We should be able to first select TOP N matching DOC IDs, and then only fetch corresponding records with these Doc IDs.
我认为仅查看查询本身并不能更快,也许可以尝试删除 ORDER BY
部分以避免不必要的排序。要深入研究,可以使用 MySQLs inbuild profiler 分析查询。 .
除此之外,您可能会查看 MySQL 服务器的配置。看看this chapter of the MySQL manual ,它包含一些有关如何根据您的需要调整全文索引的有用信息。
如果您已经最大限度地发挥了 MySQL 服务器配置的能力,那么请考虑查看硬件本身 - 有时甚至是一种损失成本的解决方案,例如将表移动到另一个更快的硬盘驱动器,也能产生奇迹。
关于mysql - 提高 MySQL 全文搜索查询的性能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31182295/