0

我有 3 张桌子。

 - Tab_active(Contains active details )(Contains 30 million records)

 - Tab_apr(Contains customer details for april) (contains 7 lakh
   records) 
 - Tab_may(Contains customer details for may) (contains 7 lakh
   records)
 - Tab_jun(Contains customer details for june) (contains 7 lakh
   records)

所有表的表结构都相同

CustNo Revenue
1000    54.55

现在我写一个程序来计算

[(Revenue for June)/(Avg Revenue for Apr,May)-1]

我需要那些活跃了 3 个月(6 月、4 月和 5 月)的人以及那些在 Tab_active 中的人。

考虑到活动客户记录的大小,使用联接会降低性能。有没有更好的方法来做到这一点?

提前致谢。

4

1 回答 1

1

我可以想到一种无需加入的方法,但我不能保证它会更快:

select sum(revJun) / (sum(RevMay)*0.5 + sum(RevApr)*0.5 - 1)
from (select CustNo, sum(revJun) as revJun, sum(revMay) as revMay, sum(revApr) as revApr,
             count(*) as NumActiveMonths
      from ((select Custno, Revenue, 'Now' as which, 0.0 as RevJun, 0.0 as RevMay, 0.0 as RevApr
             from tab_active
            ) union all
            (select Custno, Revenue, 'Jun', Revenue as RevJun, 0.0 as RevMay, 0.0 as RevApr
             from tab_Jun
            ) union all
            (select Custno, Revenue, 'May', 0.0 as RevJun, Revenue as RevMay, 0.0 as RevApr
             from tab_May
            ) union all
            (select Custno, Revenue, 'Apr', 0.0 as RevJun, 0.0 as RevMay, Revenue as RevApr
             from tab_Apr
            )
           ) t
      group by CustNo
     ) t
where NumActiveMonths = 4

我不知道这会更快。你必须进行实验。

于 2012-07-30T12:20:24.727 回答