Oracle中的优化语句

Posted

技术标签:

【中文标题】Oracle中的优化语句【英文标题】:optimization statement in Oracle 【发布时间】:2016-06-07 09:48:38 【问题描述】:

此查询显示 6 行。 需要输出2行,就是所有列

SELECT 
    D.RIND_S,
    D.RIND_D,
    D.CUIIO,
    D.CAPITOL,
    D.CUATM,
    D.ID_MD_S,
    D.ID_MD_D,
    SUM(COL1_S) COL1_S,
    SUM(COL1_D) COL1_D 
    FROM (

    SELECT

          CASE WHEN D.RIND IN ('1','2','3','4','5','6') THEN D.RIND  END   RIND_S,
          CASE WHEN D.RIND   NOT IN ('00','--','1','2','3','4','5','6') THEN D.RIND  END   RIND_D,
          D.CUIIO    AS CUIIO,
          D.CAPITOL  AS CAPITOL,
          D.CUATM    AS CUATM  ,
          CASE WHEN D.ID_MD IN (11788,11789,11790,11791,11792,11793)      THEN    ID_MD END  AS ID_MD_S,
          CASE WHEN D.ID_MD IN (11795,11796,11797,11798,11799,11800,11801,11802,11803,11804,11805,12521,12238,12240,12241,12524,12242)    THEN    ID_MD END  AS  ID_MD_D,
          CASE WHEN D.RIND IN ('1','2','3','4','5','6')     THEN    CIS2.NVAL(D.COL1) END   AS COL1_S,
          CASE WHEN D.RIND   NOT IN ('00','--','1','2','3','4','5','6')      THEN    CIS2.NVAL(D.COL1) END AS  COL1_D



        FROM
          CIS2.VW_DATA_ALL D



        WHERE
         (D.PERIOADA =:pPERIOADA) AND   
          D.FORM IN (20) AND
          D.CAPITOL IN (1010) 
          AND D.CUIIO IN (5698200,38985903)

        ) D


        GROUP BY 
        D.RIND_S,
        D.RIND_D,
        D.ID_MD_S,
        D.ID_MD_D,
        D.CUIIO,
        D.CAPITOL,
        D.CUATM


        ORDER BY 
        D.CUATM,
        D.CUIIO

此查询显示 6 行。 必须输出 2 行,即所有列。

此查询显示

我需要输出。

【问题讨论】:

如果您已经回答了自己的问题,那么您可以将答案标记为已接受(单击投票按钮下方的勾号),或者,如果您认为该问题不会使其他用户受益,您可以删除问题。 【参考方案1】:

可能会。但他没有禁食。慢一点。

SELECT
  A.RIND AS RIND_S,
  B.RIND AS RIND_D,
  A.CUIIO,
  A.CUATM,
  A.CAPITOL,
  A.ID_MD AS ID_MD_S,
  B.ID_MD AS ID_MD_D,
  SUM(A.COL1) AS COL1_S,
  SUM(B.COL1) AS COL1_D
FROM
(
SELECT
      D.CUIIO,
      D.CUATM,
      D.RIND,
      D.ID_MD,
      D.CAPITOL,
      CIS2.NVAL(D.COL1) AS COL1
    FROM
     CIS2.VW_DATA_ALL D



    WHERE
     (D.PERIOADA =:pPERIOADA) AND    
      D.FORM IN (20) AND
      D.CAPITOL IN (1010) AND
      D.RIND IN ('1','2','3','4','5','6')
      AND D.CUIIO IN (5698200,38985903)

) A
INNER JOIN
(      
 SELECT
      D.CUIIO,
      D.CUATM,
      D.RIND,
      D.ID_MD,
      D.CAPITOL,
      CIS2.NVAL(D.COL1) AS COL1
    FROM
      CIS2.VW_DATA_ALL D


    WHERE
     (D.PERIOADA =:pPERIOADA) AND    
      D.FORM IN (20) AND
      D.CAPITOL IN (1010) AND
      D.RIND NOT IN ('00','--','1','2','3','4','5','6')
     AND D.CUIIO IN (5698200,38985903)

) B ON (A.CUIIO=B.CUIIO)

GROUP BY
  A.RIND,
  B.RIND,
  A.ID_MD,
  B.ID_MD,
  A.CUIIO,
  A.CUATM,
  A.CAPITOL

【讨论】:

【参考方案2】:

如果您不了解内联视图的内部操作,我认为更好的方法是在内联视图之上进行操作。希望下面的 sn-p 有帮助。我试图得到如下所示的输出。

SELECT MAX(A.RIND_S),
  MAX(A.RIND_D),
  A.CUIIO,
  A.CAPITOL,
  A.CUATM,
  MAX(A.ID_MD_S),
  MAX(A.ID_MD_D),
  MAX(A.COL1_S),
  MAX(A.COL1_D)
FROM
  (SELECT D.RIND_S,
    D.RIND_D,
    D.CUIIO,
    D.CAPITOL,
    D.CUATM,
    D.ID_MD_S,
    D.ID_MD_D,
    SUM(COL1_S) COL1_S,
    SUM(COL1_D) COL1_D
  FROM
    (SELECT
      CASE
        WHEN D.RIND IN ('1','2','3','4','5','6')
        THEN D.RIND
      END RIND_S,
      CASE
        WHEN D.RIND NOT IN ('00','--','1','2','3','4','5','6')
        THEN D.RIND
      END RIND_D,
      D.CUIIO   AS CUIIO,
      D.CAPITOL AS CAPITOL,
      D.CUATM   AS CUATM ,
      CASE
        WHEN D.ID_MD IN (11788,11789,11790,11791,11792,11793)
        THEN ID_MD
      END AS ID_MD_S,
      CASE
        WHEN D.ID_MD IN (11795,11796,11797,11798,11799,11800,11801,11802,11803,11804,11805,12521,12238,12240,12241,12524,12242)
        THEN ID_MD
      END AS ID_MD_D,
      CASE
        WHEN D.RIND IN ('1','2','3','4','5','6')
        THEN CIS2.NVAL(D.COL1)
      END AS COL1_S,
      CASE
        WHEN D.RIND NOT IN ('00','--','1','2','3','4','5','6')
        THEN CIS2.NVAL(D.COL1)
      END AS COL1_D
    FROM CIS2.VW_DATA_ALL D
    WHERE (D.PERIOADA =:pPERIOADA)
    AND D.FORM       IN (20)
    AND D.CAPITOL    IN (1010)
    AND D.CUIIO      IN (5698200,38985903)
    ) D
  GROUP BY D.RIND_S,
    D.RIND_D,
    D.ID_MD_S,
    D.ID_MD_D,
    D.CUIIO,
    D.CAPITOL,
    D.CUATM
  )A
GROUP BY A.CUIIO,
  A.CAPITOL,
  A.CUATM;

【讨论】:

以上是关于Oracle中的优化语句的主要内容,如果未能解决你的问题,请参考以下文章

Oracle之sql语句优化

如何使用大量和语句优化 Oracle 查询

Oracle学习篇之SQL语句的优化

PLSQL性能优化技巧

Oracle系列:(31)Oracle SQL语句优化

oracle sql语句优化