-2
SELECT
      B.CE_ID,
      A.PPL_CNTR_ID,
      SUM ( A.NBR_IN ) ENTERS,
      SUM ( A.NBR_OUT ) EXITS,
      A.LOG_TS
      + DECODE ( TO_CHAR ( A.LOG_TS,
                       'mi' ),
               '05', 55
                    / 1440,
               '10', 50
                    / 1440,
               '15', 45
                    / 1440,
               '20', 40
                    / 1440,
               '25', 35
                    / 1440,
               '30', 30
                    / 1440,
               '35', 25
                    / 1440,
               '40', 20
                    / 1440,
               '45', 15
                    / 1440,
               '50', 10
                    / 1440,
               '55', 5
                    / 1440,
               0 )
          TS
FROM
      OUTPUTDB.TPPL_CNTR_TRAFFIC A,
      FLUX.V_CE_PPLCNTR B
WHERE
         A.LOG_TS BETWEEN '07-Sep-13' AND '08-Sep-13'
      AND A.PPL_CNTR_ID = B.PPL_CNTR_ID
      AND B.CE_ID IN (SELECT
                        DISTINCT CE_DPN_ID
                   FROM
                        TCE_RLT
                   START WITH
                        CE_PRN_ID = 5798
                   CONNECT BY
                        PRIOR CE_DPN_ID = CE_PRN_ID)
GROUP BY
      B.CE_ID,
      A.PPL_CNTR_ID,
      A.LOG_TS
      + DECODE ( TO_CHAR ( A.LOG_TS,
                       'mi' ),
               '05', 55
                    / 1440,
               '10', 50
                    / 1440,
               '15', 45
                    / 1440,
               '20', 40
                    / 1440,
               '25', 35
                    / 1440,
               '30', 30
                    / 1440,
               '35', 25
                    / 1440,
               '40', 20
                    / 1440,
               '45', 15
                    / 1440,
               '50', 10
                    / 1440,
               '55', 5
                    / 1440,
               0 )
ORDER BY
      B.CE_ID,
      A.PPL_CNTR_ID,
      A.LOG_TS
      + DECODE ( TO_CHAR ( A.LOG_TS,
                       'mi' ),
               '05', 55
                    / 1440,
               '10', 50
                    / 1440,
               '15', 45
                    / 1440,
               '20', 40
                    / 1440,
               '25', 35
                    / 1440,
               '30', 30
                    / 1440,
               '35', 25
                    / 1440,
               '40', 20
                    / 1440,
               '45', 15
                    / 1440,
               '50', 10
                    / 1440,
               '55', 5
                    / 1440,
               0 )

内部子查询

                   SELECT
                        DISTINCT CE_DPN_ID
                   FROM
                        TCE_RLT
                   START WITH
                        CE_PRN_ID = 5798
                   CONNECT BY
                        PRIOR CE_DPN_ID = CE_PRN_ID

返回 970 行,但是执行时间很长,请建议任何备用子句或加入,以便它运行得更快。使用的 2 个表只有一列共有,所以我很确定没有发生交叉连接。

4

3 回答 3

1
  1. 不要使用 A.LOG_TS BETWEEN '07-Sep-13' AND '08-Sep-13' 而是使用 TO_DATE( '07-09-13' , 'DD-MM-YY')
  2. 如果可能,替换 DISTINCT 查询
  3. 删除排序,即 order by
于 2013-10-01T09:46:03.807 回答
1
                 SELECT
                    DISTINCT CE_DPN_ID
               FROM
                    TCE_RLT
               START WITH
                    CE_PRN_ID = 5798
               CONNECT BY
                    PRIOR CE_DPN_ID = CE_PRN_ID

如果您可以向 TCE_RLT 添加一列,那么带有索引的“根”ID 将使查询更便宜:

SELECT CE_DPN_ID
FROM TCE_RLT
WHERE ROOT_ID = 5798

这样的字段可以在插入时廉价地计算:

INSERT INTO TCE_RLT (CE_DPN_ID, CE_PRN_ID,ROOT_ID) 
    VALUES 5798,5798,5798;
INSERT INTO TCE_RLT (CE_DPN_ID, CE_PRN_ID,ROOT_ID) 
    SELECT 12,34,ROOT_ID FROM TCE_RLT WHERE CE_DPN_ID = 34;
于 2013-10-01T13:41:39.880 回答
1
 A.LOG_TS
  + DECODE ( TO_CHAR ( A.LOG_TS,
                   'mi' ),
           '05', 55
                / 1440,
           '10', 50
                / 1440,
           '15', 45
                / 1440,
           '20', 40
                / 1440,
           '25', 35
                / 1440,
           '30', 30
                / 1440,
           '35', 25
                / 1440,
           '40', 20
                / 1440,
           '45', 15
                / 1440,
           '50', 10
                / 1440,
           '55', 5
                / 1440,
           0 )

太糟糕了,在结果中对 A.LOG_TS 使用正确的转换(例如 SELECT TO_CHAR(A.LOG_TS, 'YYYYMMDDHHMI') TS 或任何其他格式,不使用默认转换)并在 GROUP BY 中使用 A.LOG_TS 本身而不进行无意义的转换和 ORDER BY 子句。

于 2013-10-01T13:15:33.127 回答