简体   繁体   中英

How can I replace/speed up a text search query which uses LIKE?

I'm trying to speed up my query...

select PadID from Pads WHERE (keywords like '%$search%' or 
ProgramName like '%$search%' or English45 like '%$search%') AND 
RemovemeDate = '2001-01-01 00:00:00' ORDER BY VersionAddDate DESC

I've done some work already, I have a keywords table so I can add

... PadID IN (SELECT PadID FROM Keywords WHERE word = '$search') ...

However its going to be a nightmare to split up the words from English45 and ProgramName into a word table.

Any ideas ? EDIT : (also provided actual table names)

替代文字

替代文字

替代文字

Have you tried FULLTEXT indexing? Here is a good article about it:

http://devzone.zend.com/article/1304

Also, look into using SOUNDEX functions, that might help, but it might not.

Jules,

You need to provide output of explain on the query. Something like:

mysql> EXPLAIN select id from mytable WHERE (keywords like '%$search%' or ProgramName like '%$search%' or English45 like '%$search%') AND RemovemeDate = '2001-01-01 00:00:00' ORDER BY VersionAddDate DESC

And if possible table structure by doing a DESC on the table. This will help us to identify your index structure and if they are being used in the query.

Thanks.

Jules,

There are few things to note based on the EXPLAIN output:

  1. WHERE RemoveMeDate is constraining the clause.
  2. ORDER BY is doing a 2nd pass over the data to sort it. Note that there is no index on VersionAddDate.

Choice of index depends on all other queries you are using, but for this particular query i'd like to add an index for VersionAddDate. However, since MySQL can use only single index for a query, the index would look like:

INDEX `removemedate` (`RemoveMeDate` ASC, `VersionAddDate` ASC)

After doing this EXPLAIN should read something like:

+----+-------------+-------+------+---------------+--------------+---------+--------------+-------------+
| id | select_type | table | type | possible_keys | key          | key_len | ref   | rows | Extra       |
+----+-------------+-------+------+---------------+--------------+---------+-------+------+-------------+
|  1 | SIMPLE      | pads  | ref  | removemedate  | removemedate | 4       | const |    1 | Using where |
+----+-------------+-------+------+---------------+--------------+---------+-------+------+-------------+

Note that filesort under Extra is gone. This should give better performance than your previous query.

Thanks.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM