开发者

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

开发者 https://www.devze.com 2023-01-31 06:50 出处:网络
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

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开发者_Python百科 ? EDIT : (also provided actual table names)

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

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

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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号