优化mysql连接查询

时间:2015-06-01 11:02:02

标签: mysql sql testlink

以下mysql查询需要更多时间来获取结果,所以我需要帮助来优化

         SELECT status,
                   count(status) AS COUNT,
                   build_id,
                   results_bu.name,
                   results_bu.creation_ts,
                   results_bu.release_date
            FROM
              (SELECT NHTC.parent_id AS tsuite_id,
                      NHTC.id AS tcase_id,
                      NHTC.name AS name,
                      TPTCV.tcversion_id,
                      TPTCV.platform_id,
                      E.build_id,
                      E.tcversion_number AS VERSION,
                      TCV.tc_external_id AS external_id,
                      E.id AS executions_id,
                      E.status AS status,
                      (TPTCV.urgency * TCV.importance) AS urg_imp
               FROM testplan_tcversions TPTCV 
    /* GO FOR Absolute LATEST exec ID On BUILD,PLATFORM */
               JOIN
                 (SELECT EE.tcversion_id,
                         EE.testplan_id,
                         EE.platform_id,
                         EE.build_id,
                         MAX(EE.id) AS id
                  FROM executions EE
                  WHERE EE.testplan_id=13954
                    AND EE.build_id IN
                      (SELECT id
                       FROM builds
                       WHERE testplan_id = 13954
                         AND active=1
                         AND (creation_ts BETWEEN DATE('2015-01-01') AND DATE('2015-05-10'))
                       ORDER BY name ASC)
                  GROUP BY EE.tcversion_id,
                           EE.testplan_id,
                           EE.platform_id,
                           EE.build_id) AS LEBBP ON LEBBP.testplan_id = TPTCV.testplan_id
               AND LEBBP.platform_id = TPTCV.platform_id
               AND LEBBP.tcversion_id = TPTCV.tcversion_id
               AND LEBBP.testplan_id = 13954
     /* Get execution status WRITTEN on DB */
               JOIN executions E ON E.id = LEBBP.id
               AND E.build_id = LEBBP.build_id
     /* Get Test Case info from Test Case Version */
               JOIN nodes_hierarchy NHTCV ON NHTCV.id = TPTCV.tcversion_id
     /* Get Test Suite info from Test Case  */
               JOIN nodes_hierarchy NHTC ON NHTC.id = NHTCV.parent_id
     /* Get Test Case Version attributes */
               JOIN tcversions TCV ON TCV.id = E.tcversion_id
               WHERE TPTCV.testplan_id=13954
                 AND E.build_id IN
                   (SELECT id
                    FROM builds
                    WHERE testplan_id = 13954
                      AND active=1
                      AND (creation_ts BETWEEN DATE('2015-01-01') AND DATE('2015-05-10'))
                    ORDER BY name ASC)
               UNION ALL
     /* sqlUnion Test suites - not run */
  SELECT NHTC.parent_id AS tsuite_id,     
                    NHTC.id AS tcase_id,
                    NHTC.name AS name,
                    TPTCV.tcversion_id,
                    TPTCV.platform_id,
                    BU.id AS build_id,
                    TCV.VERSION,
                    TCV.tc_external_id AS external_id,
                    COALESCE(E.id,-1) AS executions_id,
                    COALESCE(E.status,'n') AS status,
                    (TPTCV.urgency * TCV.importance) AS urg_imp
               FROM testplan_tcversions TPTCV 
    /* Needed to be able to put a value on build_id on output set  */
               JOIN builds BU ON BU.id IN
                 (SELECT id
                  FROM builds
                  WHERE testplan_id = 13954
                    AND active=1
                    AND (creation_ts BETWEEN DATE('2015-01-01') AND DATE('2015-05-10'))
                  ORDER BY name ASC)
     /* GO FOR Absolute LATEST exec ID On BUILD,PLATFORM */
               LEFT OUTER JOIN
                 (SELECT EE.tcversion_id,
                         EE.testplan_id,
                         EE.platform_id,
                         EE.build_id,
                         MAX(EE.id) AS id
                  FROM executions EE
                  WHERE EE.testplan_id=13954
                    AND EE.build_id IN
                      (SELECT id
                       FROM builds
                       WHERE testplan_id = 13954
                         AND active=1
                         AND (creation_ts BETWEEN DATE('2015-01-01') AND DATE('2015-05-10'))
                       ORDER BY name ASC)
                  GROUP BY EE.tcversion_id,
                           EE.testplan_id,
                           EE.platform_id,
                           EE.build_id) AS LEBBP ON LEBBP.testplan_id = TPTCV.testplan_id
               AND LEBBP.platform_id = TPTCV.platform_id
               AND LEBBP.tcversion_id = TPTCV.tcversion_id
               AND LEBBP.build_id = BU.id
               AND LEBBP.testplan_id = 13954
     /* Get execution status WRITTEN on DB */
               LEFT OUTER JOIN executions E ON E.build_id = LEBBP.build_id
               AND E.testplan_id = TPTCV.testplan_id
               AND E.platform_id = TPTCV.platform_id
               AND E.tcversion_id = TPTCV.tcversion_id
     /* Get Test Case info from Test Case Version */
               JOIN nodes_hierarchy NHTCV ON NHTCV.id = TPTCV.tcversion_id
     /* Get Test Suite info from Test Case  */
               JOIN nodes_hierarchy NHTC ON NHTC.id = NHTCV.parent_id
     /* Get Test Case Version attributes */
               JOIN tcversions TCV ON TCV.id = TPTCV.tcversion_id
               WHERE TPTCV.testplan_id=13954
                 AND BU.id IN
                   (SELECT id
                    FROM builds
                    WHERE testplan_id = 13954
                      AND active=1
                      AND (creation_ts BETWEEN DATE('2015-01-01') AND DATE('2015-05-10'))
                    ORDER BY name ASC)
    /* Get REALLY NOT RUN => BOTH LEBBP.id AND E.id NULL  */
                 AND E.id IS NULL
                 AND LEBBP.id IS NULL) AS RESULT
            LEFT OUTER JOIN builds AS results_bu ON results_bu.id=RESULT.build_id
            GROUP BY status,
                     build_id;
