sqlserver执行效率

同样一个sql语句,再2台服务器上执行的效率不同(2台服务器上数据库结构一致,一台是局域网内,另一台是服务器),数据量基本上相同

如下sql:

select * ,
        SendNum=(case when (s.vclid is null ) then
            --55555555
            (select count(*) from tb_send y where   1=1  and sendtime >= '2012-02-05 00:00:00'  and sendtime <= '2012-02-20 23:59:59'  and sendtag = 1 and hbcnorgps=1 and toagent=1 and (exists (select 1 from V_VclinfoEx g with(nolock) where 1=1 and g.agentid = s.AgentId and g.Vclid = y.VclId )))
            else
              --9999
            (select count(*) from tb_send a where   1=1  and sendtime >= '2012-02-05 00:00:00'  and sendtime <= '2012-02-20 23:59:59'  and sendtag = 1 and hbcnorgps=1 and toagent=1 and a.vclid = s.vclid and (exists (select vclid from V_VclinfoEx with(nolock) where agentid =s.AgentId and  Vclid = a.VclId and Vclid = s.Vclid)))
            end
        )

from (select Id,AgentId,isnull(AgentName,'') as AgentName,isnull(ContactName,'') as ContactName, isnull(Tel,'')  as Tel, isnull(MobilePhone,'')  as MobilePhone, isnull(Address,'')  as Address, isnull(Comment,'')  as Comment
        , convert(nvarchar,vclid) as Vclid,d.oldphone from (
        select row_number() over (order by id desc) as rowId, t.* from (select top 1000 * from v_agent where 1=1 order by Id) t)
         as d where rowId between 1 and 4) s 


再局域网内执行的话,s.svlid = null,这种情况下sendnum字段里的case语句为直接走then,else就不会走,但是当我把else里面的sql换成一个死值的话,数据就很快出来,换成sql语句的话要不到2分钟才能出来。

同样的sql语句,换到服务器上就没有问题

还有一个就是局域网的这台机器当天重启过也不行,后来第二天来了重启了一下就没有问题了


不知道这是为啥?

我始终不明白的是,当case语句满足条件是,就会走then ,else就不会走,那样的话 无论else 里定义的是一个具体的值还是一个多么复杂的sql语句,效率都是均等的,但是我看到的情况不是。期待别人能有一个合理的解释,我先记录下来

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值