Sign up ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I'm on a task to improve an app's DB query. This heaving-loading query takes roughly 3-5 minutes to complete which is unacceptable.

The previous developers built this query and they didn't do this job very well though. Now I'm having a bunch of child-queries in the main query which I have to improve. Looks like those child-queries are killing it.

Could anybody advise me which is the best approach to this?

SELECT
   `j`.`id`          AS          `job_id`,
   `j`.`job_number`          AS `job_number`,
   `j`.`job_status`          AS `job_status`,
   `j`.`job_registration_date`          AS `job_registered`,
   `j`.`created`          AS `job_created`,
   `q`.`quote_type_id`          AS `quote_type_id`,
   `qt`.`name`          AS `quote_type_name`,
   `q`.`created_by`          AS `consultant_id`,
   (SELECT
      Concat(`pr`.`first_name`,
      ' ',
      `pr`.`last_name`)           
   FROM
      `profiles` `pr`           
   WHERE
      (
         `pr`.`user_id` = `q`.`created_by` 
      ))          AS `consultant_name`,
   `q`.`domain_id`          AS `domain_id`,
   `p`.`id`          AS `package_id`,
   `q`.`id`          AS `quote_id`,
   `q`.`client_id`          AS `client_id`,
   Concat(`c`.`title`,
   ' ',
   `c`.`first_name`,
   ' ',
   `c`.`last_name`)          AS          `client_name`,
   `q`.`site_id`          AS `site_id`,
   `st`.`name`          AS `site_name`,
   Truncate(Abs(( `q`.`accepted_cost` / 1.1 )),
   2)          AS `est_sale`,

   Coalesce((SELECT
      Sum(`i`.`amount`)                    
   FROM
      `invoices` `i`                    
   WHERE
      ( ( `i`.`void` <> 1 )                             
      AND ( `i`.`job_id` = `j`.`id` ) )),
   (SELECT
      Sum(`i`.`amount`)           
   FROM
      `invoices` `i`           
   WHERE
      (          ( `i`.`void` <>            1 )          
      AND (          `i`.`job_id` = `j`.`id` ) )),
   0)          AS `actual_sale`,

   Coalesce((SELECT
      Sum(( `pp`.`qty` * `pp`.`unit_price` ))                    
   FROM
      `package_products` `pp`                    
   WHERE
      ( `pp`.`package_id` = `p`.`id` )),
   (SELECT
      Sum((          `pp`.`qty` * `pp`.`unit_price` ))           
   FROM
      `package_products` `pp`                                                               
   WHERE
      ( `pp`.`package_id`            = `p`.`id` )),
   0)                        AS `total_package_product`,

   Coalesce((SELECT
      Sum(( `ppt`.`qty` * `ppt`.`unit_price` ))                    
   FROM
      `package_parts` `ppt`                    
   WHERE
      ( `ppt`.`package_id` = `p`.`id` )),
   (SELECT
      Sum(( `ppt`.`qty` * `ppt`.`unit_price` ))           
   FROM
      `package_parts` `ppt`                                                                
   WHERE
      ( `ppt`.`package_id` = `p`.`id` )),
   0)          AS `total_package_part`,

   Coalesce((SELECT
      Truncate(Abs(Sum(( ( `lc`.`number_of_technician` *                                              `lc`.`hours` ) *   `lc`.`hours_rate` ))),
      2)   
   FROM
      `labour_costs` `lc`   
   WHERE
      ( `lc`.`package_id` = `p`.`id` )),
   (SELECT
      Truncate(   Abs(   Sum(( ( `lc`.`number_of_technician` *   `lc`.`hours` ) *   `lc`.`hours_rate` ))),
      2)   
   FROM
      `labour_costs` `lc`   
   WHERE
      ( `lc`.`package_id` = `p`.`id` )),
   0)                              AS          `total_labor_cost`,

   Coalesce((SELECT
      Sum(( `pmi`.`qty` * `pmi`.`cost` ))   
   FROM
      `package_misc_items` `pmi`   
   WHERE
      ( `pmi`.`package_id` = `p`.`id` )),
   (SELECT
      Sum(( `pmi`.`qty` * `pmi`.`cost` ))   
   FROM
      `package_misc_items` `pmi`    
   WHERE
      ( `pmi`.`package_id` = `p`.`id` )),
   0)                                    AS          `total_package_misc`,
   `p`.`discount`                                                            AS          `discount`,

   Coalesce((SELECT
      ( CASE   
         WHEN ( Sum(`jf`.`amount`) <> 0 ) THEN   Truncate(Abs(   Sum(`jf`.`amount`)),
         2)   
         ELSE NULL   
      end ) AS `acufile_amount`   
   FROM
      ((`acufiles` `a`   
   JOIN
      `acufile_categories` `c`   
         ON(( `c`.`id` = `a`.`category_id` )))   
   JOIN
      `job_files` `jf`   
         ON(( `jf`.`acufile_id` = `a`.`id` )))   
   WHERE
      ( ( `c`.`abbr` = 'CREDINV' )   
      AND ( `jf`.`job_id` = `j`.`id` ) )),
   0)                AS   `total_acufile_amount`,

   Coalesce((SELECT
      Truncate(Abs(Sum(( `ts`.`hours` *   `ef`.`e_total_hourly_rate` ))),
      2)   
   FROM
      ((`timesheet_entries` `ts`   
   JOIN
      `users` `u`   
         ON(( `ts`.`user_id` = `u`.`id` )))   
   JOIN
      `employee_financials` `ef`   
         ON(( `ef`.`user_id` = `u`.`id` )))   
   WHERE
      ( `ts`.`job_id` = `j`.`id` )),
   (SELECT
      Truncate(Abs(   Sum(( `ts`.`hours` *   `ef`.`e_total_hourly_rate` ))),
      2) AS   `e_total_labor_timecost`   
   FROM
      (   (`timesheet_entries` `ts`   
   JOIN
      `users` `u`   
         ON(( `ts`.`user_id` = `u`.`id` )))   
   JOIN
      `employee_financials` `ef`   
         ON(( `ef`.`user_id` = `u`.`id` )))   
   WHERE
      ( `ts`.`job_id` =        `j`.`id` )),
   0) AS   `total_labor_timecost`   
FROM
   (((((((`packages` `p`                 
JOIN
   `quote_options` `qo`                   
      ON(
         (
            `qo`.`id` = `p`.`quote_option_id` 
         )
      )
   )                
JOIN
`quotes` `q`                  
   ON(
      (
         `q`.`id` = `qo`.`quote_id` 
      )
   )
)               
JOIN
`quote_types` `qt`                 
ON(
   (
      `qt`.`id` = `q`.`quote_type_id` 
   )
)
)              
JOIN
`jobs` `j`                
ON(
(
   `q`.`id` = `j`.`quote_id` 
)
)
)             
JOIN
`contacts` `c`               
ON(
(
`c`.`id` = `q`.`client_id` 
)
)
)            
LEFT JOIN
`companies` `co`                   
ON(
(
`co`.`id` = `c`.`company_id` 
)
)
)           
LEFT JOIN
`sites` `st`                  
ON(
(
`q`.`site_id` = `st`.`id` 
)
)
)   
WHERE
(
`p`.`accepted` = 1 
)   
GROUP  BY
`p`.`id`
share|improve this question
    
The desire to improve code is implied for all questions on this site. Question titles should reflect the purpose of the code, not how you wish to have it reworked. See How to Ask. –  Jamal 2 days ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.