0

请找到以下查询,它在/tmp/目录上花费更多时间、负载和空间

SELECT DISTINCT pil.pilsellersku,
  pil.pilname,
  pbi.pbicode,
  '',
  group_concat(DISTINCT ppt.pptcmtrfnum SEPARATOR ','),
  pbi.pbibrand,
  '',
  '',
  '',
  '',
  pil.pilicmdisplaycolor,
  pbi.pbidesc,
  pbi.pbiattrlabel1,
  pil.pilattrvalue1,
  pbi.pbiattrlabel2,
  pil.pilattrvalue2,
  pbi.pbiattrlabel3,
  pil.pilattrvalue3,
  '',
  pod.PODISBESTSELLER,
  pod.PODISNEWARRIVAL,
  pod.PODISCHOSEN,
  pit.pitzoomimage,
  pit.pitimage,
  pil.pilimage,
  pit.pitthumbnailimage,
  pit.pitthumbnailimage,
  pil.pilmrp,
  pod.PODMARGIN,
  pod.PODRETCOST,
  pbi.pbidiscvalue,
  pbi.pbidiscinvalue,
  pil.pilvat,
  pil.pilstockqty,
  pil.PILASRTGRP,
  sot.sotsomrfnum,
  group_concat(DISTINCT ppm.PPMPMTRFNUM SEPARATOR ','),
  ccg.ccgrfnum,
  pil.pilactive,
  pbi.pbiisgiftwrap,
  pbi.pbigiftwrapcharge,
  pbi.pbiispreffdate,
  pbi.pbissirfnum,
  pod.PODSTSL,
  '',
  '',
  pod.PODPVC,
  pod.PODWSPRICE,
  pod.PODODV,
  pod.PODSEQ,
  pod.PODSTCODE,
  pod.PODTAXATTRI,
  pod.PODPRODUNIT,
  pod.PODSTDCOST,
  pod.PODMATCODE,
  pod.PODWEEK,
  pod.PODYEAR,
  pod.PODDYRWK,
  pod.PODISBESTBUY,
  pbi.isonline,
  pbi.pbiseqnum,
  scm.scmcode
FROM Ismpbi pbi
LEFT JOIN ISMPIL pil
  ON pbi.pbirfnum = pil.pilpbirfnum
LEFT JOIN ISMPIT pit
  ON pbi.pbirfnum = pit.pitpbirfnum
    AND pit.pitviewseq = 1
LEFT JOIN ISMSOT sot
  ON sot.sotpbirfnum = pbi.pbirfnum
LEFT JOIN OTHPOD pod
  ON pod.podpbirfnum = pbi.pbirfnum
LEFT JOIN ISMPPM ppm
  ON ppm.ppmpbirfnum = pbi.pbirfnum
LEFT JOIN ISMPPT ppt
  ON ppt.pptpbirfnum = pbi.pbirfnum
LEFT JOIN ISMCCG ccg
  ON pbi.pbiccgrfnum = ccg.ccgrfnum
LEFT JOIN OCTSCM scm
  ON pbi.pbiscmrfnum = scm.scmrfnum
WHERE pbi.PBIBRAND = 35
GROUP BY pil.pilsellersku;

在上面的查询中,所有连接的列和条件列(即pbi.PBIBRAND& pit.pitviewseq)都被索引。

并解释语句如下。

 +----+-------------+-------+--------+-------------------------------------------+--------------+---------+-------------------------+------+----------------------------------------------+ 
 | id | select_type | table | type   | possible_keys                             | key          | key_len | ref                     | rows | Extra                                        |
 +----+-------------+-------+--------+-------------------------------------------+--------------+---------+-------------------------+------+----------------------------------------------+ 
 |  1 | SIMPLE      | pbi   | ref    | IDX_PBIBRAND                              | IDX_PBIBRAND | 9       | const                   | 4420 | Usingwhere; Using temporary; Using filesort  |  
 |  1 | SIMPLE      | pil   | ref    | IDX_PILPBI                                | IDX_PILPBI   | 8       | apparel.pbi.PBIRFNUM    |    3 |                                              | 
 |  1 | SIMPLE      | pit   | ref    | IDX_PITPBI                                | IDX_PITPBI   | 8       | apparel.pbi.PBIRFNUM    |    9 |                                              |  
 |  1 | SIMPLE      | sot   | ref    | PRIMARY,SOTPBIRFNUM,IDX_SOTPBI            | PRIMARY      | 8       | apparel.pbi.PBIRFNUM    |    1 | Using index                                  |
 |  1 | SIMPLE      | pod   | ref    | PODPBIRFNUM,FK_POD_PBI,FK8B908042468F70B6 | PODPBIRFNUM  | 9       | apparel.pbi.PBIRFNUM    |    1 |                                              |
 |  1 | SIMPLE      | ppm   | ref    | IDX_PPMPBI                                | IDX_PPMPBI   | 8       | apparel.pbi.PBIRFNUM    |    1 |                                              |  
 |  1 | SIMPLE      | ppt   | ref    | PRIMARY,PPTPBIRFNUM,IDX_PPTPBI            | PRIMARY      | 8       | apparel.pbi.PBIRFNUM    |    1 | Using index                                  |  
 |  1 | SIMPLE      | ccg   | eq_ref | PRIMARY,CCGRFNUM                          | PRIMARY      | 8       | apparel.pbi.PBICCGRFNUM |    1 | Using index                                  | 
 |  1 | SIMPLE      | scm   | eq_ref | PRIMARY,SCMRFNUM                          | PRIMARY      | 8       | apparel.pbi.PBISCMRFNUM |    1 |                                              | 
 +----+-------------+-------+--------+-------------------------------------------+--------------+---------+-------------------------+------+----------------------------------------------+
 9 rows in set (0.00 sec)

上述查询耗时近 17 分钟。服务器上的负载超过 15,/tmp/磁盘级别的方向消耗 21 GB。

任何人都可以建议优化和修改查询以获得结果。

4

1 回答 1

0

您的整体查询看起来简单而合理......您的 ISMPBI 主表全部左连接到其他表,这对我来说意味着它们是更多的辅助/查找表。

为了提供帮助,我将提供以下内容...将“STRAIGHT_JOIN”关键字添加到您的选择中

选择 STRAIGHT_JOIN DISTINCT ....

对于您的 ISMPBI 表,确保它在 (PBIBRAND) 上有一个索引

确保所有其他表在它们各自的“JOIN”列名上都有索引......对于 ISMPIT 表,在 (PITBIRFNUM, PITVIEWSEQ) 上有索引——因为你有一个用于 PitViewSeq 的 AND 子句

编辑——预查询测试选项..

您可以将其作为预查询运行,以查看 IT 与每个表的连接相比的性能...

         SELECT
              pbi.pbirfnum,
              pil.pilsellersku,
              group_concat(DISTINCT ppt.pptcmtrfnum SEPARATOR ',') as PPT_Values,
              group_concat(DISTINCT ppm.PPMPMTRFNUM SEPARATOR ',') as PPM_Values
           FROM 
              Ismpbi pbi
                 LEFT JOIN ISMPIL pil
                    ON pbi.pbirfnum = pil.pilpbirfnum
                 LEFT JOIN ISMPPM ppm
                    ON pbi.pbirfnum = ppm.ppmpbirfnum
                 LEFT JOIN ISMPPT ppt
                    ON pbi.pbirfnum = ppt.pptpbirfnum 
           WHERE 
              pbi.PBIBRAND = 35
           GROUP BY 
              pbi.pbirfnum,
              pil.pilsellersku
于 2013-06-03T10:55:12.090 回答