了解SQL效率

时间:2014-08-08 17:29:27

标签: sql oracle sqlperformance

我正在查看一个我没有写过的旧查询,这些查询是以我通常不会使用的格式编写的。所以我只是重新编写它,它返回完全相同的数据,但它在40秒内执行,原始代码在14秒内执行。这引起了我的兴趣,我还查看了解释计划标签以了解更多信息。

我的查询的基数是185,000,因为旧查询是416.为什么我的查询效率不高?

旧查询

SELECT C.ID,
   C.DATE_ADDED AS C_DATE,
   G.DATE_ADDED AS G_DATE,
   DECODE (C.CPATH, NULL, 'N', 'Y') AS LINKED,
   ABS (ROUND (TO_NUMBER (G.DATE_ADDED - C.DATE_ADDED))) AS DIFF
FROM VT_C_REC C, VT_G_PATH G
WHERE     C.ID = G.C_ID
   AND (   (    C.CPATH IS NOT NULL
            AND C.CPATH = G.C_ID
            AND C.DATE_ADDED <> G.DATE_ADDED)
        OR (    C.CPATH IS NULL
            AND ABS (ROUND (TO_NUMBER (G.DATE_ADDED - C.DATE_ADDED))) <= 6))
   AND TO_DATE (C.DATE_ADDED, 'YYYYMMDD') >
          TO_DATE ('20070101', 'yyyymmdd')
   AND G.DEL_DT IS NULL
   AND C.DEL_DT IS NULL

新查询

SELECT C.ID,
   C.DATE_ADDED AS C_DATE,
   G.DATE_ADDED AS G_DATE,
   CASE C.CPATH
        WHEN NULL THEN 'N'
        ELSE 'Y'
   END AS LINKED,
   ABS (ROUND(TO_NUMBER(G.DATE_ADDED - C.DATE_ADDED))) AS DIFF

FROM VT_C_REC C INNER JOIN VT_G_PATH G
    ON C.ID = G.C_ID

WHERE TO_DATE(C.DATE_ADDED, 'YYYYMMDD') >  TO_DATE('20070101', 'yyyymmdd')
AND G.DEL_DT IS NULL 
AND C.DEL_DT IS NULL
AND (
    (
        C.CPATH IS NOT NULL
        AND C.CPATH = G.C_ID
        AND C.DATE_ADDED != G.DATE_ADDED
    )  
OR  (
     C.CPATH IS NULL
     AND ABS(ROUND(TO_NUMBER(G.DATE_ADDED - C.DATE_ADDED))) <= 6
    )
);         

如果有人能帮助我理解那会很棒。

0 个答案:

没有答案