求sql优化。。高分送

ladybirds2008 2012-06-12 02:30:00
请优化以下sql,我本意是想用sum来统计,但是我还要做user_id(varchar型)排重 ,sum我没有办法对varchar型的user_id排重(反正我不会),所以现在用count来统计,可是效率很低需要30多秒才能出结果,求高手优化。。

select
'stat_time' = '2011-04',
'fixed_net' = count(distinct case certify_user_type when 1 then user_id else null end ) ,
'school_net' = count(distinct case certify_user_type when 3 then user_id else null end ),
'differ_net' = count(distinct case certify_user_type when 4 then user_id else null end ),
'wlan_net' = count(distinct case certify_user_type when 5 then user_id else null end ),
'other_net' = count(distinct case certify_user_type when 0 then user_id else null end ),
'oneType_user' = count(distinct case when user_type_id=1 and certify_user_type=2 then user_id else null end ) ,
'twoType_user' = count(distinct case when user_type_id=2 and certify_user_type=2 then user_id else null end ),
'threeType_user' = count(distinct case when user_type_id=3 and certify_user_type=2 then user_id else null end ),
'other_user' = count(distinct case when user_type_id=99 and certify_user_type=2 then user_id else null end ),
'local_rom' = count(distinct case romflag when 0 then user_id else null end ),
'out_rom' = count(distinct case romflag when 1 then user_id else null end ),
'in_rom' = count(distinct case romflag when 2 then user_id else null end ),
'nation_out_rom' = count(distinct case romflag when 3 then user_id else null end ),
'nation_in_rom' = count(distinct case romflag when 5 then user_id else null end )
from wlan_auth_user_vlan_2011_04 a




...全文
175 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
ladybirds2008 2012-06-12
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 的回复:]
SQL code

select '2011-04' stat_time,
case certify_user_type when 1 then sum(cnt) else 0 end fixed_net,
case certify_user_type when 3 then sum(cnt) else 0 end school_net,
case certify_user_typ……
[/Quote]首先感谢7楼同学,但是这个只是一般的分组,不能满足负载条件的。。
ladybirds2008 2012-06-12
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 的回复:]
每一个去distinct估计比较慢吧,整体distinct以后再统计试试看
(用你原来的count也可以(count(case certify_user_type when 1 then 1 else null end )),这里改为用sum)

SQL code

select
'stat_time' = '2011-04',
'fixed_net' = sum(case ce……
[/Quote]
from (select distinct user_id, user_type_id, certify_user_type, romflag
from wlan_auth_user_vlan_2011_04) a --把distinct统一在一起而不是每个都去distinct

这样去重不可以吧,因为后面还有其它字段呢,有一个不同的 就会多显示一条的,也就是说前面的distinct user_id就有一条失效啊
qybao 2012-06-12
  • 打赏
  • 举报
回复
每一个去distinct估计比较慢吧,整体distinct以后再统计试试看
(用你原来的count也可以(count(case certify_user_type when 1 then 1 else null end )),这里改为用sum)
select 
'stat_time' = '2011-04',
'fixed_net' = sum(case certify_user_type when 1 then 1 else 0 end ) ,
'school_net' = sum(case certify_user_type when 3 then 1 else 0 end ),
'differ_net' = sum(case certify_user_type when 4 then 1 else 0 end ),
'wlan_net' = sum(case certify_user_type when 5 then 1 else 0 end ),
'other_net' = sum(case certify_user_type when 0 then 1 else 0 end ),
'oneType_user' = sum(case when user_type_id=1 and certify_user_type=2 then 1 else 0 end ) ,
'twoType_user' = sum(case when user_type_id=2 and certify_user_type=2 then 1 else 0 end ),
'threeType_user' = sum(case when user_type_id=3 and certify_user_type=2 then 1 else 0 end ),
'other_user' = sum(case when user_type_id=99 and certify_user_type=2 then 1 else 0 end ),
'local_rom' = sum(case romflag when 0 then 1 else 0 end ),
'out_rom' = sum(case romflag when 1 then 1 else 0 end ),
'in_rom' = sum(case romflag when 2 then 1 else 0 end ),
'nation_out_rom' = sum(case romflag when 3 then 1 else 0 end ),
'nation_in_rom' = sum(case romflag when 5 then 1 else 0 end )
from (select distinct user_id, user_type_id, certify_user_type, romflag
from wlan_auth_user_vlan_2011_04) a --把distinct统一在一起而不是每个都去distinct
古布 2012-06-12
  • 打赏
  • 举报
回复
LZ 去数据库版可能会得到更好的答案。
cbdhxka 2012-06-12
  • 打赏
  • 举报
回复
select '2011-04' stat_time,
case certify_user_type when 1 then sum(cnt) else 0 end fixed_net,
case certify_user_type when 3 then sum(cnt) else 0 end school_net,
case certify_user_type when 4 then sum(cnt) else 0 end differ_net,
case certify_user_type when 5 then sum(cnt) else 0 end wlan_net,
case certify_user_type when 0 then sum(cnt) else 0 end other_net,
case when user_type_id=1 and certify_user_type=2 then sum(cnt) else 0 end oneType_user
......
from
(
select certify_user_type,user_type_id,romflag,count(distinct userid) cnt
from wlan_auth_user_vlan_2011_04 a group by certify_user_type,user_type_id,romflag
) t
cbdhxka 2012-06-12
  • 打赏
  • 举报
回复
select '2011-04' stat_time,
case certify_user_type when 1 then sum(cnt) else 0 end fixed_net,
case certify_user_type when 3 then sum(cnt) else 0 end school_net,
case certify_user_type when 4 then sum(cnt) else 0 end differ_net,
case certify_user_type when 5 then sum(cnt) else 0 end wlan_net,
case certify_user_type when 0 then sum(cnt) else 0 end other_net,
case when user_type_id=1 and certify_user_type=2 then sum(cnt) else 0 end oneType_user
......
select certify_user_type,user_type_id,romflag,count(distinct userid) cnt
from wlan_auth_user_vlan_2011_04 a group by certify_user_type,user_type_id,romflag

没测试,大概意思是这个,对这个结果集再处理下,应该就可以得到你想要的结果了,

你想要的应该是group by 后行转列吧
huage 2012-06-12
  • 打赏
  • 举报
回复
看见你的sql太麻烦了 我写一个列子把
2 XiaoHong 11-12 115
3 XiaoMing 06-11 19
4 XiaoGang 05-23 19
5 XiaoTian 08-14 20
以上为原始数据

SELECT SUM(MID(t.birthDate,1,2)) FROM test_user t GROUP BY t.age

11
8
11
上面为查询后的 不知道对你有启发没有
huage 2012-06-12
  • 打赏
  • 举报
回复
user_id 是存的String的数字吗 是的话可以直接sum啊
哎呦喂哈 2012-06-12
  • 打赏
  • 举报
回复
来接分的有分木有啊,其实我是来学习的、嘿嘿
ladybirds2008 2012-06-12
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]
来者 有分啊
[/Quote]来者 有分啊来者 有分啊来者 有分啊来者 有分啊来者 有分啊来者 有分啊来者 有分啊来者 有分啊来者 有分啊来者 有分啊来者 有分啊来者 有分啊来者 有分啊
ladybirds2008 2012-06-12
  • 打赏
  • 举报
回复
来者 有分啊

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