MySQL | REGEXP VS Like

本秂侑毒 提交于 2019-11-28 11:53:47

REGEXP and LIKE are used to totally different cases.

LIKE is used to add wildcards to a string whereas REGEXP is used to match an attribute with Regular Expressions.

In your case a firstname is more likely to be matched using LIKE than REGEXP and hence, it will be more optimized.

If you can use LIKE instead of REGEXP, use LIKE

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

Also LIKE is much faster than REGEXP.

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:

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

Query took 10.0418 seconds.

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

Query took 11.0742 seconds.

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!