我正在尝试优化字典中的搜索(109,000 个条目、MyISAM、FULLTEXT),我现在正在比较 MATCH() AGAINST()
的性能与 REGEXP '[[:<:]]keyword1[[:>:]]' AND table.field REGEXP '[[:<:]]keyword2[[:>:]]'
的.
使用两个关键字,我得到(在 PhpMyAdmin 内)0.0000 秒 或 0.0010 秒 MATCH() AGAINST()
查询与正则表达式查询的 0.1962 秒 或 0.2190 秒。速度是这里唯一重要的指标吗?我应该更喜欢哪个查询(两者似乎都产生完全相同的结果)?这是显而易见的——更快吗?
以下是完整的查询:
SELECT * FROM asphodel_dictionary_unsorted
JOIN asphodel_dictionary_themes ON asphodel_dictionary_unsorted.theme_id = asphodel_dictionary_themes.theme_id
LEFT JOIN asphodel_dictionary_definitions ON asphodel_dictionary_unsorted.term_id = asphodel_dictionary_definitions.term_id
WHERE MATCH (asphodel_dictionary_unsorted.english)
AGAINST ('+boiler +pump' IN BOOLEAN MODE)
和
SELECT * FROM asphodel_dictionary_unsorted
JOIN asphodel_dictionary_themes ON asphodel_dictionary_unsorted.theme_id = asphodel_dictionary_themes.theme_id
LEFT JOIN asphodel_dictionary_definitions ON asphodel_dictionary_unsorted.term_id = asphodel_dictionary_definitions.term_id
WHERE asphodel_dictionary_unsorted.english REGEXP '[[:<:]]boiler[[:>:]]'
AND asphodel_dictionary_unsorted.english REGEXP '[[:<:]]pump[[:>:]]'
ORDER BY asphodel_dictionary_unsorted.theme_id, asphodel_dictionary_unsorted.english
最佳答案
MATCH/AGAINST
解决方案使用 FULLTEXT 索引,并且它搜索索引的效率非常高。
REGEXP
解决方案无法使用索引。它总是强制进行表扫描并使用正则表达式测试每一行。随着表的增长,执行 REGEXP
查询所需的时间将与行数成线性比例。
我做了一个演示 Full Text Search Throwdown几年前,我将全文索引方法与 LIKE
和 REGEXP
进行了比较。对于 740 万行的样本数据,REGEXP
花费了 7 分 57 秒,而以 bool 模式搜索 InnoDB FULLTEXT
索引花费了 350 毫秒 - MATCH/AGAINST
查询速度提高了 1,363 倍。
行数越多,差异就越大。
关于MySQL MATCH() AGAINST() 与 REGEXP 匹配整个单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54971966/