我有一个用于报告的 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')