MySQL | REGEXP VS Like

陌路散爱 提交于 2019-11-27 03:52:27

问题


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.

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

回答1:


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.




回答2:


If you can use LIKE instead of REGEXP, use LIKE




回答3:


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

Also LIKE is much faster than REGEXP.




回答4:


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.



来源:https://stackoverflow.com/questions/16646686/mysql-regexp-vs-like

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