MySQL查询需要花费太多时间来执行

时间:2016-02-11 12:32:44

标签: mysql

我必须离开大约14张桌子,执行时间太长 谁能告诉我如何减少时间?

$sql= "SELECT
          pd.*,
          pd.id              AS did,
          gdr.name           AS region,
          gds.name           AS state,
          gdc.name           AS city,
          pus.name           AS user,
          pus.salesheirarchy AS saleshead,
          pur.name           AS rname,
          pc.comp_name       AS cname,
          pl.name            AS dlevel,
          plob.lob_name,
          sr.itemname,
          sr.fromdate,
          sr.openqty AS batchopenqty,
          pp.id,
          pp.sfield1,
          pp.attribute1,
          pp.attribute2,
          pp.attribute3,
          pp.attribute4,
          pp.attribute5,
          pp.attribute6,
          pp.attribute7,
          pp.attribute8,
          pp.attribute9,
          pp.attribute10,
          pu.symbol,
          pg.name  AS pgroup,
          ppc.name AS pcategory,
          (
                 SELECT Sum(openqty)
                 FROM   pre_stock_reports
                 WHERE  fromdate BETWEEN '$year-01-01' AND    '$year-01-31'
                 AND    pre_stock_reports.itemname=sr.itemname
                 AND    pre_stock_reports.distid = sr.distid
                 AND    pre_stock_reports.openqty !='') AS jan,
          (
                 SELECT Sum(openqty)
                 FROM   pre_stock_reports
                 WHERE  fromdate BETWEEN '$year-02-01' AND    '$year-02-31'
                 AND    pre_stock_reports.itemname=sr.itemname
                 AND    pre_stock_reports.distid = sr.distid
                 AND    pre_stock_reports.openqty !='' ) AS feb,
          (
                 SELECT Sum(openqty)
                 FROM   pre_stock_reports
                 WHERE  fromdate BETWEEN '$year-03-01' AND    '$year-03-31'
                 AND    pre_stock_reports.itemname=sr.itemname
                 AND    pre_stock_reports.distid = sr.distid
                 AND    pre_stock_reports.openqty !='') AS mar,
          (
                 SELECT Sum(openqty)
                 FROM   pre_stock_reports
                 WHERE  fromdate BETWEEN '$year-04-01' AND    '$year-04-31'
                 AND    pre_stock_reports.itemname=sr.itemname
                 AND    pre_stock_reports.distid = sr.distid
                 AND    pre_stock_reports.openqty !='') AS apr,
          (
                 SELECT Sum(openqty)
                 FROM   pre_stock_reports
                 WHERE  fromdate BETWEEN '$year-05-01' AND    '$year-05-31'
                 AND    pre_stock_reports.itemname=sr.itemname
                 AND    pre_stock_reports.distid = sr.distid
                 AND    pre_stock_reports.openqty !='') AS may,
          (
                 SELECT Sum(openqty)
                 FROM   pre_stock_reports
                 WHERE  fromdate BETWEEN '$year-06-01' AND    '$year-06-31'
                 AND    pre_stock_reports.itemname=sr.itemname
                 AND    pre_stock_reports.distid = sr.distid
                 AND    pre_stock_reports.openqty !='') AS jun,
          (
                 SELECT Sum(openqty)
                 FROM   pre_stock_reports
                 WHERE  fromdate BETWEEN '$year-07-01' AND    '$year-07-31'
                 AND    pre_stock_reports.itemname=sr.itemname
                 AND    pre_stock_reports.distid = sr.distid
                 AND    pre_stock_reports.openqty !='') AS jul,
          (
                 SELECT Sum(openqty)
                 FROM   pre_stock_reports
                 WHERE  fromdate BETWEEN '$year-08-01' AND    '$year-08-31'
                 AND    pre_stock_reports.itemname=sr.itemname
                 AND    pre_stock_reports.distid = sr.distid
                 AND    pre_stock_reports.openqty !='') AS aug,
          (
                 SELECT Sum(openqty)
                 FROM   pre_stock_reports
                 WHERE  fromdate BETWEEN '$year-09-01' AND    '$year-09-31'
                 AND    pre_stock_reports.itemname=sr.itemname
                 AND    pre_stock_reports.distid = sr.distid
                 AND    pre_stock_reports.openqty !='') AS sep,
          (
                 SELECT Sum(openqty)
                 FROM   pre_stock_reports
                 WHERE  fromdate BETWEEN '$year-10-01' AND    '$year-10-31'
                 AND    pre_stock_reports.itemname=sr.itemname
                 AND    pre_stock_reports.distid = sr.distid
                 AND    pre_stock_reports.openqty !='') AS oct,
          (
                 SELECT Sum(openqty)
                 FROM   pre_stock_reports
                 WHERE  fromdate BETWEEN '$year-11-01' AND    '$year-11-31'
                 AND    pre_stock_reports.itemname=sr.itemname
                 AND    pre_stock_reports.distid = sr.distid
                 AND    pre_stock_reports.openqty !='') AS nov,
          (
                 SELECT Sum(openqty)
                 FROM   pre_stock_reports
                 WHERE  fromdate BETWEEN '$year-12-01' AND    '$year-12-31'
                 AND    pre_stock_reports.itemname=sr.itemname
                 AND    pre_stock_reports.distid = sr.distid
                 AND    pre_stock_reports.openqty !='') AS dece
FROM      pre_stock_reports                             AS sr
LEFT JOIN pre_products                                  AS pp
ON        sr.itemname=pp.product_name
LEFT JOIN pre_uom AS pu
ON        pp.uom=pu.id
LEFT JOIN pre_product_group AS pg
ON        pp.product_group=pg.id
LEFT JOIN pre_product_category AS ppc
ON        pp.product_category=ppc.id
LEFT JOIN pre_distributor AS pd
ON        sr.distid=pd.id
LEFT JOIN pre_company_geo_details AS gdr
ON        pd.region = gdr.id
LEFT JOIN pre_company_geo_details AS gds
ON        pd.state = gds.id
LEFT JOIN pre_company_geo_details AS gdc
ON        pd.city = gdc.id
LEFT JOIN pre_user AS pus
ON        pd.user = pus.id
LEFT JOIN pre_user AS pur
ON        pus.sales_head_name = pur.id
LEFT JOIN pre_company AS pc
ON        pd.company = pc.id
LEFT JOIN pre_levels AS pl
ON        pd.type = pl.level_id
LEFT JOIN pre_lob AS plob
ON        pd.distributor_type = plob.id
WHERE     1 $where
AND       sr.fromdate BETWEEN '$year-01-01' AND       '$year-12-31'
AND       sr.openqty !=''
GROUP BY  sr.itemname
ORDER BY  sr.fromdate ASC,
          gdr.name ASC,
          sr.distid DESC";

感谢。

0 个答案:

没有答案