MySQL如何使用一堆子查询优化查询

时间:2017-07-20 08:13:30

标签: php mysql sql

我有一个庞大的项目,我需要进行统计。此查询为我提供了准确的结果,但加载时有点慢,需要以某种方式优化,但无法弄清楚如何。

SELECT
    TRIM(`op`.`products_quantity`) AS `products_quantity`,
    TRIM(`op`.`products_price`) AS `products_price`,
    TRIM(`op`.`orders_products_status`) AS `orders_products_status`,
    TRIM(`p`.`product_type`) AS `product_type`,
    IF(`p`.`product_type` IN ('G'),FLOOR(`op`.`final_price`*`op`.`products_quantity`),
        `op`.`final_price`) AS `final_price`,
    TRIM((
        SELECT
            `o`.`payment_method`
        FROM
            `orders` `o`
        WHERE
            `o`.`orders_id` = `op`.`orders_id`
        GROUP BY `o`.`orders_id`
    )) AS `payment_method`,
    IF(
        TRIM(DATE_FORMAT(STR_TO_DATE(`cd`.`concert_date`,'%d/%m/%Y'),'%d/%m/%Y')) LIKE (
            SELECT  
                TRIM(DATE_FORMAT(`o`.`date_purchased`,'%d/%m/%Y'))
            FROM
                `orders` `o`
            WHERE
                `o`.`orders_id` = `op`.`orders_id`
            GROUP BY `o`.`orders_id`
        ),
        1,0
    ) AS `same_day`
FROM
    `categories` `c`,
    `categories_description` `cd`,
    `products` `p`,
    `orders_products` `op`
WHERE
    `c`.`section_id` = 25
AND
    `cd`.`categories_id` = `c`.`categories_id`
AND
    `p`.`section_id` = `c`.`section_id`
AND
    `p`.`product_type` IN ('P')
AND
    `op`.`products_id` = `p`.`products_id`
GROUP BY `op`.`orders_products_id`
ORDER BY `payment_method` ASC

有人有什么建议吗?

我还想从SQL专家那里得到一些很好的解释,如何组织类似查询的最佳方式以及首先需要在WHERE部分内部进行查询。

3 个答案:

答案 0 :(得分:2)

您可以通过使用 JION

加入所有表格来更好地执行此操作

试试这样: -

SELECT
        TRIM(`op`.`products_quantity`) AS `products_quantity`,
        TRIM(`op`.`products_price`) AS `products_price`,
        TRIM(`op`.`orders_products_status`) AS `orders_products_status`,
        TRIM(`p`.`product_type`) AS `product_type`,
        IF(`p`.`product_type` IN ('G'),FLOOR(`op`.`final_price`*`op`.`products_quantity`),
            `op`.`final_price`) AS `final_price`,
        TRIM((
            SELECT
                `o`.`payment_method`
            FROM
                `orders` `o`
            WHERE
                `o`.`orders_id` = `op`.`orders_id`
            GROUP BY `o`.`orders_id`
        )) AS `payment_method`,
        IF(
            TRIM(DATE_FORMAT(STR_TO_DATE(`cd`.`concert_date`,'%d/%m/%Y'),'%d/%m/%Y')) LIKE (
                SELECT  
                    TRIM(DATE_FORMAT(`o`.`date_purchased`,'%d/%m/%Y'))
                FROM
                    `orders` `o`
                WHERE
                    `o`.`orders_id` = `op`.`orders_id`
                GROUP BY `o`.`orders_id`
            ),
            1,0
        ) AS `same_day`
    FROM
        `categories` `c`
        join `categories_description` `cd` on `cd`.`categories_id` = `c`.`categories_id`
        join `products` `p` on `p`.`section_id` = `c`.`section_id`
        join `orders_products` `op` on `op`.`products_id` = `p`.`products_id`
    WHERE
        `c`.`section_id` = 25
    AND
        `p`.`product_type` IN ('P')

    GROUP BY `op`.`orders_products_id`
    ORDER BY `payment_method` ASC

答案 1 :(得分:1)

您可以尝试并运行几个基本步骤:

  • 检查/添加连接列的索引
  • 分别运行较小的查询并首先改进它们。 使用'解释'检查用于返回结果的行数。 尽量减少这个数字。
  • 按照相同的步骤进行更大的查询。

答案 2 :(得分:1)

由于没有数据,我无法验证查询。但我认为以下可选查询可能会有所帮助:

SELECT
    TRIM(`op`.`products_quantity`) AS `products_quantity`,
    TRIM(`op`.`products_price`) AS `products_price`,
    TRIM(`op`.`orders_products_status`) AS `orders_products_status`,
    TRIM(`p`.`product_type`) AS `product_type`,
    IF(`p`.`product_type` = 'G', FLOOR(`op`.`final_price` * `op`.`products_quantity`), `op`.`final_price`) AS `final_price`,
    TRIM(`t`.`payment_method`) AS `payment_method`,
    IF( TRIM(DATE_FORMAT(STR_TO_DATE(`cd`.`concert_date`,'%d/%m/%Y'),'%d/%m/%Y')) LIKE 
        TRIM(DATE_FORMAT(`t`.`date_purchased`,'%d/%m/%Y')), 1, 0 ) AS `same_day`
FROM
    `categories` `c` INNER JOIN `categories_description` `cd` ON `c`.`categories_id` = `cd`.`categories_id`
    INNER JOIN `products` `p` ON `c`.`section_id` = `p`.`section_id`
    INNER JOIN `orders_products` `op` ON `p`.`products_id` = `op`.`products_id`
    INNER JOIN
    ( SELECT `o`.`orders_id`, `o`.`payment_method`, 
       TRIM(DATE_FORMAT(`o`.`date_purchased`,'%d/%m/%Y')) AS `date_purchased`
       FROM `orders` `o` GROUP BY `o`.`orders_id` ) AS `t` ON `op`.`orders_id` = `t`.`orders_id`
WHERE `c`.`section_id` = 25 AND `p`.`product_type` IN ('P')
GROUP BY `op`.`orders_products_id`
ORDER BY `payment_method`;

请注意,以下部分对于两个内部子查询都很常见:

FROM `orders` `o`
WHERE `o`.`orders_id` = `op`.`orders_id`
GROUP BY `o`.`orders_id`

这意味着您指的是具有相同条件的同一个表两次,只是为了得到两个不同的列。所以我删除了那部分并将其添加为单个子查询的INNER JOIN

另请注意,IN的括号中只有一个值。所以我已将IN替换为=

如果这对您有帮助,请告诉我。