请问下面这条语句有什么办法可以优化下:select code,descp,sum(money) sum_money
from ( select a.code,b.descp,sum(c.money*b.acount)money from table1 a,table2 b,table3 c 
       where a.code=b.code and b.descp=c.descp and .. group by a.code,b.descp
       union all
       select e.code,b.descp,sum(f.money*b.acount)money from table4 e,table5 f,table2 b
       where e.code=b.code and b.descp=f.descp and .. group by e.code,b.descp
       union all
       select g.code,b.descp,sum(h.money*b.acount)money from table7 g,table8 h,table2 b
       where g.code=b.code and b.descp=h.descp and .. group by e.code,b.descp
)group by code,descp

解决方案 »

  1.   

    不清楚你的业务逻辑,光看语句看不出优化的地方
    把你的表结构,业务逻辑说一下
    这样才知道是否需要通过UNION来实现
      

  2.   

    table1,3与table4,5与table7,8有没有关联啊,没有关联估计没法不union all。。
    from子句,数据量小的表放最后。里面的group by,a.code,e.code,g.code可以改成b.code
      

  3.   

    主要是统计出三张不同表里面的money然后再合计。而这三张表里面有些信息又要到其它的表里面取出。