加快oracle SQL查询

时间:2019-02-06 09:55:23

标签: sql oracle performance query-optimization

我有一个查询,我正在尝试优化一个查询,该查询需要6分钟才能运行(DS1有200k行,DS2也有200k行)。我想知道甲骨文大师是否可以看到任何优化? 我绝不是专家,而是业余爱好者……这个问题看来是分组工作花费了最长的时间……

WITH DS1 AS
  (SELECT
    /*+ PARALLEL */
    *
  FROM PD1
  WHERE TYPE      = 'TSY'
  AND SOURCE = 'A'
  ),
  DS2 AS
  (SELECT
    /*+ PARALLEL */
    *
  FROM PD1
  WHERE TYPE     = 'LGL'
  AND SOURCE = 'B'
  ),
  Q AS
  (SELECT DS1.ID  AS DS1_ID,
    DS1.CODE  AS DS1_CODE,
    DS2.CODE  AS DS2_CODE,
    DS2.ID        AS DS2_ID,
    DS1.TYPE        AS TYPE1,
    DS2.TYPE        AS TYPE2,
    DS1.SOURCE AS SS1,
    DS2.SOURCE AS SS2,
    DS1.ATTRIBUTE,
    DS1.ATTRIBUTE_VAL,
    DS1.ATT_AND_VAL
  FROM DS1
  JOIN DS2
  ON DS1.ATT_AND_VAL = DS2.ATT_AND_VAL
  ) ,
  GROUPINGS AS
  (SELECT
    /*+no_merge */
    DS1_ID                  AS BASE_ID ,
    DS1_CODE               AS BASE_CODE,
    DS2_CODE               AS TARGET_CODE,
    COUNT(DISTINCT(DS2_ID)) AS COUNT_OF_TARGET_ID
  FROM Q
  GROUP BY DS1_ID,
    DS1_CODE,
    DS2_CODE
  )
SELECT *
FROM GROUPINGS
ORDER BY BASE_ID,
  BASE_CODE,
  TARGET_CODE;

1 个答案:

答案 0 :(得分:0)

我很想通过删除所有公用表表达式和提示来简化现有查询。像这样:

SELECT
    DS1_ID                     AS BASE_ID
  , DS1_CODE                   AS BASE_CODE
  , DS2_CODE                   AS TARGET_CODE
  , COUNT( DISTINCT (DS2_ID) ) AS COUNT_OF_TARGET_ID
FROM (
    SELECT
        DS1.ID     AS DS1_ID
      , DS1.CODE   AS DS1_CODE
      , DS2.CODE   AS DS2_CODE
      , DS2.ID     AS DS2_ID
    FROM PD1 DS1
    JOIN PD1 DS2 ON DS1.ATT_AND_VAL = DS2.ATT_AND_VAL
                AND DS2.TYPE = 'LGL'
                AND DS2.SOURCE = 'B'
    WHERE DS1.TYPE = 'TSY'
    AND DS1.SOURCE = 'A'
    ) Q
GROUP BY
    DS1_ID
  , DS1_CODE
  , DS2_CODE
ORDER BY
    BASE_ID
  , BASE_CODE
  , TARGET_CODE
;

如果要花几分钟时间运行,那么我建议您需要添加索引。首先要做的是获得一个解释计划,以确定是否使用任何索引来缩小后续步骤的范围。