优化查询mysql

时间:2018-06-05 18:43:18

标签: mysql sql

我使用Google和文档长时间写了这个请求,但结果很糟糕。

SELECT   id,
         title,
         asin,
         sku,
         shipping1,
         manufacturing1,
         shipping2,
         manufacturing2,
         effective_date
FROM     (
                   SELECT    p.id,
                             p.title,
                             p.asin,
                             p.sku,
                             p.shipping_price      AS shipping1,
                             p.manufacturing_price AS manufacturing1,
                             eh1.amount            AS manufacturing2,
                             eh2.amount            AS shipping2,
                             eh1.effective_date
                   FROM      stores   AS s
                   LEFT JOIN products AS p
                   ON        s.id = p.store_id
                   LEFT JOIN expenses_history AS eh1
                   ON        eh1.product_id = p.id
                   AND       eh1.cost_type = 1
                   LEFT JOIN expenses_history AS eh2
                   ON        eh1.product_id = eh2.product_id
                   AND       eh1.effective_date = eh2.effective_date
                   AND       eh2.cost_type = 2
                   WHERE     s.user_id = 2
                   ORDER BY  eh1.effective_date DESC limit 18446744073709551615) AS exp
GROUP BY exp.id;I unset only_full_group_by

1 个答案:

答案 0 :(得分:0)

如果我们要创建一个聚合,最好是识别它并避免子查询。

您可以执行MySQL的执行计划,这将提供有关索引创建和查询改进的更深入细节。

{{3}}