简体   繁体   English

提高 MySQL 左连接子查询的性能

[英]Improving the performance of a MySQL left join sub query

I have the following MySQL query which calculates the total number of orders for each month within a given date range eg a year.我有以下 MySQL 查询,它计算给定日期范围内每个月的订单总数,例如一年。 The query works correctly, but the performance is slow (around 250ms).查询正常工作,但性能很慢(大约 250 毫秒)。

Any ideas on how to rewrite it to make it more efficient?关于如何重写它以提高效率的任何想法?

WITH recursive `dates` AS (
    (
        SELECT '2019-11-28' AS item
    )
    UNION
    ALL (
        SELECT
            item + INTERVAL 1 DAY
        FROM
            `dates`
        WHERE
            item + INTERVAL 1 DAY <= '2020-11-27'
    )
)
SELECT
    DATE_FORMAT(`item`, '%b %y') AS `date`,
    COUNT(`orders`.`id`) AS `total`
FROM
    `dates`
    LEFT JOIN (
        SELECT
            `orders`.`id`,
            `orders`.`created_at`
        FROM
            `orders`
            INNER JOIN `locations` ON `orders`.`location_id` = `locations`.`id`
        WHERE
            `orders`.`shop_id` = 10379184
            AND `locations`.`country_id` = 128
            AND `orders`.`created_at` >= '2019-11-28 12:01:42'
            AND `orders`.`created_at` <= '2020-11-27 12:01:42'
    ) AS `orders` ON DATE(`orders`.`created_at`) = `dates`.`item`
GROUP BY
    `date`

UPDATE : Some have suggested using two left joins, however if I do that then the country_id filter is not applied:更新:有些人建议使用两个左连接,但是如果我这样做,则不会应用country_id过滤器:

WITH recursive `dates` AS (
    (
        SELECT
            '2019-11-28' AS item
    )
    UNION
    ALL (
        SELECT
            item + INTERVAL 1 DAY
        FROM
            `dates`
        WHERE
            item + INTERVAL 1 DAY <= '2020-11-27'
    )
)
SELECT
    DATE_FORMAT(`item`, '%b %y') AS `date`,
    COUNT(`orders`.`id`) AS `total`
FROM
    `dates`
    LEFT JOIN `orders` USE INDEX (`orders_created_at_index`) ON DATE(`created_at`) = `dates`.`item`
    AND `orders`.`shop_id` = 10379184
    AND `orders`.`created_at` >= '2019-11-28 12:22:43'
    AND `orders`.`created_at` <= '2020-11-27 12:22:43'
    LEFT JOIN `locations` ON `orders`.`location_id` = `locations`.`id`
    AND `locations`.`country_id` = 128
GROUP BY
    `date`

Thanks!谢谢!

I would suggest using a correlated subquery:我建议使用相关子查询:

SELECT DATE_FORMAT(d.item, '%b %y') AS `date`,
       (SELECT COUNT(*)
        FROM orders o JOIN
             locations l
             ON o.location_id = l.id
        WHERE shop_id = 10379184 AND
              country_id = 128 AND
              o.created_at >= d.item AND
              o.created_at < d.item + interval 1 day
       ) as total
FROM dates d;

This avoids the outer aggregation, which is often a performance improvement.这避免了外部聚合,这通常是一种性能改进。

In addition, indexes could probably help the query, but it is unclear where columns such as country_id and shop_id are coming from.此外,索引可能有助于查询,但不清楚country_idshop_id等列的来源。

After much tinkering, I produced the following which operates in under 40ms, which is good enough for my needs.经过多次修改,我制作了以下运行时间不到 40 毫秒的代码,这足以满足我的需求。 I still think it's not ideal and would welcome any improvements...我仍然认为这并不理想,欢迎任何改进......

SELECT
    `date`,
    COUNT(`order`)
FROM
    (
        WITH recursive `dates` AS (
            (
                SELECT
                    '2019-11-28' AS item
            )
            UNION
            ALL (
                SELECT
                    item + INTERVAL 1 DAY
                FROM
                    `dates`
                WHERE
                    item + INTERVAL 1 DAY <= '2020-11-27'
            )
        )
        SELECT
            DATE_FORMAT(`item`, '%b %y') AS `DATE`,
            `orders`.`id` AS `order`,
            `locations`.`id` AS `location`
        FROM
            `dates`
        LEFT JOIN 
            `orders` 
        ON 
            DATE(`created_at`) = `dates`.`item`
        AND 
            `orders`.`shop_id` = 10379184
        AND 
            `orders`.`created_at` >= '2019-11-28 12:22:43'
        AND 
            `orders`.`created_at` <= '2020-11-27 12:22:43'
        LEFT JOIN 
            `locations` 
        ON 
            `orders`.`location_id` = `locations`.`id`
        AND 
            `locations`.`country_id` = 209
    ) AS items
WHERE
    (
        `order` IS NULL
        AND `location` IS NULL
    )
    OR (
        `order` IS NOT NULL
        AND `location` IS NOT NULL
    )
GROUP BY
    `date`

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM