I have two tables. The first one contains all the item informations and the second contains the item ID with a category ID. The reason of that is because an item can be in more than one category. I have about 500 000 items in my table.
Here is a select query exemple:
SELECT SQL_CALC_FOUND_ROWS items.*
FROM items
INNER JOIN cat
ON items.iid=cat.iid
WHERE (items.expire>'1308061323' AND cat.cid = '1')
AND (items.code=71 OR items.code=23)
ORDER BY price DESC
LIMIT 0, 50
- iid = item ID
- cid = category ID
- code = A code I use for search only.
- expire = The expiration time for an item
I use SQL_CALC_FOUND_ROWS because I want to display the total matching results. I display only 50 items on the page (LIMIT 0, 50).
When I execute that query, my php page take about 5 seconds to load (less than 1 without the query).
- Is there a way to optimize it?
- Is it faster to use SQL_CALC_FOUND_ROWS or a second query with SELECT COUNT(*)?
- I heard about indexes, but I don't know how to use them and what they do. Can they help?