MySQL - 如何确定要索引的列?

时间:2014-02-26 01:23:34

标签: mysql indexing

如何确定要索引的列?

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

2 个答案:

答案 0 :(得分: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

上创建一个索引

没有桌子结构就很难说。

答案 1 :(得分:0)

阅读indexes and covering index

根据您所拥有的内容,从where子句中的内容开始,将条件加入另一个表。此外,如果可能且实用,则在完成查询时,按顺序使用/按顺序排列的那些列通常是杀手。

那就是说,我会在你的OrderPOItems表上有一个索引

(ordpo_number,orderpoitem_ModelNumber,ord_poordid_list,ord_id)

这样,FIRST元素就会命中你的WHERE子句。接下来是数据分组的列,最后是您的订单列。这样,连接和限定组件可以单独从索引“覆盖”,而不必转到返回的其余列的原始数据页。希望你的场景特别适合跳跃。