退出历史的更有效的查询

时间:2016-04-04 20:11:46

标签: sql db2

此查询已经有效,但我收到超过资源限制的报告"这吓到我了。

我有三个表:mainTable,sideTable和leftJoin。

mainTable与两个员工编号相关联,因为它们可以单独使用,也可以两个一对。

sideTable有员工姓名。

LeftJoin有一个Employee Activity,这是一个很大的活动表,我必须得到最新的状态。

我不知道这是不是一个好的查询,但它会调用sideTable两次并调用LeftJoin两次。

此查询为12秒。

WITH cteTbl AS (
    SELECT
         EmployeeID
         ,MAX(TIMESTAMP(StampDate, StampTime)) AS cteMaxTime
    FROM
         /**/ getMaxDateTime
    GROUP BY EmployeeID
)
SELECT
    /* stuff */
    ,sideTable1.EmployeeName1
    ,sideTable2.EmployeeName2
    ,leftJoinA.EmployeeStatus 
    ,leftJoinB.EmployeeStatus 


FROM 
    /**/ mainTable

LEFT JOIN /**/ sideTable1
    ON mainTable.employeeNumber = sideTable1.employeeNumber

LEFT JOIN /**/ sideTable2
    ON mainTable.employeeNumber = sideTable2.employeeNumber

    /*tons of left joins*/


LEFT JOIN
(
    SELECT      leftJoinA.EmployeeNumber, leftJoinA.EmployeeStatus 
    FROM        /**/ leftJoinA
    INNER JOIN  cteTbl
        ON leftjoinA.EmployeeNumber = cteTbl.EmployeeNumber
            AND TIMESTAMP(leftjoinA.StampDate,leftjoinA.StampTime) = cteTbl.cteMaxTime
) innerStampData1
    ON sideTable1.EmployeeNumber1 = innerStampData1.EmployeeNumber


LEFT JOIN
(
    SELECT      leftJoinB.EmployeeNumber, leftJoinB.EmployeeStatus 
    FROM        /**/ leftJoinB
    INNER JOIN  cteTbl
        ON leftjoinB.EmployeeNumber = cteTbl.EmployeeNumber
            AND TIMESTAMP(leftjoinB.StampDate,leftjoinB.StampTime) = cteTbl.cteMaxTime
) innerStampData2
    ON sideTable2.EmployeeNumber2 = innerStampData2.EmployeeNumber

WHERE
    /**/

GROUP BY 
    /**/

ORDER BY
    /**/

如果我这样做,查询是7秒,但每个团队的结果都是重复的。

LEFT JOIN
(
    SELECT      leftJoinA.EmployeeNumber, leftJoinA.EmployeeStatus 
    FROM        /**/ leftJoinA
    INNER JOIN  cteTbl
        ON leftjoinA.EmployeeNumber = cteTbl.EmployeeNumber
            AND TIMESTAMP(leftjoinA.StampDate,leftjoinA.StampTime) = cteTbl.cteMaxTime
) innerStampData1
    ON sideTable.EmployeeNumber1 = innerStampData1.EmployeeNumber OR sideTable2.EmployeeNumber = innerStampData2.EmployeeNumber

1 个答案:

答案 0 :(得分:0)

尝试此查询:

WITH 
  cteTbl AS (
    SELECT EmployeeID
    , MAX(TIMESTAMP(StampDate, StampTime)) AS cteMaxTime
    FROM /**/ getMaxDateTime
    GROUP BY EmployeeID)
SELECT
/* stuff */
  sideTable1.EmployeeName1
, sideTable2.EmployeeName2
, innerStampData1.EmployeeStatus 
, innerStampData2.EmployeeStatus 
FROM /**/ mainTable
LEFT JOIN /**/ sideTable1
  ON mainTable.employeeNumber = sideTable1.employeeNumber
LEFT JOIN /**/ sideTable2
  ON mainTable.employeeNumber = sideTable2.employeeNumber
  /*tons of left joins*/
LEFT JOIN ( 
  SELECT EmployeeNumber
  , EmployeeStatus 
  FROM /**/ leftJoinA
  WHERE EXISTS(SELECT 1 FROM cteTbl 
    WHERE leftjoinA.EmployeeNumber = cteTbl.EmployeeNumber
    AND TIMESTAMP(leftjoinA.StampDate,leftjoinA.StampTime) = cteTbl.cteMaxTime)
)  innerStampData1
  ON (sideTable1.EmployeeNumber1 = innerStampData1.EmployeeNumber 
    OR sideTable2.EmployeeNumber2 = innerStampData2.EmployeeNumber)

WHERE
    /**/
GROUP BY 
    /**/
ORDER BY
    /**/