ORACLE SQL:使用“id = id上的连接表”与“其中id =数字”时的慢查询

时间:2018-03-14 13:09:11

标签: sql oracle

当我使用子查询设置ID =数字时,我在查询中遇到性能问题,然后在主查询中加入该子查询以查找该ID,此方法大约需要150秒。但是如果我删除子查询并直接在主查询中查找ID =数字,则需要0.5秒。

这里有一些代码例如: 这是150秒的例子 在这里我在With子句中设置了cto_in_codigo。

WITH CONTRATOS AS (
  SELECT CTO_IN_CODIGO FROM MGCAR.CAR_CONTRATO
  WHERE CTO_IN_CODIGO = 14393
)

SELECT
    PT.PAR_IN_CODIGO,
    PTC.PARCOR_IN_INDICE

  FROM (
      SELECT
        MAX(PT.HPAR_IN_CODIGO) OVER (PARTITION BY PT.PAR_IN_CODIGO, PT.CTO_IN_CODIGO) HPAR_IN_CODIGO_MAX,
        PT.HPAR_IN_CODIGO,
        PT.CTO_IN_CODIGO,
        PT.PAR_IN_CODIGO

      FROM
        QUERIE.PARCELA_TOTAL PT
        JOIN CONTRATOS CTO
          ON CTO.CTO_IN_CODIGO = PT.CTO_IN_CODIGO

      WHERE
        PT.PAR_DT_REAJUSTE <= TO_DATE('31/12/2017', 'DD/MM/YYYY')


    ) PT
  LEFT OUTER JOIN (
      SELECT
        MAX(PTC.PARCOR_IN_CODIGO) OVER (PARTITION BY PTC.PAR_IN_CODIGO, PTC.CTO_IN_CODIGO) PARCOR_IN_CODIGO_MAX,
        PTC.PARCOR_IN_CODIGO,
        PTC.CTO_IN_CODIGO,
        PTC.PAR_IN_CODIGO,
        PTC.HPAR_IN_CODIGO,
        PTC.PARCOR_IN_INDICE

      FROM
        QUERIE.PARCELA_TOTAL_CORRECAO PTC
        JOIN CONTRATOS CTO
          ON CTO.CTO_IN_CODIGO = PTC.CTO_IN_CODIGO

    ) PTC
    ON PTC.CTO_IN_CODIGO = PT.CTO_IN_CODIGO
    AND PTC.PAR_IN_CODIGO = PT.PAR_IN_CODIGO
    AND PTC.HPAR_IN_CODIGO = PT.HPAR_IN_CODIGO
    AND PTC.PARCOR_IN_CODIGO = PTC.PARCOR_IN_CODIGO_MAX

  WHERE
    PT.HPAR_IN_CODIGO = PT.HPAR_IN_CODIGO_MAX

这是0.5秒。 在这里我在每个查询中设置了cto_in_codigo

SELECT
    PT.PAR_IN_CODIGO,
    PTC.PARCOR_IN_INDICE

  FROM (
      SELECT
        MAX(PT.HPAR_IN_CODIGO) OVER (PARTITION BY PT.PAR_IN_CODIGO, PT.CTO_IN_CODIGO) HPAR_IN_CODIGO_MAX,
        PT.HPAR_IN_CODIGO,
        PT.CTO_IN_CODIGO,
        PT.PAR_IN_CODIGO

      FROM
        QUERIE.PARCELA_TOTAL PT

      WHERE
        PT.PAR_DT_REAJUSTE <= TO_DATE('31/12/2017', 'dd/MM/yyyy')
        AND PT.CTO_IN_CODIGO = 14393

    ) PT
  LEFT OUTER JOIN (
      SELECT
        MAX(PTC.PARCOR_IN_CODIGO) OVER (PARTITION BY PTC.PAR_IN_CODIGO, PTC.CTO_IN_CODIGO) PARCOR_IN_CODIGO_MAX,
        PTC.PARCOR_IN_CODIGO,
        PTC.CTO_IN_CODIGO,
        PTC.PAR_IN_CODIGO,
        PTC.HPAR_IN_CODIGO,
        PTC.PARCOR_IN_INDICE

      FROM
        QUERIE.PARCELA_TOTAL_CORRECAO PTC

      WHERE
        PTC.CTO_IN_CODIGO = 14393

    ) PTC
    ON PTC.CTO_IN_CODIGO = PT.CTO_IN_CODIGO
    AND PTC.PAR_IN_CODIGO = PT.PAR_IN_CODIGO
    AND PTC.HPAR_IN_CODIGO = PT.HPAR_IN_CODIGO
    AND PTC.PARCOR_IN_CODIGO = PTC.PARCOR_IN_CODIGO_MAX

  WHERE
    PT.HPAR_IN_CODIGO = PT.HPAR_IN_CODIGO_MAX 

让我感到困惑的是,with子句只返回一行带有cto_in_codigo的数字,就像我在每个查询中的硬编码一样,就像第二个代码一样。什么可能导致这种超级延迟?

0 个答案:

没有答案