1

我有一个用于报告的 mysql 视图。即它计算了一天中有多少 cod 订单、预付订单、cod 订单金额、预付订单金额、总计以及更多的计算,并且它连接了 5 个表。它变得太慢了。除了使用视图之外,有没有更好的方法来满足这种要求。或者如何更快地提出这样的要求

SELECT
  DATE_FORMAT(`orders`.`placed_date`,'%Y-%m%-%d 00:00:00') AS `orderdate`,
  DATE_FORMAT(`orders`.`placed_date`,'%Y-%m%-%d') AS `orderdt`,
  COUNT(DISTINCT `orders`.`order_id`) AS `orderscount`,
  SUM(`order_item`.`quantity`) AS `qunty`,
  SUM((`order_item`.`quantity` * `order_item`.`a_unitprice`)) AS `price`,
  COUNT(DISTINCT (CASE `payment_type`.`a_name` WHEN 'COD' THEN `orders`.`order_id` END)) AS `codorderscount`,
  COUNT(DISTINCT (CASE `payment_type`.`a_name` WHEN 'Prepaid' THEN `orders`.`order_id` END)) AS `prepaidorderscount`,
  ROUND(SUM(IF((`payment_type`.`a_name` = 'COD'),ROUND((((((`order_item`.`quantity` * `order_item`.`a_unitprice`) - IFNULL(ABS(`getdisc_function`(`order_item`.`order_item_id`,`order_item`.`quantity`)),0)) + IFNULL(`getgiftwrap_amount_function`(`order_item`.`order_item_id`,`order_item`.`quantity`),0)) + IFNULL(`order_item`.`sales_tax`,0)) + IFNULL(`getshipping_charge_function`(`order_item`.`order_item_id`,`order_item`.`quantity`),0)),2),0)),0) AS `cod`,
  ROUND(SUM(IF((`payment_type`.`a_name` = 'Prepaid'),ROUND((((((`order_item`.`quantity` * `order_item`.`a_unitprice`) - IFNULL(ABS(`getdisc_function`(`order_item`.`order_item_id`,`order_item`.`quantity`)),0)) + IFNULL(`getgiftwrap_amount_function`(`order_item`.`order_item_id`,`order_item`.`quantity`),0)) + IFNULL(`order_item`.`sales_tax`,0)) + IFNULL(`getshipping_charge_function`(`order_item`.`order_item_id`,`order_item`.`quantity`),0)),2),0)),0) AS `Prepaid`,
  ROUND((SUM(IF((`payment_type`.`a_name` = 'COD'),ROUND((((((`order_item`.`quantity` * `order_item`.`a_unitprice`) - IFNULL(ABS(`getdisc_function`(`order_item`.`order_item_id`,`order_item`.`quantity`)),0)) + IFNULL(`getgiftwrap_amount_function`(`order_item`.`order_item_id`,`order_item`.`quantity`),0)) + IFNULL(`order_item`.`sales_tax`,0)) + IFNULL(`getshipping_charge_function`(`order_item`.`order_item_id`,`order_item`.`quantity`),0)),2),0)) + SUM(IF((`payment_type`.`a_name` = 'Prepaid'),ROUND((((((`order_item`.`quantity` * `order_item`.`a_unitprice`) - IFNULL(ABS(`getdisc_function`(`order_item`.`order_item_id`,`order_item`.`quantity`)),0)) + IFNULL(`getgiftwrap_amount_function`(`order_item`.`order_item_id`,`order_item`.`quantity`),0)) + IFNULL(`order_item`.`sales_tax`,0)) + IFNULL(`getshipping_charge_function`(`order_item`.`order_item_id`,`order_item`.`quantity`),0)),2),0))),0) AS `grandtotal`,
  ROUND((ROUND((SUM(IF((`payment_type`.`a_name` = 'COD'),ROUND((((((`order_item`.`quantity` * `order_item`.`a_unitprice`) - IFNULL(ABS(`getdisc_function`(`order_item`.`order_item_id`,`order_item`.`quantity`)),0)) + IFNULL(`getgiftwrap_amount_function`(`order_item`.`order_item_id`,`order_item`.`quantity`),0)) + IFNULL(`order_item`.`sales_tax`,0)) + IFNULL(`getshipping_charge_function`(`order_item`.`order_item_id`,`order_item`.`quantity`),0)),2),0)) + SUM(IF((`payment_type`.`a_name` = 'Prepaid'),ROUND((((((`order_item`.`quantity` * `order_item`.`a_unitprice`) - IFNULL(ABS(`getdisc_function`(`order_item`.`order_item_id`,`order_item`.`quantity`)),0)) + IFNULL(`getgiftwrap_amount_function`(`order_item`.`order_item_id`,`order_item`.`quantity`),0)) + IFNULL(`order_item`.`sales_tax`,0)) + IFNULL(`getshipping_charge_function`(`order_item`.`order_item_id`,`order_item`.`quantity`),0)),2),0))),0) / COUNT(DISTINCT `orders`.`order_id`)),0) AS `avgtickectsize`,
  ROUND((ROUND((SUM(IF((`payment_type`.`a_name` = 'COD'),ROUND((((((`order_item`.`quantity` * `order_item`.`a_unitprice`) - IFNULL(ABS(`getdisc_function`(`order_item`.`order_item_id`,`order_item`.`quantity`)),0)) + IFNULL(`getgiftwrap_amount_function`(`order_item`.`order_item_id`,`order_item`.`quantity`),0)) + IFNULL(`order_item`.`sales_tax`,0)) + IFNULL(`getshipping_charge_function`(`order_item`.`order_item_id`,`order_item`.`quantity`),0)),2),0)) + SUM(IF((`payment_type`.`a_name` = 'Prepaid'),ROUND((((((`order_item`.`quantity` * `order_item`.`a_unitprice`) - IFNULL(ABS(`getdisc_function`(`order_item`.`order_item_id`,`order_item`.`quantity`)),0)) + IFNULL(`getgiftwrap_amount_function`(`order_item`.`order_item_id`,`order_item`.`quantity`),0)) + IFNULL(`order_item`.`sales_tax`,0)) + IFNULL(`getshipping_charge_function`(`order_item`.`order_item_id`,`order_item`.`quantity`),0)),2),0))),0) / SUM(`order_item`.`quantity`)),0) AS `avgitemprice`,
  ROUND((SUM(`order_item`.`quantity`) / COUNT(DISTINCT `orders`.`order_id`)),0) AS `avgitemperorder`,
  COUNT(DISTINCT (CASE `status`.`status_code` WHEN 'S' THEN `orders`.`order_id` WHEN 'DE' THEN `orders`.`order_id` END)) AS `dispached`,
  (COUNT(DISTINCT `orders`.`order_id`) - COUNT(DISTINCT (CASE `status`.`status_code` WHEN 'S' THEN `orders`.`order_id` WHEN 'DE' THEN `orders`.`order_id` END))) AS `notdispached`
