日期:2014-05-18  浏览次数:20816 次

比较两条SQL的执行效率
数据量在80W条左右,大家看看哪种SQL的效率高些,请给出理由
经过测试的结果是第2种比第1种的速度快150毫秒左右
--1.
select  
a.BeginTime,a.ServerId,a.BssId,a.BtsId,
sum(isnull(CALLORIGINATEDNUM,0))   as   CALLORIGINATEDNUM,
sum(isnull(PAGINGRESPONSENUM,0))   as   PAGINGRESPONSENUM
from  
dbo.R_CellVoiceCallOrigin   as   a(nolock),
dbo.R_CellVoiceCallPaging   as   b(nolock)
where
a.ServerId=b.ServerId
and   a.BssId=b.BssId
and   a.BtsId=b.BtsId  
and   a.BeginTime=b.BeginTime  
and   a.CellId=b.CellId
and   a.CARRIERID=b.CARRIERID
and   a.begintime   <>   '2005-05-27   00:00:00 '
and   b.begintime   <>   '2005-05-27   00:00:00 '
group   by   a.BeginTime,a.ServerId,a.BssId,a.BtsId
--2.
select  
x.BeginTime,
x.ServerId,
x.BssId,
x.BtsId,
sum(x.CALLORIGINATEDNUM)   as   CALLORIGINATEDNUM,
sum(y.PAGINGRESPONSENUM)   as   PAGINGRESPONSENUM
from
(select  
a.BeginTime,a.ServerId,a.BssId,a.BtsId,a.CellId,a.CARRIERID,
sum(isnull(a.CALLORIGINATEDNUM,0))   as   CALLORIGINATEDNUM
from  
dbo.R_CellVoiceCallOrigin   as   a(nolock)
where
a.begintime   <>   '2005-05-27   00:00:00 '
group   by   a.BeginTime,a.ServerId,a.BssId,a.BtsId,a.CellId,a.CARRIERID
)   as   x
join
(select  
b.BeginTime,b.ServerId,b.BssId,b.BtsId,b.CellId,b.CARRIERID,
sum(isnull(b.PAGINGRESPONSENUM,0))   as   PAGINGRESPONSENUM
from  
dbo.R_CellVoiceCallPaging   as   b(nolock)
where
b.begintime   <>   '2005-05-27   00:00:00 '
group   by   b.BeginTime,b.ServerId,b.BssId,b.BtsId,b.CellId,b.CARRIERID
)   as   y
on  
x.BeginTime=y.BeginTime
and   x.ServerId=y.ServerId
and   x.BssId=y.BssId
and   x.BtsId=y.BtsId
and   x.CellId=y.CellId
and   x.CARRIERID=y.CARRIERID
group   by  
x.BeginTime,
x.ServerId,
x.BssId,
x.BtsId


------解决方案--------------------
为什么不自己做下测试呢?
------解决方案--------------------
用方法1,
方法2用了嵌套查询