I have a table of 2+ million (rows) products with 44 fields (columns).
I am attempting to query this table based on the ‘NAME’ field (varchar 160) which I have a fulltext index on.
Here is the query that is currrently taking 71.34 seconds to execute with a three word $keyword,
62.47 seconds with two word $keyword and 0.017 seconds to execute with a single word $keyword.
SELECT ID,
MATCH(NAME) AGAINST ('$keyword') as Relevance,
MANUFACTURER,
ADVERTISERCATEGORY,
THIRDPARTYCATEGORY,
DESCRIPTION,
AID,
SALEPRICE,
RETAILPRICE,
PRICE,
SKU,
BUYURL,
IMAGEURL,
NAME,
PROGRAMNAME
FROM products
WHERE MATCH(NAME) AGAINST ('$keyword' IN BOOLEAN MODE)
GROUP BY NAME
HAVING Relevance > 6
ORDER BY Relevance DESC LIMIT 24
How can I optimize this query to perform better on 2+ word $keyword queries?
This may not be quite the answer you were looking for, however:
With a table that large, fulltext searches are never going to be speedy. I would suggest looking into a fulltext engine like Sphinx.
As an added benefit, you can also do the relevancy matching in Sphinx, as it will return results ranked in order of relevance. Once Sphinx returns matching IDs, you can then just an IN statement in your query’s WHERE clause and select the other data you need.
I would also suggest looking into Sphinx’s Extended Query syntax, as this will let you match multiple words across things like proximity and word order.