MySQL,在子查询中使用索引

时间:2018-11-17 09:49:10

标签: mysql

仍在尝试学习索引的方法,外部查询中的JOIN是否不应该使用主键上的索引?索引不能与子查询一起使用吗?谢谢!

SELECT SQL_BIG_RESULT
  I.item_group_id
FROM
(
    SELECT SQL_BIG_RESULT
      MAX(ITM.id) as max_id
    FROM a_movements M
    JOIN a_items_to_movements ITM ON ITM.movement_id = M.id    -- Index used
    WHERE M.warehouse_id IN (...)                              -- Index used
    GROUP BY ITM.item_id
    ORDER BY NULL
) X
JOIN a_items_to_movements ITM ON ITM.id = X.max_id             -- Index not used
JOIN a_movements M ON M.id = ITM.movement_id
    AND M.direction = 0
    AND M.settled IS NOT NULL
JOIN a_items I ON I.id = ITM.item_id                           -- Index not used
GROUP BY I.item_group_id
ORDER BY NULL

编辑:在此处附加EXPLAIN输出:https://imgur.com/PdO3mIo

0 个答案:

没有答案