首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java Web开发 >

求大神-sql语句优化,该怎么处理

2012-01-13 
求大神---sql语句优化如下四条sql语句,只有where条件不同,怎样写能让他们更加优化,速度,速度,我要将查询出

求大神---sql语句优化
如下四条sql语句,只有where条件不同,怎样写能让他们更加优化,速度,速度,我要将查询出来的数据放在一个集合中,appID,serviceCode,wayID,都一样,只不过count(ID)不一样

select appID,serviceCode,wayID,count(ID) from CupSmsMT where receipt=0 group by appID,serviceCode,wayID

select appID,serviceCode,wayID,count(ID) from CupSmsMT where receipt>=0 group by appID,serviceCode,wayID

select appID,serviceCode,wayID,count(ID) from CupSmsMT whereresult>=0 group by appID,serviceCode,wayID

select appID,serviceCode,wayID,count(ID) from CupSmsMT where result=0 group by appID,serviceCode,wayID

[解决办法]
神马优化,where就一个条件,优化啥?
是要把四条语句的结果合在一起吗?
你的条件有重复,取>=0,在外面判断好了,要不就直接用union all
或者在select处判断

SQL code
select appID,serviceCode,wayID,       sum(case when receipt=0 then 1 else 0 end) as rec0,       sum(case when receipt>0 then 1 else 0 end) as rec1,        sum(case when result=0 then 1 else 0 end) as res0,        sum(case when result>0 then 1 else 0 end) as res1  from CupSmsMT where receipt >= 0 or result >= 0 group by appID,serviceCode,wayID
[解决办法]
SQL code
select appID,serviceCode,wayID,count(case when receipt=0 then 1 else null end),count(case when receipt>=0 then 1 else null end),count(case when result>=0 then 1 else null end),count(case when result=0 then 1 else null end) from CupSmsMT  group by appID,serviceCode,wayID 

热点排行