FROM (((((`orders`
       JOIN `order_item`
         ON ((`order_item`.`order_id` = `orders`.`order_id`)))
      JOIN `payment_instruction`
        ON ((`payment_instruction`.`order_id` = `orders`.`order_id`)))
     JOIN `subpayment_type`
       ON ((`payment_instruction`.`payment_method` = `subpayment_type`.`subpayment_type_id`)))
    JOIN `payment_type`
      ON (((`payment_type`.`payment_type_id` = `subpayment_type`.`payment_type_id`)
           AND (`payment_type`.`a_name` = ('Prepaid'
                                             OR 'COD')))))
   LEFT JOIN `status`
     ON ((`status`.`a_statusid` = `orders`.`orderstatus_id`)))
WHERE (`status`.`status_code` NOT IN('PP','PE','X'))
GROUP BY DATE_FORMAT(`orders`.`placed_date`,'%y-%m%-%d')
4

2 回答 2

0

MySQL 中的视图可能很慢。有一篇关于这个问题的文章 - MySQL VIEW 作为性能麻烦制造者

如果可能的话 - 使用没有视图的简单 SELECT 查询。

于 2013-04-05T06:51:21.217 回答
0

只有查询(没有 DDL,行数等)这是相当猜测。

您可以尝试在用于连接表和 inWHERE子句的列上添加索引。如果这些表中有很多行,这尤其有意义。

还要尝试识别此语句中最慢的部分。也许是 JOIN,也许是 SELECT 列表中的这些计算?你可以剪掉一部分,看看没有它会不会很快。然后你应该专注于这个最慢的部分。

于 2013-04-05T06:56:29.470 回答