加快计数

时间:2012-09-20 18:19:25

标签: sql sql-server tsql

如果您要查看此页面:http://www.storeboard.com/directory/california/sanfrancisco/您会发现一个几乎无法访问的页面,因为加载时间可能超过30秒。原因是有一个计数器可以计算每个类别中的业务成员数量。我想知道是否有办法加速我现有的脚本,因为现在我们有超过一百万个成员,并且当前状态下的脚本是无用的。这是我目前的剧本:

SELECT COUNT(MemberID) AS MembersInCountyCat
FROM Member
WHERE NYKACountyID = @NYKACountyID
AND (
        NYKACatID IN (
            SELECT NYKACatID
            FROM NYKACat
            WHERE ParentID = @NYKACatID
        )
        OR
        NYKACatID = @NYKACatID
    )
AND ProfileTypeID <> 1

关于如何加速sql脚本的任何建议都将不胜感激。

非常感谢,

neojakey

2 个答案:

答案 0 :(得分:1)

请试一试。将NYKACat condidtions移动到联接可能会有所帮助,我认为它不会伤害它。如果NYKACat.ParentID = @NYKACatID或NYKACat.NYKACatID = @NYKACatID从NYKACat中删除大部分行,那么它可以预先做到。

with(nolock)允许脏读 如果您不能接受脏读,请将其取下

SELECT COUNT(Member.MemberID) AS MembersInCountyCat
FROM  Member  with (nolock) 
JOIN  NYKACat with (nolock)
  on  NYKACat.NYKACatID = Member.NYKACatID
 and (NYKACat.ParentID = @NYKACatID or NYKACat.NYKACatID = @NYKACatID)
WHERE Member.NYKACountyID = @NYKACountyID
  and Member.rofileTypeID <> 1

答案 1 :(得分:0)

SELECT COUNT(MemberId) AS MembersInCountyCat
FROM Member
    INNER JOIN NYKACat ON Member.NYKACatId = NYKACat.NYKACatId 
WHERE NYKACountyId = @NYKACountyId
    AND (NYKACat.NYKACatId = @NYKACatId OR NYKACat.ParentId = @NYKACatId)
    AND ProfileTypeId <> 1

确保您在ProfileTypeID,Member.NYKACountyId和Member.NYKACatId上有索引。