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

时间:2019-03-03 18:00:50

标签: mysql regex match-against

我正在尝试优化字典中的搜索(109,000个条目,MyISAM,FULLTEXT),现在我在比较MATCH() AGAINST()REGEXP '[[:<:]]keyword1[[:>:]]' AND table.field REGEXP '[[:<:]]keyword2[[:>:]]'的性能。

使用两个关键字,对于MATCH() AGAINST()查询,我(在PhpMyAdmin内部)获得 0.0000秒 0.0010秒,而 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

1 个答案:

答案 0 :(得分:0)

MATCH/AGAINST解决方案使用FULLTEXT索引,并且非常有效地搜索索引。

REGEXP解决方案不能使用索引。它总是强制进行表扫描并使用正则表达式测试每一行。随着表的增长,与行数成线性比例的REGEXP查询将花费更长的时间。

几年前,我做了一个演示文稿Full Text Search Throwdown,在其中我将全文索引方法与LIKEREGEXP进行了比较。凭借740万行的样本数据,REGEXP花费了7分57秒,而以布尔模式搜索InnoDB FULLTEXT索引则花费了350毫秒-MATCH/AGAINST查询的速度提高了1,363倍。

行数越多,差异越大。