Search code examples
mysqlsearchsearch-enginelarge-data

Best way to search for partial words in large MySQL dataset


I've looked for this question on stackoverflow, but didn't found a really good answer for it.

I have a MySQL database with a few tables with information about a specific product. When end users use the search function in my application, it should search for all the tables, in specific columns.

Because the joins and many where clauses where not performing really well, I created a stored procedure, which splits all the single words in these tables and columns up, and inserts them in the table. It's a combination of 'word' and 'productID'. This table contains now over 3.3 million records.

At the moment, I can search pretty quick if I match on the whole word, or the beginning of the word (LIKE 'searchterm%'). This is obvious, because it uses an index right now.

However, my client want to search on partial words (LIKE '%searchterm%'). This isn't performing at all. Also FULLTEXT search isn't option, because it can only search for the beginning of a word, with a wildcard after it.

So what is the best practice for a search function like this?


Solution

  • MySQL is not well tailored for text search. Use other software to do that. For example use Sphinx to index data for text search. It will do a great job and is very simple to set up. If you user MySQL 5.1 you could use sphinx as an engine.

    There are other servers for performing text search better than Spinx, but they are eather not free or require other software installed.

    You can read more about: ElasticSearch, Sphinx, Lucene, Solr, Xapian. Which fits for which usage?