Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

improve performance for LIKE clause

Tags:

sql

mysql

This has been a pain on my head for the past few days. I created the database before without any knowledge about the performance of LIKE. The query I used is like this,

SELECT  ....
FROM    .... JOINS ....
WHERE   tableA.col1 LIKE '%keyword%' OR
        tableB.col2 LIKE '%keyword%' OR
        tableC.col2 LIKE '%keyword%' OR
        .....

When I tested the query, it was very fast because there was only around 100-150 records on it. I wanted to search for any string which contains the keyword. As months have past, the database grew huge containing 50,000 records. And this time, I already experiencing the low performance of the query. It was extremely low.

Any suggestions how can I improve it? I can't alter the database because it has already been used by the corporation already.

By the way, my tables were all INNODB.

like image 688
SkyDrive Avatar asked Oct 29 '12 00:10

SkyDrive


People also ask

Does with Clause improve performance?

The Oracle "with" clause will help performance in situations where your query contains several identical sub queries. Instead of re-computing the repeating sub queries, it will query or aggregate once, assign a name to the resulting data and refer to it.

What is the purpose of the LIKE clause?

The LIKE operator is used in a WHERE clause to search for a specified pattern in a column. There are two wildcards often used in conjunction with the LIKE operator: The percent sign (%) represents zero, one, or multiple characters.

How do you improve the performance of a update statement?

Best practices to improve SQL update statement performance We need to consider the lock escalation mode of the modified table to minimize the usage of too many resources. Analyzing the execution plan may help to resolve performance bottlenecks of the update query. We can remove the redundant indexes on the table.


2 Answers

This type of search is call Full Text Search, and you really need to use specialized systems for it instead of forcing the database to constantly do table scans. You essnetially hand off all the text you want searched to a search engine, which then indexes it for quick search.

One option would be Apache Lucene.

like image 158
Chris Pitman Avatar answered Oct 18 '22 01:10

Chris Pitman


Using a wildcard prefix '%abc' will very likely stop any indexes being used.

No index = full table scan = (usually) slow...

Btw, 50,000 records is not huge; it is tiny.

Have you considered using MySql's Full-Text Search Functions? (requires MyISAM tables)

like image 22
Mitch Wheat Avatar answered Oct 18 '22 01:10

Mitch Wheat