删除订单时,MySQL查询成本会上升

时间:2017-08-28 17:05:27

标签: mysql performance

Without Order With Order

我有两个表,我正在运行一个简单的查询。我遇到了一些性能问题,我注意到如果我通过删除顺序来简化查询,那么解释计划实际上会变得更糟。我会认为解释计划会因为我简化查询而变得更好,但恰恰相反。

我的问题是,为什么会发生这种情况?

Query cost with order by: 399,210.33
Query cost without order by: 648,450.33


SELECT count(alpha.id) AS object_value, bravo.company_id AS object_id 
FROM alpha 
INNER JOIN bravo ON alpha.bravo_id = bravo.id 
WHERE alpha.completed_at >= date('2015-08-01') AND alpha.completed_at <= date('2017-08-10')
GROUP BY object_id
ORDER BY object_value desc;

mysql Ver 14.14 Distrib 5.7.19,for osx10.11(x86_64)using EditLine wrapper

Show Status with Order

Show Status without Order

1 个答案:

答案 0 :(得分:0)

EXPLAIN,优化工具统计信息近似

你有INDEX(completed_at, bravo_id)吗?它应该有助于提高绩效。

重写查询:

SELECT  count(*) AS object_value,  -- no need to test alpha.id for not-NULL
        bravo.company_id AS object_id
    FROM  alpha
    INNER JOIN  bravo  ON alpha.bravo_id = bravo.id
    WHERE  alpha.completed_at >= '2015-08-01'
      AND  alpha.completed_at  < '2015-08-01' + INTERVAL 9 DAY  -- avoids extra second, leap calc, etc
    GROUP BY  object_id
    ORDER BY  object_value desc;

'处理程序'值是准确的; EXPLAIN值是猜测值。

ORDER BY可能不是什么大问题。结果集中有多少行?如果不是数百万,那么排序可能会在RAM中完成。

我希望ORDER BY需要第二个文件排序。哪里有Handler...write值?

是什么产生了图形输出?它是否同意EXPLAIN FORMAT=JSON SELECT ...