2016-04-04 89 views
0

此查詢已經生效,但我收到了「超出資源限制」的報告,這讓我很害怕。提取歷史記錄效率更高的查詢

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

mainTable關聯於兩個員工編號,因爲它們可以單獨使用或以兩個一對工作。

sideTable具有員工姓名。

LeftJoin有員工活動,這是在那裏我得到的最新狀態的活動一大桌。

我不知道這是否是一個好的查詢或沒有,但它調用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 
    /**/ 

如果我這樣做,查詢是七秒鐘,但結果是每個團隊重複。

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 
+0

你對此表什麼指標? –

+0

很難找到更好的查詢,如果你只寫了一部分的sql,不要寫表的定義和不寫示例數據... –

回答

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 
    /**/