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
MySQL | REGEXP VS Like
提问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
REGEXP
and LIKE
are used to totally different cases.
REGEXP
并且LIKE
习惯于完全不同的情况。
LIKE
is used to add wildcards to a string whereas REGEXP
is used to match an attribute with Regular Expressions.
LIKE
用于向字符串添加通配符,而REGEXP
用于将属性与正则表达式匹配。
In your case a firstname
is more likely to be matched using LIKE
than REGEXP
and hence, it will be more optimized.
在你的情况下,firstname
更容易使用匹配LIKE
比REGEXP
,因此,它会更加优化。
回答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.13
and compared LIKE
vs REGEXP
on 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 秒。
LIKE
performance 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 快得多。