33

Rumour has it that this:

SELECT * FROM lineage_string where lineage like '%179%' and  lineage regexp '(^|/)179(/|$)'

Would be faster than this:

SELECT * FROM lineage_string where lineage regexp '(^|/)179(/|$)'

Can anyone confirm? Or know a decent way to test the speed of such queries. Thanks

4
  • According to this poorly supported blog post, LIKE was 10x faster than REGEXP (data may vary, no word on index use, etc): thingsilearn.wordpress.com/2008/02/28/… Commented Apr 29, 2010 at 19:14
  • Additional link - same story, LIKE is faster: lists.mysql.com/mysql/101728 Commented Apr 29, 2010 at 19:16
  • It was definitely faster for TEXT and VARCHAR fields I tried it on. I remember having read in a blog comment that REGEXP is faster than LIKE for UNSIGNED fields, but I can't confirm that. Commented Aug 16, 2012 at 3:24
  • Does anyone know how full search (supported from MySQL 5.6.? in InnoDB I think) could help out here and how is the performance compared to LIKE and regexp? Commented Jan 11, 2013 at 17:39

2 Answers 2

36

It is possible that it could be faster because the LIKE condition can be evaluated more quickly then the regular expression so if most rows fail the test it could be faster. However it will be slower if most rows succeed as two tests must be run for successful rows instead of just one. It also depends on which expression the optimizer chooses to run first.

An even bigger speedup can be witnessed if you have something like this:

SELECT * FROM (
   SELECT * FROM lineage_string
   WHERE lineage LIKE '179%'
) WHERE lineage regexp '^179(/|$)'

Now an index can be used to find likely rows because LIKE '179%' is sargable. Many rows won't need to be checked at all.

As always the best way to be sure is to measure it for yourself on your actual data.

Sign up to request clarification or add additional context in comments.

1 Comment

The example you give assumes that the 179 is at the start of the string, but that's not what OP asked.
16

Yeah, it probably would be a tiny bit faster because standard-SQL LIKE is a simpler comparison operation than a full-on regex parser.

However, in real terms both are really slow, because neither can use indices. (LIKE can use an index if the match string doesn't start with a wildcard, but that's not the case here.)

If you are concerned about speed, you should change your schema so that you can put the 179 directly in a column and index it, rather than having to check through a string manually on every row.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.