在select语句中调整oracle子查询

时间:2015-09-22 11:15:53

标签: sql oracle11g sqlperformance query-tuning

我有一个主表和一个参考表,如下所示。

WITH MAS as (
SELECT 10 as CUSTOMER_ID, 1 PROCESS_ID, 44 PROCESS_TYPE, 200 as AMOUNT FROM DUAL UNION ALL
SELECT 10 as CUSTOMER_ID, 1 PROCESS_ID, 44 PROCESS_TYPE, 250 as AMOUNT FROM DUAL UNION ALL
SELECT 10 as CUSTOMER_ID, 2 PROCESS_ID, 45 PROCESS_TYPE, 300 as AMOUNT FROM DUAL UNION ALL
SELECT 10 as CUSTOMER_ID, 2 PROCESS_ID, 45 PROCESS_TYPE, 350 as AMOUNT FROM DUAL 
), REFTAB as (
SELECT 44 PROCESS_TYPE, 'A' GROUP_ID FROM DUAL UNION ALL 
SELECT 44 PROCESS_TYPE, 'B' GROUP_ID FROM DUAL UNION ALL
SELECT 45 PROCESS_TYPE, 'C' GROUP_ID FROM DUAL UNION ALL 
SELECT 45 PROCESS_TYPE, 'D' GROUP_ID FROM DUAL
) SELECT ...

我的第一个select语句正常工作就是这个:

SELECT CUSTOMER_ID,
       SUM(AMOUNT) as AMOUNT1,
       SUM(CASE WHEN PROCESS_TYPE IN (SELECT PROCESS_TYPE FROM REFTAB WHERE GROUP_ID = 'A') 
                THEN AMOUNT ELSE NULL END) as AMOUNT2,
       COUNT(CASE WHEN PROCESS_TYPE IN (SELECT PROCESS_TYPE FROM REFTAB WHERE GROUP_ID = 'D') 
                  THEN 1 ELSE NULL END) as COUNT1
   FROM MAS
  GROUP BY CUSTOMER_ID

但是,为了解决性能问题,我将其更改为此select语句:

SELECT CUSTOMER_ID,
       SUM(AMOUNT) as AMOUNT1,
       SUM(CASE WHEN GROUP_ID = 'A' THEN AMOUNT ELSE NULL END) as AMOUNT2,
       COUNT(CASE WHEN GROUP_ID = 'D' THEN 1 ELSE NULL END) as COUNT1
   FROM MAS A
   LEFT JOIN REFTAB B ON A.PROCESS_TYPE = B.PROCESS_TYPE
  GROUP BY CUSTOMER_ID

对于AMOUNT2COUNT1列,值保持不变。但是对于AMOUNT1,由于与引用表的连接,该值会成倍增加。

我知道我可以在GROUP_ID添加1个左连接并添加其他连接条件。但这与使用子查询不同。

知道如何在不加倍AMOUNT1值的情况下仅使用1个左连接进行查询吗?

3 个答案:

答案 0 :(得分:0)

通常的方法是汇总group by之前的值。如果查询的其余部分正确,您还可以使用条件聚合:

SELECT CUSTOMER_ID,
       SUM(CASE WHEN seqnum = 1 THEN AMOUNT END) as AMOUNT1,
       SUM(CASE WHEN GROUP_ID = 'A' THEN AMOUNT ELSE NULL END) as AMOUNT2,
       COUNT(CASE WHEN GROUP_ID = 'D' THEN 1 ELSE NULL END) as COUNT1
FROM MAS A LEFT JOIN
     (SELECT B.*, ROW_NUMBER() OVER (PARTITION BY PROCESS_TYPE ORDER BY PROCESS_TYPE) as seqnum
      FROM REFTAB B
     ) B
     ON A.PROCESS_TYPE = B.PROCESS_TYPE
GROUP BY CUSTOMER_ID;

这会忽略连接创建的重复项。

答案 1 :(得分:0)

  

我知道我可以添加1个左连接并添加aditional GROUP_ID子句,但它不会与子查询不同。

你会感到惊讶。在SELECT中有2个左连接而不是子查询,为优化器提供了更多优化查询的方法。我还是会尝试一下:

select m.customer_id,
       sum(m.amount) as amount1,
       sum(case when grpA.group_id is not null then m.amount end) as amount2,
       count(grpD.group_id) as count1
  from mas m
  left join reftab grpA
    on grpA.process_type = m.process_type
   and grpA.group_id = 'A'
  left join reftab grpD
    on grpD.process_type = m.process_type
   and grpD.group_id = 'D'
 group by m.customer_id

您还可以尝试此查询,该查询使用SUM()分析函数在联接之前计算amount1,以避免重复值问题:

select m.customer_id,
       m.customer_sum as amount1,
       sum(case when r.group_id = 'A' then m.amount end) as amount2,
       count(case when r.group_id = 'D' then 'X' end) as count1
  from (select customer_id,
               process_type,
               amount,
               sum(amount) over (partition by customer_id) as customer_sum
          from mas) m
  left join reftab r
    on r.process_type = m.process_type
 group by m.customer_id,
          m.customer_sum

您可以测试这两个选项,看看哪个选项效果更好。

答案 2 :(得分:0)

从原始查询开始,只需使用IN语句替换EXISTS个查询,即可获得显着提升。另外,要谨慎总结NULL s,或许您的ELSE语句应为0

SELECT CUSTOMER_ID,
       SUM(AMOUNT) as AMOUNT1,
       SUM(CASE WHEN EXISTS(SELECT 1 FROM REFTAB WHERE REFTAB.GROUP_ID = 'A' AND REFTAB.PROCESS_TYPE = MAS.PROCESS_TYPE)
                THEN AMOUNT ELSE NULL END) as AMOUNT2,
       COUNT(CASE WHEN EXISTS(SELECT 1 FROM REFTAB WHERE REFTAB.GROUP_ID = 'D' AND REFTAB.PROCESS_TYPE = MAS.PROCESS_TYPE) 
                  THEN 1 ELSE NULL END) as COUNT1
   FROM MAS
  GROUP BY CUSTOMER_ID