非群集索引无法正常工作

时间:2014-11-03 12:13:03

标签: sql-server

我有一个包含70000多条记录的表格,如果我选择前60行,它会扫描整个聚集索引,执行大约需要7到8秒。我已经定义了Non Clustered index但是那些不起作用下面是详细信息。

聚集指数:同上 NON Clustered Index:UserTypeId,LocationId和CityId

            SELECT TOP ( 60 )
                    UP.Id AS UserId ,
                    UP.FirstName ,
                    UP.LastName ,
                    UP.City ,
                    UP.ImageURL ,
                    0 AS ActivityCount ,
                    UP.IsPublic ,
                    ISNULL(UP.GemsCount, 0) AS GemsCount ,
                    ISNULL(UP.PointCount, 0) AS PointsCount ,
                    ISNULL(UP.FriendsCount, 0) AS FriendsCount ,
                    FORMAT(UP.LastUpdatedDate, 'MM/d/yyyy HH:m:ss tt','en-US') LastUpdatedDateText,
                    Neighbourhood,
                    UP.UserTypeId,
                    UP.CityId,
                    UP.LocationId

            FROM    UserProfiles AS UP 
            WHERE   
                     UP.UserTypeId = 1
                     AND UP.LocationId > 0
                     AND UP.CityId > 0
            ORDER BY UP.LastUpdatedDate DESC

1 个答案:

答案 0 :(得分:2)

原因是查询的最后一部分:

ORDER BY UP.LastUpdatedDate DESC

将其踢出或将列添加到NC索引中你应该没问题。一如既往地提出这样的建议,可能会在其他地方造成更多麻烦,所以要对它进行测试,等等。