MySQL MATCH() AGAINST() 与 REGEXP 匹配整个单词

MySQL MATCH() AGAINST() vs. REGEXP for matching whole words

我正在尝试优化我的字典中的搜索(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解决方案不能使用索引。它总是强制执行 table 扫描并使用正则表达式测试每一行。随着 table 的增长,执行与行数成线性比例的 REGEXP 查询将需要更长的时间。

几年前我做了一个演讲 Full Text Search Throwdown,其中我将全文索引方法与 LIKEREGEXP 进行了比较。对于 740 万行的样本数据,REGEXP 花费了 7 分 57 秒,而在布尔模式下搜索 InnoDB FULLTEXT 索引花费了 350 毫秒——MATCH/AGAINST 查询快了 1,363 倍。

行数越多,差异就越大。