Develop

Small Business Websites

Creating Websites for Small Businesses & Sole Traders

Promote

Affiliate Marketing

Promotion of merchant products and services in an ethical manor

Support

Computer Services

Providing PC services in our local area, OS installs problems upgrades and networking

Fast MySql REGEXP LIKE Search Solution

Recently had the job of improving the speed of a clients PHP script MySql query which used REGEXP (RLIKE)

The table was large >50k rows and negative matches needed to be used, the original query was not written using FullText Match Against Boolean due to the over 50% threshold limit and the Stopwords would cause poor results.

As we know LIKE with wildcards are pretty fast but no use for exact word searches, example %build% would find builder and building as well as build, so the query was written as REGEXP [[:<:]]build[[:>:]] which matches End Of Words.

Most queries took >6 seconds which was simply pathetic, so how do we speed this up?

Simply use LIKE  AND REGEXP, this way we only perform the REGEXP on the LIKE’ results

0.244 Seconds

And Now

0.285 Seconds

REGEXP results at LIKE Speeds.

Let me know if this works for you.

3 comments below, add yours

how it works?! some kind of magic?))

Nurziya on May 21, 2009

Hi,

Looks good, although I think in your specific example the

AND field NOT LIKE ‘%material%’

portion will exclude some results that you wouldn’t necessarily want to exclude. e.g. if field was “build materialx” it would be excluded in your optimized but not in the original regex version.

mykel on July 29, 2009

Hi there,

It looks like the initial LIKE is doing the bulk of the filtering – could this be where the performance gains are coming from?

From what I know, MySQL will optimize the query before running it and will get the first result set using the LIKE and then use the 2nd and 3rd REGEXP’s to filter the result set further before returning it (but I could be wrong)

Paul

Paul Bennett on November 24, 2009
* means required