| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1026 人关注过本帖
标题:求助,关于统计的SQL语句的优化?
只看楼主 加入收藏
球球
Rank: 6Rank: 6
等 级:贵宾
威 望:27
帖 子:1146
专家分:265
注 册:2005-11-28
结帖率:100%
收藏
 问题点数:0 回复次数:4 
求助,关于统计的SQL语句的优化?
在线求助SQL优化:
Select series,xValue,sum(yValue) AS yValue From (
    SELECT 1 as series,0 As Version,Convert(varchar(2),datepart(m,AnalyseDate))+'.'+Convert(varchar(2),datepart(dd,AnalyseDate)) As xValue,Total As yValue
    FROM dbo.IPTV_USER_LoginAnalyse WHERE AnalyseType = 3 AND [Type] = 2 AND Analysedate between '2008-08-22 00:00:00' and '2008-09-09 23:59:59'
Union All
    SELECT 2 as series,0 As Version,xValue,Count(1) AS yValue FROM(
    SELECT distinct userid,Convert(varchar(2),datepart(m,RowTime))+'.'+Convert(varchar(2),datepart(dd,RowTime)) As xValue FROM IPTV_FISH_HISTORY
    Where RowTime between '2008-08-22 00:00:00' and '2008-09-09 23:59:59') a
    Group by xValue  
Union All
    SELECT 3 as series,0 As Version,xValue,Count(1) AS yValue FROM(
    SELECT ID,Convert(varchar(2),datepart(m,RowTime))+'.'+Convert(varchar(2),datepart(dd,RowTime)) As xValue FROM IPTV_FISH_HISTORY
    WHERE Status = 1 AND RowTime between '2008-08-22 00:00:00' and '2008-09-09 23:59:59') a
    Group by xValue
) AS x group by series,xValue
搜索更多相关主题的帖子: SQL 语句 统计 
2008-09-04 17:02
hslglzs2008
Rank: 1
等 级:新手上路
帖 子:16
专家分:0
注 册:2007-11-9
收藏
得分:0 
各位大虾,请问一下, 我是刚学数据库的,想下个SQL 2005,但是很难下到,不知道这个软件占多大内存,谢谢啦
2008-09-04 18:41
球球
Rank: 6Rank: 6
等 级:贵宾
威 望:27
帖 子:1146
专家分:265
注 册:2005-11-28
收藏
得分:0 
迅雷上面有下载。
分别在UserID_RowTime_Status,Analysedate_AnalyseType_Type,Analysedate,AnalyseType,Type上建了索引,查询百万条数据,速度从1万6提高到8千,还有能优化的地方请不吝指教一下,谢谢了。

好累
2008-09-05 02:15
西风独自凉
Rank: 8Rank: 8
等 级:贵宾
威 望:43
帖 子:3380
专家分:28
注 册:2007-8-2
收藏
得分:0 
基本上也就這樣 了

2008-09-05 16:44
bb3852
Rank: 1
等 级:新手上路
帖 子:82
专家分:0
注 册:2008-4-21
收藏
得分:0 
Select series,xValue,sum(yValue) AS yValue From (
    SELECT 1 as series,0 As Version,Convert(varchar(2),datepart(m,AnalyseDate))+'.'+Convert(varchar(2),datepart(dd,AnalyseDate)) As xValue,Total As yValue
    FROM dbo.IPTV_USER_LoginAnalyse WHERE AnalyseType = 3 AND [Type] = 2 AND Analysedate >='2008-08-22 00:00:00' and Analysedate <='2008-09-09 23:59:59'
Union All
    SELECT case when Status=1 then 3 else 2 end as series,0 As Version,xValue,Count(1) AS yValue FROM(
    SELECT distinct userid,Convert(varchar(2),datepart(m,RowTime))+'.'+Convert(varchar(2),datepart(dd,RowTime)) As xValue     (select count(1) from )
FROM IPTV_FISH_HISTORY
    Where RowTime >='2008-08-22 00:00:00' and RowTime<='2008-09-09 23:59:59') a
    Group by xValue ,series
 
) AS x group by series,xValue
不知道这样会不会快一点
另外:我觉得换种思路写法,不用group by 应该会比较快一点吧

http://hi.baidu.com/bb3852
数据库开发有兴趣的朋友们,里面有很多原创代码
qq:184882759
2008-09-06 13:26
快速回复:求助,关于统计的SQL语句的优化?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.037855 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved