使用更多结果的查询工作正常但结果花费的时间较少

时间:2017-06-08 07:58:20

标签: sql oracle performance sqlperformance

SELECT COUNT(*) AS ct
FROM TABLEA _tablea
LEFT OUTER JOIN
  (SELECT DISTINCT CM_CASE_ID,
    LOG_TIMESTAMP,
    ACTION_PERFORMED_BY,
    CASE_FOLLOW_UP_DATE,
    ACTION_PICKLIST_ID,
    CASE_REQUEST_NOTE_VALUE,
    CASE_STATUS_ID,
    RESOLUTION_PICKLIST_ID,
    PREVIOUS_REQUEST_OWNER,
    CURRENT_REQUEST_OWNER,
    CASE_FOLLOW_UP_NOTE_VALUE,
    CM_PENDING_REASON_ID,
    ACCOUNT_STATUS
  FROM TABLEB _tableb
  WHERE _tableb.LOG_TIMESTAMP
    ||_tableb.CM_CASE_ID IN
    (SELECT MIN(_tableb.LOG_TIMESTAMP)
      ||_tableb.CM_CASE_ID AS col_0_0_
    FROM TABLEB _tableb
    WHERE _tableb.CASE_STATUS_ID='A'
    GROUP BY _tableb.CM_CASE_ID
    )
  ) _tableb
ON _tablea.CM_CASE_ID=_tableb.CM_CASE_ID
WHERE _tablea.CASE_CREATION_DATE BETWEEN to_date('12/01/2016', 'mm/dd/yyyy') AND to_date('06/07/2017', 'mm/dd/yyyy') + 1
AND _tablea.CASE_REQUEST_TYPE                                                                                       IN (1,2,3)
AND _tablea.CASE_STATUS_ID                                                                                          IN ('A','B')       

上述查询预计在2016年1月12日至2017年7月6日之间的记录可以在更短的时间内正常工作

SELECT COUNT(*) AS ct
FROM TABLEA _tablea
LEFT OUTER JOIN
  (SELECT DISTINCT CM_CASE_ID,
    LOG_TIMESTAMP,
    ACTION_PERFORMED_BY,
    CASE_FOLLOW_UP_DATE,
    ACTION_PICKLIST_ID,
    CASE_REQUEST_NOTE_VALUE,
    CASE_STATUS_ID,
    RESOLUTION_PICKLIST_ID,
    PREVIOUS_REQUEST_OWNER,
    CURRENT_REQUEST_OWNER,
    CASE_FOLLOW_UP_NOTE_VALUE,
    CM_PENDING_REASON_ID,
    ACCOUNT_STATUS
  FROM TABLEB _tableb
  WHERE _tableb.LOG_TIMESTAMP
    ||_tableb.CM_CASE_ID IN
    (SELECT MIN(_tableb.LOG_TIMESTAMP)
      ||_tableb.CM_CASE_ID AS col_0_0_
    FROM TABLEB _tableb
    WHERE _tableb.CASE_STATUS_ID='A'
    GROUP BY _tableb.CM_CASE_ID
    )
  ) _tableb
ON _tablea.CM_CASE_ID=_tableb.CM_CASE_ID
WHERE _tablea.CASE_CREATION_DATE BETWEEN to_date('05/01/2017', 'mm/dd/yyyy') AND to_date('06/07/2017', 'mm/dd/yyyy') + 1
AND _tablea.CASE_REQUEST_TYPE                                                                                       IN (1,2,3)
AND _tablea.CASE_STATUS_ID   IN ('A','B')   

这个预计在2017年1月5日至2017年7月6日期间记录的查询花费了大约20分钟的时间......

请帮助解释为什么期望更多结果的查询工作正常而不是更少。

1 个答案:

答案 0 :(得分:0)

你正在使sql混淆。将每个表格作为CTE表格如下:但是在应该修复的条件中也有杂乱。我建议将每个选择放在单独的CTE和ON中的每个JOIN条件以及WHERE中的每个其他条件并避免混合太多的东西.part by part select,join,on,where ...,然后在结束时由结果组成。然后只需比较每个查询的执行计划,你会发现差异

;with cte_tablea as (
 SELECT  CM_CASE_ID, CASE_CREATION_DATE, 
 CASE_REQUEST_TYPE , CASE_STATUS_ID
  FROM TABLEA )
,cte_tableb as  (
  SELECT DISTINCT CM_CASE_ID,
  LOG_TIMESTAMP,
  ACTION_PERFORMED_BY,
  CASE_FOLLOW_UP_DATE,
  ACTION_PICKLIST_ID,
  CASE_REQUEST_NOTE_VALUE,
  CASE_STATUS_ID,
  RESOLUTION_PICKLIST_ID,
  PREVIOUS_REQUEST_OWNER,
  CURRENT_REQUEST_OWNER,
  CASE_FOLLOW_UP_NOTE_VALUE,
  CM_PENDING_REASON_ID,
  ACCOUNT_STATUS
 FROM TABLEB 
)

SELECT COUNT(*) AS ct
 FROM cte_tablea _tablea
 LEFT OUTER JOIN
   cte_tablea  _tableb
    on _tablea.CM_CASE_ID=_tableb.CM_CASE_ID
  WHERE _tableb.LOG_TIMESTAMP
    ||_tableb.CM_CASE_ID IN
     (SELECT MIN(_tableb.LOG_TIMESTAMP)
    ||_tableb.CM_CASE_ID AS col_0_0_
       FROM TABLEB cte_tablea
      WHERE _tableb.CASE_STATUS_ID='A'
      GROUP BY _tableb.CM_CASE_ID
    )
  and _tablea.CASE_CREATION_DATE BETWEEN to_date('05/01/2017', 
 'mm/dd/yyyy') AND to_date('06/07/2017', 'mm/dd/yyyy') + 1
  AND _tablea.CASE_REQUEST_TYPE                                                                                       
  IN (1,2,3)
  AND _tablea.CASE_STATUS_ID   IN ('A','B')