查询不使用索引

时间:2013-03-05 09:01:17

标签: mysql

任何人都可以告诉我为什么这个查询没有使用索引idx_accessorieslinkid 我创建了索引:

    CREATE INDEX idx_accessorieslinkid ON `accessorieslink` (actionid);

我是索引的新手,并试图了解它们的工作原理(以及有时为什么不这样做!)

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 

解释结果

    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 

1 个答案:

答案 0 :(得分:1)

派生表不能在物理表上使用索引。

尝试以下查询

SELECT  SUM(accprice) AS accprice, accessorieslink.actionid FROM accessorieslink 
LEFT JOIN  `action`  ON accessorieslink.actionid = action.actionid 
GROUP BY accessorieslink.actionid