我有一个用于报告的mysql视图。即它计算了一天中有多少鳕鱼订单,预付订单,鳕鱼订单金额,预付订单金额,总计和更多的计算,以及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')
答案 0 :(得分:0)
MySQL中的视图可能很慢。有一篇关于这个问题的文章 - MySQL VIEW as performance troublemaker。
如果可能 - 使用没有视图的简单SELECT查询。
答案 1 :(得分:0)
只有查询(没有DDL,行数等),这是相当猜测。
您可以尝试在用于连接表的列和WHERE
子句中添加索引。如果这些表中有很多行,它就有意义。
同时尝试识别此陈述中最慢的部分。也许它是JOIN,也许是SELECT列表中的这些计算?如果没有它,你可以切掉一个部分,看看它会很快。那么你应该专注于这个最慢的部分。