sql - Mysql query having multiple calculation getting slower -


i have mysql views used report. i.e calculated how many cod orders, prepaid orders, cod order amount, prepaid order amount, grand total , few more calculations in day , join on 5 tables. getting slower. there better way such kind of requirement other using view. or how make such kind of requirements faster

select   date_format(`orders`.`placed_date`,'%y-%m%-%d 00:00:00') `orderdate`,   date_format(`orders`.`placed_date`,'%y-%m%-%d') `orderdt`,   count(distinct `orders`.`order_id`) `orderscount`,   sum(`order_item`.`quantity`) `qunty`,   sum((`order_item`.`quantity` * `order_item`.`a_unitprice`)) `price`,   count(distinct (case `payment_type`.`a_name` when 'cod' `orders`.`order_id` end)) `codorderscount`,   count(distinct (case `payment_type`.`a_name` when 'prepaid' `orders`.`order_id` end)) `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) `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) `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) `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) `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) `avgitemprice`,   round((sum(`order_item`.`quantity`) / count(distinct `orders`.`order_id`)),0) `avgitemperorder`,   count(distinct (case `status`.`status_code` when 's' `orders`.`order_id` when 'de' `orders`.`order_id` end)) `dispached`,   (count(distinct `orders`.`order_id`) - count(distinct (case `status`.`status_code` when 's' `orders`.`order_id` when 'de' `orders`.`order_id` end))) `notdispached` (((((`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`)            , (`payment_type`.`a_name` = ('prepaid'                                              or 'cod')))))    left join `status`      on ((`status`.`a_statusid` = `orders`.`orderstatus_id`))) (`status`.`status_code` not in('pp','pe','x')) group date_format(`orders`.`placed_date`,'%y-%m%-%d') 

having query (without ddl, number of rows etc.) it's rather guessing.

you can try adding indexes on columns use join tables , in where clause. has sense if there many rows in these tables.

also try recognize slowest part of statement. maybe it's join, maybe these calculations in select list? can cut off 1 part , see wheter fast without it. should focus on slowest part.


Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -