0

I have an article table with 2 columns

Id INT(4) PK autoincrement
Description VARCHAR(250)
(and more columns)

This table contains 500.000 records and is a INNODB table. Now I want to search an article like this:

SELECT COUNT(*) FROM article (description like '%cannon%');

It takes almost a second to execute ..

What can I to to make this faster?

I have alread an index on the Description column

7
  • There is a few things that you could do, one would be to add a primary key to the table to ensure that it can be indexed correctly, and then add indexes on the table to speed up the query. Also, SELECT COUNT(*) FROM article WHERE description LIKE '%cannon%'; Commented Jan 21, 2013 at 12:12
  • 2
    index on description does not help Commented Jan 21, 2013 at 12:13
  • 3
    like '%cannon%' kills the index. Commented Jan 21, 2013 at 12:13
  • Possible duplicate: stackoverflow.com/questions/2081998/… Commented Jan 21, 2013 at 12:15
  • The best optimization possible is if you use COUNT(1) instead of COUNT(*). The %canon% negates any other kind of optimization. Commented Jan 21, 2013 at 12:15

2 Answers 2

2

You should consider adding fulltext index on description:

http://dev.mysql.com/doc/refman/5.6/en/fulltext-search.html

And then use:

SELECT 
    COUNT(*) 
FROM article 
WHERE MATCH (description) AGAINST ('cannon' WITH QUERY EXPANSION);
Sign up to request clarification or add additional context in comments.

2 Comments

the storage engine is innodb
my dear, the support of full-text search is 5.6 onwards, yet, you paste a documentation of 5.0
1

Queries with like '%cannon%' are very hard to optimize. No, indexes can't help you. Maybe full-text search can help you.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.