MySQL | REGEXP VS 喜欢

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16646686/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 17:41:31  来源:igfitidea点击:

MySQL | REGEXP VS Like

mysqlregexdatabase-performance

提问by Vardan Gupta

I have a table CANDIDATE in my db which is running under MySQL 5.5 and I am trying to get rows from table where RAM is contains in firstname, so I can run below two queries, but I would like to now which query we should use for long term with respect to optimization.

我的数据库中有一个表 CANDIDATE,它在 MySQL 5.5 下运行,我试图从 RAM 包含在名字中的表中获取行,所以我可以在两个查询下运行,但我现在想知道我们应该使用哪个查询长期来看优化。

SELECT * FROM CANDIDATE c WHERE firstname REGEXP 'ram';
SELECT * FROM CANDIDATE c WHERE firstname LIKE'%ram%';

回答by draxxxeus

REGEXPand LIKEare used to totally different cases.

REGEXP并且LIKE习惯于完全不同的情况。

LIKEis used to add wildcards to a string whereas REGEXPis used to match an attribute with Regular Expressions.

LIKE用于向字符串添加通配符,而REGEXP用于将属性与正则表达式匹配。

In your case a firstnameis more likely to be matched using LIKEthan REGEXPand hence, it will be more optimized.

在你的情况下,firstname更容易使用匹配LIKEREGEXP,因此,它会更加优化。

回答by Casimir et Hippolyte

If you can use LIKE instead of REGEXP, use LIKE

如果您可以使用 LIKE 而不是 REGEXP,请使用 LIKE

回答by Joel Karunungan

I've tried it out on MySQL 8.0.13and compared LIKEvs REGEXPon a table with 1M+ rows on a column with an index:

我已经尝试过并在带有索引的列上有 1M+ 行的表上MySQL 8.0.13进行了LIKE对比REGEXP

SELECT * FROM table WHERE column LIKE '%foobar%';

Query took 10.0418 seconds.

查询耗时 10.0418 秒。

SELECT * FROM table WHERE REGEXP_LIKE(column, 'foobar');

Query took 11.0742 seconds.

查询耗时 11.0742 秒。

LIKEperformance is faster. If you can get away with using it instead of REGEXP, do it.

LIKE性能更快。如果您可以使用它而不是REGEXP,那就去做吧。

回答by Rahul

Better Use of LIKE Query instead of REGEXP if you are not sure about value.

如果您不确定价值,请更好地使用 LIKE 查询而不是 REGEXP。

Also LIKE is much faster than REGEXP.

LIKE 也比 REGEXP 快得多。