不使用Row_Number()是否有任何其他方式来获取行号

时间:2014-03-20 07:00:57

标签: sql sql-server

当我使用带有SORT的Row_Number()时,查询计划表现不佳。类似于SORT 34%Cost。有没有其他方法来解决这个问题。请参阅此查询中的最后一行

        SELECT * FROM
          (
        SELECT   F.Id, 
                        F.FolderNo, 
                        F.FolderStatusDate, 
                        (SELECT codedesc 
                         FROM   cm.codetable 
                         WHERE  id = F.FolderStatusCode) 'FolderStatus', 
                        (SELECT codedesc 
                         FROM   cm.codetable 
                         WHERE  id = F.FolderLocationCode) 'Location', 
                        F.Volume, 
                        F.SDExhibits, 
                        F.Cost, 
                        F.Remarks,
                        Row_number() OVER(partition BY F.Id  ORDER BY (SELECT NULL) ASC) RN                             
        FROM   cm.pfmfolder F WITH(nolock) 
               INNER JOIN cm.pfmfoldermstipmap PMMAP WITH(nolock) 
                       ON PMMAP.pfmfolderfk = F.id 
               INNER JOIN cm.mstip MST WITH(nolock) 
                       ON MST.id = PMMAP.mstipfk 
        WHERE  ( @FolderNbr IS NULL 
                  OR F.folderno LIKE + @FolderNbr + '%' ) 
               AND ( @ApplnNbr IS NULL 
                      OR MST.applnnbr LIKE + @ApplnNbr + '%' ) 
               AND ( @FolderStatusCode IS NULL 
                      OR F.folderstatuscode = @FolderStatusCode ) 
               AND ( @FolderLocationCode IS NULL 
                      OR F.folderlocationcode = @FolderLocationCode ) 
               AND ( F.folderstatusdate >= @FolderStatusDateFrom 
                     AND F.folderstatusdate < @FolderStatusDateTo ) 
               AND MST.registrycode = @RegistryCode 
               AND PMMAP.deletedby IS NULL 
                  ) AS T
            WHERE RN = 1

1 个答案:

答案 0 :(得分:0)

我非常怀疑sort条款有任何实际成本。您可以将两个查询与打开的实际执行计划进行比较,并查看他们对包的贡献,一个查询和另一个没有row_number()列的查询。

实际上partition by条款有一些实际的运营成本。