当前位置: 代码迷 >> Java Web开发 >> 求大神-sql语句优化,该怎么处理
  详细解决方案

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

热度:224   发布时间:2016-04-17 00:24:58.0
求大神---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
  相关解决方案