在SQL Server中使用联接而不是子查询

| 大家好,我与我有以下餐桌关系, 并且我正在存储过程中编写以下嵌套查询
. Select useremail,fullname,city,[state], allowAlerts,allowLetters,aboutMe,avatar,dateregistered,
          (Select COUNT(*) from blog_info where blog_info.userId = @userId)as blogCount,
           (Select count(*) from blog_info where blog_info.isfeatured = 1 AND blog_info.userId = @userId)as featuredCount,
           (Select COUNT(*) from blog_comments where blog_comments.userId = @userId)as commentsCount,
            (Select COUNT(*) from forum where forum.createdby = @userId) as forumCount,
             (Select COUNT(*) from forumresponse where forumresponse.userId = @userId)as responseCount 

          from user_info where _id = @userId.
我想将这个嵌套查询替换为ues Joins查询。 请帮助我实现这一目标。 提前致谢     
已邀请:
select
    u.useremail,
    u.fullname,
    u.city,
    u.[state],
    u.allowAlerts,
    u.allowLetters,
    u.aboutMe,
    u.avatar,
    u.dateregistered,
    isnull(ub.blogCount,0) blogCount,
    uf.featuredCount,
    uf2.forumCount,
    ur.responseCount 
from
    user_info u
left outer join 
    (select userid, count(*) blogCount from blog_info group by userid) ub on ub.userid = u._id
(以此类推,对您原来拥有的每个子查询执行类似的联接)
where
    u._id = @userId
对不起,没用的格式! 注意:查询引擎可能足够聪明,可以对两个查询使用相同的执行计划,因此您不一定会看到改进     
您是否尝试粘贴该查询并将其突出显示在 查询编辑器,然后单击查询设计器工具栏按钮? Management Studio将在查询设计器中打开查询,并根据联接对其进行重写。     
一种替代方法是运行所有这些count子查询并将其存储在单独的SP变量中。您是否尝试过查看其在缓存等方面是否比多个子查询更有效?     

要回复问题请先登录注册