Can anyone tell me why this query is not using index idx_accessorieslinkid I have created index:
CREATE INDEX idx_accessorieslinkid ON `accessorieslink` (actionid);
I am new to indexes and trying to understand how they work (and why sometimes they don't!)
sql
SELECT action.actionid
FROM
ACTION
LEFT JOIN
(SELECT
SUM(accprice) AS accprice,
actionid
FROM
accessorieslink
GROUP BY actionid) AS accprice
ON accprice.actionid = action.actionid
WHERE action.typeid = '2'
AND actiondate2 BETWEEN '20130301' AND '20130301'
ORDER BY actiondate2 DESC
explain results
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY action index_merge idx_actiontypeid,idx_actiondate idx_actiondate,idx_actiontypeid 4,5 141 Using intersect(idx_actiondate,idx_actiontypeid); Using where; Using index; Using temporary; Using filesort
1 PRIMARY <derived2> ALL 7804
2 DERIVED accessorieslink ALL 12175 Using temporary; Using filesort
The following recommendations will help you in your SQL tuning process.
You'll find 3 sections below:
SELECT
action.actionid
FROM
ACTION
LEFT JOIN
(
SELECT
SUM(accprice) AS accprice,
accessorieslink.actionid
FROM
accessorieslink
GROUP BY
accessorieslink.actionid
ORDER BY
NULL
) AS accprice
ON accprice.actionid = action.actionid
WHERE
action.typeid = '2'
AND actiondate2 BETWEEN '20130301' AND '20130301'
ORDER BY
actiondate2 DESC