0

我有一个非常复杂的 SQL 视图定义,它已被继承,需要更改以提高性能。它采用基于外键的记录列表并将返回的行显示为列。

因此:- 使用 RANK 选择的数据

ID RANK DKEY RECORD1 RECORD2 RECORD3
1  1    1    003     Rob     Emmerry
1  2    2    004     Sue     Emmerry

退货

ID REC11 REC12 REC13   REC21 REC22 REC23
1  003   Rob   Emmerry 004   Sue   Emmerry

每个返回的行有 37 列数据重复,最多 5 列。使用

SELECT ID,
MIN(DECODE(ranking,1,RECORD1, NULL)) AS REC11
MIN(DECODE(ranking,1,RECORD2, NULL)) AS REC12
MIN(DECODE(ranking,1,RECORD3, NULL)) AS REC13
MIN(DECODE(ranking,1,RECORD4, NULL)) AS REC14
MIN(DECODE(ranking,1,RECORD5, NULL)) AS REC15
MIN(DECODE(ranking,1,RECORD6, NULL)) AS REC16
MIN(DECODE(ranking,2,RECORD1, NULL)) AS REC21
MIN(DECODE(ranking,2,RECORD2, NULL)) AS REC22
MIN(DECODE(ranking,2,RECORD3, NULL)) AS REC23
MIN(DECODE(ranking,2,RECORD4, NULL)) AS REC24
MIN(DECODE(ranking,2,RECORD5, NULL)) AS REC25
MIN(DECODE(ranking,2,RECORD6, NULL)) AS REC26
FROM 
(
SELECT ID, RANK () OVER (PARTITION BY id ORDER BY dkey) ranking,
RECORD1,
RECORD2,
RECORD3,
RECORD4,
RECORD5,
RECORD6
FROM TABLEA
JOIN
 (SELECT ID, DKEY, RECORD4, RECORD5, RECORD6
  FROM TABLEB
 ) ON TABLEB.DKEY = TABLEA.DKEY AND TABLEB.ID = TABLEA.ID
)
GROUP BY ID;

当对具有索引的 DKEY 字段使用解释计划和过滤时,可能会因为 min/decode 语句而忽略索引。

所以我想用 PIVOT 重写它,但不知道如何开始。

关于如何 a) 获取查询以使用索引 b) 使用 PIVOT 重写的任何想法

第一种选择显然更可取。

谢谢克雷格

更新

这是一些示例数据,显示了我的表格。

    Table 1             
    DKEY PID RECORD1 RECORD2 RECORD3
    1    1   3       Rob     Emmerry
    2    1   4       Sue     Emmerry
    3    1   4       Jan     Morris
    4    1   4       Sue     Pye
    5    1   4       Jane    Taylor

    Table 2             
    CID DKEY RECORD10       
    1   3    A      
    2   3    D      
    3   3    G      
    4   3    J      
    5   4    A      
    6   5    A      
    7   5    D      
    8   6    A      
    9   6    D      
    10  6    G      
    11  7    A      
    12  7    D      
    13  7    G      
    14  7    J      
    15  7    M      

    Table 3             
    QID DKEY RECORD3        
    1   3    C      
    2   6    C      
    3   6    F      
    4   7    C      
    5   7    F      

所以表 2 和表 3 使用 DKEY 链接到表 1 如果我们以 DKEY=3 为例,我希望看到这个:-

    PID DKEY REC1 REC2 REC3   REC4 REC5 REC6 REC7 REC8 REC9 REC10 REC11 REC12 REC13
    1   3    4    Jan  Morris A    D    G    J    NULL C    NULL  NULL  NULL  NULL

表 2 和表 3 中的每一个最多可以有 5 行。表 1 中的字段 PID、DKEY、REC1-REC3,表 2 中的 REC4-REC8 字段,表 3 中的其余字段。表 1 中的其他记录将继续在 REC13、DKEY=4 等之后的行上。

希望这是有道理的。

4

1 回答 1

0
SELECT
    ID,
    MIN(DECODE(ranking,1,RECORD1, NULL)) AS REC11,
    MIN(DECODE(ranking,1,RECORD2, NULL)) AS REC12,
    MIN(DECODE(ranking,1,RECORD3, NULL)) AS REC13,
    MIN(DECODE(ranking,1,RECORD4, NULL)) AS REC14,
    MIN(DECODE(ranking,1,RECORD5, NULL)) AS REC15,
    MIN(DECODE(ranking,1,RECORD6, NULL)) AS REC16,
    MIN(DECODE(ranking,2,RECORD1, NULL)) AS REC21,
    MIN(DECODE(ranking,2,RECORD2, NULL)) AS REC22,
    MIN(DECODE(ranking,2,RECORD3, NULL)) AS REC23,
    MIN(DECODE(ranking,2,RECORD4, NULL)) AS REC24,
    MIN(DECODE(ranking,2,RECORD5, NULL)) AS REC25,
    MIN(DECODE(ranking,2,RECORD6, NULL)) AS REC26
FROM 
(
    SELECT /*+ INDEX(tablea tablea_index) */
        ID,
        RANK () OVER (PARTITION BY id ORDER BY dkey) ranking,
        RECORD1,
        RECORD2,
        RECORD3,
        RECORD4,
        RECORD5,
        RECORD6
    FROM TABLEA
    JOIN TABLEB
    -- was: ON TABB.DKEY = TABLEA.DKEY AND TABB ON TABB.ID = TABLEA.ID
    ON  TABLEB.DKEY = TABLEA.DKEY
    AND TABLEB.ID   = TABLEA.ID
)
GROUP BY ID;
于 2013-06-11T12:25:47.943 回答