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

标签 mysql regex match-against

我正在尝试优化字典中的搜索(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几年前,我将全文索引方法与 LIKEREGEXP 进行了比较。对于 740 万行的样本数据,REGEXP 花费了 7 分 57 秒,而以 bool 模式搜索 InnoDB FULLTEXT 索引花费了 350 毫秒 - MATCH/AGAINST 查询速度提高了 1,363 倍。

行数越多,差异就越大。

关于MySQL MATCH() AGAINST() 与 REGEXP 匹配整个单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54971966/

相关文章:

mysql匹配不返回不区分大小写的结果

mysql - 使表单安全地提交变量

php - Mysql join 或 php in_array() 哪个更适合优化 Mysql 查询?

mysql - 在 MySQL 中计算时间跨度

c - Posix 正则表达式非捕获组

MySQL 匹配和喜欢

mysql - 无法使表列从预设值自动递增

c# - 正则表达式匹配逗号分隔的字符串,行尾没有逗号

Python,正则表达式 : non-greedy not working when overlapping?

mysql - 使用 MYSQL 生成分数时如何检查所有连接