2013-03-05 154 views
1
爲什麼這個查詢不使用索引idx_accessorieslinkid

誰能告訴我我已經創建的索引:查詢不使用索引

CREATE INDEX idx_accessorieslinkid ON `accessorieslink` (actionid); 

我是新來的索引,並試圖瞭解他們的工作(爲什麼有時他們不「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 

解釋結果

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

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

試試下面的查詢

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

嗨 - 這種解決方案沒有變化所以刪除這些子查詢 - 剛剛離開連接表和外部功能 SELECT action.actionid, SUM(accessorieslink.accprice)歸類爲accprice FROM ACTION LEFT JOIN accessorieslink ON accessorieslink.actionid = action.actionid WHERE action.typeid = '2' AND actiondate2 '之間20130301' AND '20130301' GROUP BY accessorieslink.actionid ORDER BY actiondate2 DESC – zima10101 2013-03-05 17:29:53