I am using MySQL.
I have several lookups like this:
SELECT * from users WHERE user_name LIKE '%search_term%'
What index do I need to make these lookups fast?
I am using MySQL.
I have several lookups like this:
SELECT * from users WHERE user_name LIKE '%search_term%'
What index do I need to make these lookups fast?
No index will help. It would be like looking in a dictionary for all words containing the substring "ab". You will still have to look at every word.
Have you looked into Full Text Search? http://dev.mysql.com/tech-resources/articles/full-text-revealed.html
If for some reason full-text search won't work for you (try it first it is the bestter solution), I had an issue like this where I searched first for the string LIKE 'search_term%' which can use the index and then only tried the longer search if the first one didn't return any records. The majority of people will search using the first letters of the search term and they can have faster returns. Tell them on the site that using the first letter of the search term will improve the performance of the search. OR even make it a requirement and avoid ever having to do the "search for these random letters I typed in" search.
We used to run these kinds of queries too, but soon realized that relational databases like MySQL aren't ideal for full-text searching. Depending on what kind of other lookups you have, you might want to look at Lucene and Solr.
Lucene supports indexing and search, completely separate from your database. It's extremely fast and also supports spell checking, stemming, relevancy, and wildcard searches.
Solr is a search server powered by Lucene and also supports MySQL database integration. Highly recommended.
Having a wildcard on both ends of the string, no index will make that faster. It will have to do a full table scan. However, if the wildcard string is only the end, e.g. 'term%', then an index will be used.
As others pointed out, fulltext index may work for you as well. However, it only supports "full word" searches, and (by default) only words with 4 or more characters.