EXPLAIN的

结果如下

id  select_type     table          type          possible_keys                          key                             key_len ref                             rows        Extra
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
1   PRIMARY         <derived2>      ALL                                                                                                                         32768       Using temporary; Using filesort
1   PRIMARY         results_bu      eq_ref          PRIMARY                             PRIMARY                             4   result.build_id 1   
2   DERIVED         <derived3>      ALL                                                                                                                         32229       Using where
2   DERIVED         NHTCV           eq_ref          PRIMARY,pid_m_nodeorder             PRIMARY                             4   LEBBP.tcversion_id                      1   
2   DERIVED         NHTC            eq_ref          PRIMARY                             PRIMARY                             4   bitnami_testlink.NHTCV.parent_id        1   
2   DERIVED         TPTCV           eq_ref          testplan_tcversions_tplan_tcversion testplan_tcversions_tplan_tcversion 12  LEBBP.tcversion_id,LEBBP.platform_id     1  
2   DERIVED         E               eq_ref          PRIMARY                             PRIMARY                             4   LEBBP.id                                 1      Using where
2   DERIVED         TCV             eq_ref          PRIMARY                             PRIMARY                             4   bitnami_testlink.E.tcversion_id          1  
4   DEPENDENT 
    SUBQUERY        builds          unique_subquery PRIMARY,name,testplan_id            PRIMARY                             4   func                                     1      Using where
3   DERIVED         EE              ref             executions_idx1                     executions_idx1                     4                                           135350  Using where; Using index
3   DERIVED         BB              eq_ref          PRIMARY,name,testplan_id            PRIMARY                             4   bitnami_testlink.EE.build_id            1       Using where
5   UNION           BU                              index                               testplan_id                         4                                           569     Using where; Using index
5   UNION           TPTCV           ref             testplan_tcversions_tplan_tcversion testplan_tcversions_tplan_tcversion 4                                           1472    
5   UNION           TCV             eq_ref          PRIMARY                             PRIMARY                             4   bitnami_testlink.TPTCV.tcversion_id     1   
5   UNION           NHTCV           eq_ref          PRIMARY,pid_m_nodeorder             PRIMARY                             4   bitnami_testlink.TPTCV.tcversion_id     1   
5   UNION           NHTC            eq_ref          PRIMARY                             PRIMARY                             4   bitnami_testlink.NHTCV.parent_id        1   
5   UNION           <derived6>      ALL                                                                                                                                 32229   Using where
5   UNION           E               ref             executions_idx1                     executions_idx1                     16 bitnami_testlink.TPTCV.tcversion_id,
                                                                                                                                bitnami_testlink.TPTCV.platform_id,
                                                                                                                                LEBBP.build_id                           1      Using where; Not exists
8   DEPENDENT 
    SUBQUERY        builds          unique_subquery PRIMARY,name,testplan_id            PRIMARY                             4   func                                     1      Using where
7   DEPENDENT
    SUBQUERY        builds          unique_subquery PRIMARY,name,testplan_id            PRIMARY                             4   func                                     1      Using where
6   DERIVED         EE              ref             executions_idx1                     executions_idx1                     4                                           135350  Using where; Using index
6   DERIVED         BB              eq_ref          PRIMARY,name,testplan_id            PRIMARY                             4   bitnami_testlink.EE.build_id            1       Using where
    UNION RESULT    <union2,5>      ALL                     

实际上我们正在尝试获取通过的执行记录数量,从测试链接数据库中选择的测试计划的时间段之间失败

谢谢, 拉姆

0 个答案:

没有答案