sqlserver执行效率

来源:互联网 发布:摩托车淘宝 编辑:程序博客网 时间:2024/06/03 12:31

同样一个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语句,效率都是均等的,但是我看到的情况不是。期待别人能有一个合理的解释,我先记录下来

原创粉丝点击