当前位置: 代码迷 >> Sql Server >> 写了一个SQL函数要求返回一个count关联了三个表,该怎么处理
  详细解决方案

写了一个SQL函数要求返回一个count关联了三个表,该怎么处理

热度:72   发布时间:2016-04-27 17:36:00.0
写了一个SQL函数要求返回一个count关联了三个表
ALTER                   function   GetInvitePersonCount(@ActorCode   varchar(10))
returns   int
as
begin
declare   @PersonCount   int

select   @PersonCount=count(*)   from   InvitePerson   p  
where   exists(select   t.InviteID   from   InviteInfo   t     where   t.InviteID=p.InviteID   and      
exists(   select   j.ActorCode   from   ActorJoin   j     where   j.ActorCode=t.ActorCode    
and   ([email protected]   or   [email protected])))


return   @PersonCount

end

当这三个表数据量很大的时候执行数据就比较慢了,希望高手们看能不能优化一下!谢谢各位了

------解决方案--------------------
ALTER function GetInvitePersonCount(@ActorCode varchar(10))
returns int
as
begin
declare @PersonCount int

select @PersonCount=count(1) from InvitePerson p
inner join InviteInfo t on t.InviteID=p.InviteID
inner join ActorJoin j on j.ActorCode=t.ActorCode
where [email protected] or [email protected]

return @PersonCount
end
  相关解决方案