求sql优化。。高分送。

ladybirds2008 2012-06-12 02:19:51
请优化以下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
...全文
120 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
wwwwb 2012-06-12
  • 打赏
  • 举报
回复
certify_user_type
user_type_id
romflaG
user_iD
这4个字段的,内容就可以了,有数据才能测试
ladybirds2008 2012-06-12
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 的回复:]
所以要你将记录内容及要求结果贴出来,去掉多余的字段,上述SQL语句针对 5楼的数据
[/Quote]看我原来的sql语句看不明白吗?。。我觉得我说的 还是挺详尽的啊。。。。要是空表就不能统计数据了吗?
wwwwb 2012-06-12
  • 打赏
  • 举报
回复
所以要你将记录内容及要求结果贴出来,去掉多余的字段,上述SQL语句针对 5楼的数据
wwwwb 2012-06-12
  • 打赏
  • 举报
回复
所以要你将记录帐号出来,去掉多余的字段,上述SQL语句针对 5楼的数据
ladybirds2008 2012-06-12
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 的回复:]
OR
假设认证时间唯一

SELECT count(*) from tta a where not exists(select 1 from tta where a.用户号=用户号 and a.认证时间>认证时间)
[/Quote]我是要去重复 统计。。看我原来的sql啊。。。你写的sql只是最基本的sql啊。。我是用case when 统计不同的数据条件的数据。。
请再分析下看看呢
wwwwb 2012-06-12
  • 打赏
  • 举报
回复
在用户号、认证时间上建立复合索引
wwwwb 2012-06-12
  • 打赏
  • 举报
回复

OR
假设认证时间唯一

SELECT count(*) from tta a where not exists(select 1 from tta where a.用户号=用户号 and a.认证时间>认证时间)
wwwwb 2012-06-12
  • 打赏
  • 举报
回复
select count(*) from (
select 用户号 from tt group by 用户号)

or

select count(*) from (
select distinct 用户号 from tt )

ladybirds2008 2012-06-12
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 的回复:]
记录呢?
[/Quote]数据太多,不用记录数据也应该可以看出规律吧
大体来说就是统计满足条件的 去除重复的user_id的个数。。
比如:
用户号 认证次数 认证时间
1890000001 5 2001
1890000001 5 2002
1890000002 5 2002
1890000001 5 2004
我按一些条件统计后得到用户数 是2(即 满足条件的有1890000001、1890000002 这2个账号在此表中)
wwwwb 2012-06-12
  • 打赏
  • 举报
回复
记录呢?
ladybirds2008 2012-06-12
  • 打赏
  • 举报
回复
其中certify_user_type=2的c网用户是用 user_type_id来区分出c网中的几个类型,
帐号类型 区分规则 类型编号
189帐号 189开头 1
153帐号 153开头 2
133帐号 133开头 3
其它 99
ladybirds2008 2012-06-12
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]
贴建表及插入记录的SQL,及要求结果出来看看

从SQL语句上看不出如何优化
[/Quote]
序号 描述 列名 类型(长度) 主键 缺省值 是否不为空 备注
1. 时间 stat_time numeric(10)
2. 认证平台编号 auth_platform_id numeric(2) 0:表示不区分认证平台具体的认证平台id都是大于0的.
3. 用户账号 user_id varchar(50) 是 是
4. bras_ip bras_ip varchar2(50) 是 是
5. nas_port_id nas_port_id varchar2(100)
6. 统计单元id stu_id varchar2(50)
7. 统计单元描述 stu_desc varchar2(100)
8. 热点类型 hotspot_type_id numeric(6)
9. 属地 city_id varchar2(20)
10. 账号类型 user_type_id numbeic(2)
11. 漫游状态 romflag numeric(1) 0 非漫游1 漫游出访2 漫游来访3 国际漫游出访5.国际漫游来访 6.省内漫游
12. 认证成功次数 authsuc_times numeric(20) 0
13. 在线时长 online_time numeric(20) 秒
14. 用户接收字节数 recv_bytes numeric(20)
15. 用户发送字节数 send_bytes numeric(20)
16. 总流量 total_bytes numeric(20) Byte
17. 用户接收包数 recv_packets numeric(20)
18. 用户发送包数 send_packets numeric(20)
19. 总包数 total_packets numeric(20)
20. 用户IP user_ip varchar2(50)
21. 接入服务器端口 server_port varchar2(30)
22. 登录域 area_name varchar(200) 登录域名,
23. 用户属地 area_no varchar(20) 用户属地(账号办理地),和接入地相对,江苏使用
24. 固网认证用户类型 certify_user_type numric(2) 1:固网用户,2:c网用户,3:校园网用户,4:异网用户, 5:wlan上网卡用户 0:其它
wwwwb 2012-06-12
  • 打赏
  • 举报
回复

贴建表及插入记录的SQL,及要求结果出来看看

从SQL语句上看不出如何优化

2,596

社区成员

发帖
与我相关
我的任务
社区描述
Sybase相关技术讨论区
社区管理员
  • Sybase社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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