I need to search for some user input patterns in database.
is it efficient to use Regex in Mysql SELECT statement’s where clause ( in a scale of 10000 rows on an indexed column)?
My search is for dates and can be in these three forms :
2012that should match2012,2012/*&2012/*/*2012/10that should match2012/10&2012/10/*2012/10/3that should match only2012/10/3
Note: I have three columns that should be checked for this type of match separately.
Although I can’t find a definite reference in the MySQL documentation, due to the index structure, regex matches can not use index lookups.
They may scan indexes using the regex instead of scanning the full table, but any regex search will be more or less equivalent to a full table scan.
If your regex starts with a constant string, you may want to structure the query as;
to get some index help from the LIKE that – if matched from the start of the string – can use the index to its advantage.
EDIT: Since your selects are fairly simple and match from the start of the string, you can just use LIKE to make them work, which will utilize indexes well;
The reason to split the comparison into two is to avoid a search for
2012/1that would match2012/10with just a LIKE that covers both cases (ieLIKE '2012/1%').