提高MySQL全文搜索查询的性能

蝶形

我有以下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行,大约需要200ms的时间使用265,000行,大约需要500ms的时间

在性能测试(约80个并发用户)下,它显示了约18秒的平均延迟。

有什么办法可以改善此查询的性能吗?

说明输出:

在此处输入图片说明

更新

我们使用添加了一个新的镜像MyISAM表post_iddescriptionposts通过触发器将其与同步现在,在这个新的MyISAM表上进行全文搜索的时间约为400ms(在相同的性能负载下,InnoDB显示的性能为〜18sec ..这是巨大的性能提升)看起来MyISAM在MySQL中用于全文搜索的速度要比InnoDB快得多。你能解释一下吗?

MySQL Profiler结果:

AWS RDS db.t2.small实例上测试

原始的InnoDBposts表:

在此处输入图片说明

带有post_id的MyISAM镜像表,仅描述:

在此处输入图片说明

比约恩

这里有一些技巧,可以帮助您最大程度地提高使用InnoDB进行此类查询的速度:

  1. 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.

  2. 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.

  3. 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.

  4. 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.

I don't think you cannot get that much faster looking only at the query itself, maybe try removing the ORDER BY part to avoid unnecessary sorting. To dig deeper into this, maybe profile the query using MySQLs inbuild profiler.

Other than that, you might look into the configuration of your MySQL server. Have a look at this chapter of the MySQL manual, it contains some good informations on how to tune the fulltext index to your needs.

如果您已经最大限度地利用了MySQL服务器配置的功能,则可以考虑研究硬件本身-有时甚至是成本降低的解决方案,例如将表移动到另一个更快的硬盘驱动器,也可以发挥作用。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章