MySQL奇怪的EXPLAIN PLAN结果

时间:2016-03-15 22:31:52

标签: mysql sql-execution-plan

我正在尝试调整存储过程内部的查询,该存储过程在第一次调用存储过程时运行正常,但在后续调用中慢慢爬行。

我已经对一些原始查询进行了评论,所以如果看起来有不必要的嵌套,请原谅我:

EXPLAIN EXTENDED SELECT
  glm.store_id,
  glm.account_balance
FROM 
(
  SELECT
    m.store_id,
    m.account_number,
    ROUND(COALESCE(SUM(gl.account_balance), 0.00), 2) AS account_balance
  FROM
    account_map m
  LEFT JOIN (
    select
      store_id,
      account_number_dms,
      sum(account_balance) AS account_balance
    FROM
      general_ledger
    WHERE
      account_date >= startDate
    AND
      account_date < endDate
    GROUP BY
      store_id, account_number_dms
  ) AS gl
    ON gl.store_id = m.store_id
    AND gl.account_number_dms = m.account_number_dms
  WHERE
    m.map_type = mapType
  AND m.store_id IN (
    SELECT store_id
    FROM store
    WHERE FIND_IN_SET(store_id, storeIdList)
  )
  GROUP BY
    m.store_id,
    m.account_number
) AS glm;

我第一次打电话给这个时,我得到了一个合理的EXPLAIN计划:

1   PRIMARY <derived2>  ALL NULL    NULL    NULL    NULL    26550   100.00  NULL
2   DERIVED store   index   PRIMARY PRIMARY 3   NULL    5   100.00  Using where; Using index; Using temporary; Using filesort
2   DERIVED m   ref PRIMARY,IX_store_id__account_number__sales_account__dept_id IX_store_id__account_number__sales_account__dept_id 3   id_data_1.store.store_id    531 100.00  Using where; Using index
2   DERIVED <derived3>  ref <auto_key0> <auto_key0> 66  id_data_1.store.store_id,id_data_1.m.account_number_dms 10  100.00  Using where
3   DERIVED general_ledger  index   NULL    PRIMARY 69  NULL    496442  100.00  Using where

但如果我第二次打电话:

1   PRIMARY <derived2>  ALL NULL    NULL    NULL    NULL    1318053510  100.00  NULL
2   DERIVED store   index   PRIMARY PRIMARY 3   NULL    5   100.00  Using where; Using index; Using temporary; Using filesort
2   DERIVED m   ref PRIMARY,IX_store_id__account_number__sales_account__dept_id IX_store_id__account_number__sales_account__dept_id 3   id_data_1.store.store_id    531 100.00  Using where; Using index
2   DERIVED <derived3>  ALL NULL    NULL    NULL    NULL    496442  100.00  Using where; Using join buffer (Block Nested Loop)
3   DERIVED general_ledger  index   NULL    PRIMARY 69  NULL    496442  100.00  Using where
哇,哎呀!第一行是从26550行跳到1318053510行。 13亿!它来自哪里?我没有超过几十万行。它试图缓存一些东西吗?它从250毫秒减慢到近5秒。

0 个答案:

没有答案