2014-02-26 44 views
0

如何確定要索引哪些列?MySQL - 如何確定要索引的列?

SELECT a.ORD_ID AS Manual_Added_Orders, 
    a.ORD_poOrdID_List AS Auto_Added_Orders, 
    a.ORDPOITEM_ModelNumber, 
    a.ORDPO_Number, 
    a.ORDPOITEM_ID, 
    (SELECT sum(ORDPOITEM_Qty) AS ORDPOITEM_Qty 
     FROM orderpoitems 
    WHERE  ORDPOITEM_ModelNumber = a.ORDPOITEM_ModelNumber 
      AND ORDPO_Number = 123007) 
     AS ORDPOITEM_Qty, 
    a.ORDPO_TrackingNumber, 
    a.ORDPOITEM_Received, 
    a.ORDPOITEM_ReceivedQty, 
    a.ORDPOITEM_ReceivedBy, 
    b.ORDPO_ID 
FROM orderpoitems a 
    LEFT JOIN orderpo b ON (a.ORDPO_Number = b.ORDPO_Number) 
WHERE a.ORDPO_Number = 123007 
GROUP BY a.ORDPOITEM_ModelNumber 
ORDER BY a.ORD_poOrdID_List, a.ORD_ID 

enter image description here

我的解釋是我如何得到這些照片。我增加了一些指標......仍然沒有看起來很不錯。

enter image description here

+0

看看mysql中的「EXPLAIN」特性。它有助於理解查詢如何優化我的引擎。 – arkascha

回答

1

那麼首先你的查詢可以簡化爲:

SELECT a.ORD_ID AS Manual_Added_Orders, 
a.ORD_poOrdID_List AS Auto_Added_Orders, 
a.ORDPOITEM_ModelNumber, 
a.ORDPO_Number, 
a.ORDPOITEM_ID, 
SUM(ORDPOITEM_Qty) AS ORDPOITEM_Qty 
a.ORDPO_TrackingNumber, 
a.ORDPOITEM_Received, 
a.ORDPOITEM_ReceivedQty, 
a.ORDPOITEM_ReceivedBy, 
b.ORDPO_ID 
FROM orderpoitems a 
LEFT JOIN orderpo b ON (a.ORDPO_Number = b.ORDPO_Number) 
WHERE a.ORDPO_Number = 123007 
GROUP BY a.ORDPOITEM_ModelNumber 
ORDER BY a.ORD_poOrdID_List, a.ORD_ID 

其次我會通過對orderpoitems.ORDPO_Numberorderpo.ORDPO_number

位很難創建一個索引,而不表的結構說開始。

0

indexes and covering index

從你有什麼瞭解時,先從什麼是你WHE​​RE子句和聯接標準到另一個表。此外,如果可能和實際包括,按順序排序的那些列在完成查詢時通常是一個殺手。

這就是說,我會對

(ordpo_number,orderpoitem_ModelNumber,ord_poordid_list,ord_id)

這樣,第一個元素擊中了你的WHERE子句您OrderPOItems表的索引。接下來是數據分組列,最後是您的訂單列。通過這種方式,可以從索引中「覆蓋」連接和限定組件,而無需轉到返回的其餘列的原始數據頁面。希望有一個很好的開始,針對你的場景。