MySQL - 改进繁重的多子嵌套查询查询

时间:2015-10-06 02:01:26

标签: php mysql sql join

我正在完成改进应用程序数据库查询的任务。这种重载加载查询大约需要3-5分钟才能完成,这是不可接受的。

以前的开发人员构建了这个查询,但他们并没有很好地完成这项工作。现在我在主查询中有一堆子查询,我必须改进。看起来那些孩子查询正在杀死它。

有人可以告诉我哪种方法最好吗?

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`

0 个答案:

没有答案