I've got an sql table with over 1 million rows, queries can take anything from 0 to 10 seconds. I want to reduce this time. I've tried using indexes but with minimal success as I have no idea what i'm doing. The mysql official site is just too confusing for me. All I am querying is one column only like "SELECT * FROM table WHERE title LIKE '%free%' AND title LIKE '%antivirus%'" Can anyone recommend some things to do? Please don't just reply telling me to use indexes as I have no clue what they do and what I really need to put in the size field. Thx all
I have worked with large databases and tables with >1 billion rows and multiple indexes. I can optimize your table for $100.
I think the issue you're running into is that MySQL can't use an index given the way you're searching. Your search logic says that you want all rows that contain 'free' or 'antivirus'. MySQL can't use the index b/c the LIKE values start with a wildcard. In order for an index to be used, the search logic would have to be like this.. SELECT * FROM table WHERE title LIKE 'free%' ..... Of course, that logic is different b/c it's only asking for rows where title begins with 'free' instead of rows where title contains 'free'. Have you considered using full-text search? Here's a resource that might be helpful: http://dev.mysql.com/doc/refman/5.0/en/fulltext-search.html
Hi Symmetric, thanks very much for your reply. Full text search looks like the answer i've been looking for, I'll give it a go in a few hours when I get back. Thanks for your time!
Symmetric is right. Indexing works with sub-parts of a column, as per your index spec. To surround a term with % renders any index useless. Here's a little helper so you can predict how well a query will perform: explain select.... ie Just type "explain" before your query and the result will tell you how many rows will be searched, what indexes (if any) will be used, whether the search would require a filesort, use of a temporary file, etc. If you can work a way to avoid surrounding terms with %, you can also further optimize query execution times by teaking your server config and also pre-loading your indexes assuming you've got the available RAM for it.