文本版|topic 高级搜索
   名人堂 帮助 论坛制度 意见反馈 | 首页 博客 周新贴 招聘 专题 新闻
RSS 底部
 
社区导航: 专家门诊   网络技术   操作系统   数据库   程序设计   系统应用   考试认证   CIO及信息化   站长交流   综合交流   下载基地  51CTO产品服务 设为首页 | 收藏本站
51CTO技术论坛» 微软SQL Server专区 » 微软商务智能 » 数据库中生成数据透视表的SQL的效率比较       [ 打印]  [ 订阅]  [ 收藏]  [ 推荐给朋友]   [ 本帖文本页]

论坛跳转:
     
标题: [转载] 数据库中生成数据透视表的SQL的效率比较  ( 查看:275  回复:0 )   
  本主题由 yangfanlengdie 于 2007-11-5 20:45 移动  
 
梦想连接
技术员  点击可查看详细



帖子 200
精华 0
无忧币 1695
积分 691
阅读权限 30
注册日期 2007-4-9
最后登录 2007-12-2 离线

[查看资料]  [发短消息]  [Blog
       
发表于:2007-11-5 20:34   标题:数据库中生成数据透视表的SQL的效率比较
上一帖 |
  (1)以下SQL语句执行效率较低,大约在90分钟执行完毕

select distinct a.pulse_id,a.field_id,
(select  sum(kwh_val) from his_pbs_20041201 where pulse_id = a.pulse_id and to_number(to_char(unix_to_oracle(occur_time),'hh24'))=0 ) as v0,
(select  sum(raw_val) from his_pbs_20041201 where pulse_id = a.pulse_id and to_number(to_char(unix_to_oracle(occur_time),'hh24'))=0 ) as r0,
(select  sum(status)  from his_pbs_20041201 where pulse_id = a.pulse_id and to_number(to_char(unix_to_oracle(occur_time),'hh24'))=0 ) as st0,
from his_pbs_20041201 a
where pulse_id = 164000029;
  (2)通过在SQL中加入Case语句,可以极大提高SQL的执行速度

select distinct a.pulse_id,a.field_id,
sum(case to_number(to_char(unix_to_oracle(occur_time),'hh24')) when 0  then kwh_val  else 0 end) as v0,
sum(case to_number(to_char(unix_to_oracle(occur_time),'hh24')) when 0  then raw_val  else 0 end) as r0,
sum(case to_number(to_char(unix_to_oracle(occur_time),'hh24')) when 0  then status   else 0 end) as st0,
from his_pbs_20041201 a
group by a.pulse_id,a.field_id;
  以上两种取得数据透视表的方法在Oracle与Sql Server中均适用。第一种方法要耗时90分钟,第二种方法只需3分钟即可。



网络工程师到底该不该去考CCIE认证?
2007-11-5 20:341楼
[ 顶部 ]
     
论坛跳转:  

| | |

标记已读 · 删除论坛Cookies · 文本版 · WAP
 
| 诚征版主 | 版主堂 | 意见建议 | 大史记 | 论坛地图
Copyright©2005-2008 51CTO.COM  Powered by Discuz!
本论坛言论纯属发布者个人意见,不代表51CTO网站立场!如有疑义,请与管理员联系。
京ICP备05051492号