日期:2014-05-17  浏览次数:20783 次

sql 语句的优化问题。。。
请高人把sql 性能 效率 优化下。。谢谢。。分全送。。
下面的city_id in(....) 这里数据是不同的,所以要多个sql union all 在一起,
比如sqlA如下:根据city_id 的范围不同还会有 同样的sql 需要union all 在一起。。求优化。
 
SQL code

select aa.fixed_net,aa.school_net,aa.differ_net,aa.wlan_net,aa.other_net,aa.city_name,
aa.oneType_user,aa.twoType_user,aa.threeType_user,aa.other_user,
aa.local_rom,aa.out_rom,aa.in_rom ,aa.nation_out_rom,aa.nation_in_rom from 
(select 
    'city_name' = '写死的', 
    'fixed_net' = sum(case a.id when 1 then a.num else 0 end ) ,
    'school_net' = sum(case a.id when 3 then a.num else 0 end ),
    'differ_net' = sum(case a.id when 4 then a.num else 0 end ),
    'wlan_net' = sum(case a.id when 5 then a.num else 0 end ),
    'other_net' = sum(case a.id when 0 then a.num else 0 end ),
    'oneType_user' = sum(case b.id when 1 then b.num else 0 end ) ,
    'twoType_user' = sum(case b.id when 2 then b.num else 0 end ),
    'threeType_user' = sum(case b.id when 3 then b.num else 0 end ),
    'other_user' = sum(case b.id when 99 then b.num else 0 end ),
    'local_rom' = sum(case c.id when 0 then c.num else 0 end ),
    'out_rom' = sum(case c.id when 1 then c.num else 0 end ),
    'in_rom' = sum(case c.id when 2 then c.num else 0 end ),
    'nation_out_rom' = sum(case c.id when 3 then c.num else 0 end ),
    'nation_in_rom' = sum(case c.id when 5 then c.num else 0 end ) 
    from  
    (select count(user_id) as num,certify_user_type as id 
        from ${tableName} where certify_user_type not in (2) and city_id in ('001','002','003') //此处的数据是动态的
        group by certify_user_type
    ) a 
    ,   
    ( select count(user_id) as num ,user_type_id as id 
        from ${tableName} 
        where user_type_id in (1,2,3,99) and certify_user_type=2 and city_id in ('001','002','003') 
        group by user_type_id
    ) b 
    , 
    ( select count(user_id) as num,romflag as id 
        from ${tableName} where city_id in ('001','002','003') 
        group by romflag
    ) c
) aa



------解决方案--------------------
你的a,b,c的关联条件呢?笛卡尔积?
------解决方案--------------------
如果a b c 各1000行,那你的结果集就是1000000000行。肯定不是你想要的。看你的语句想要的是1000行结果没一行的数据分别从a b c来取吧。你最好还是描述下需求,把表的结构贴上来。
实际上你贴的代码效率非常差而且看不出来到底想要做什么
------解决方案--------------------
完全看不懂你想表达什么。
------解决方案--------------------
探讨

你的a,b,c的关联条件呢?笛卡尔积?

------解决方案--------------------
指定在详细点,能给软件节约很多时间
------解决方案--------------------
没有where条件的sql很可怕!
------解决方案--------------------
这样的语句肯定执行不下去。贴出需求。