当前位置:数据库 > SQLServer >>

sqlserver2005中ROW_NUMBER和存储过程性能比较

语法:ROW_NUMBER() OVER(PARTITION BY COLUMN ORDER BY COLUMN)

简单的说row_number()从1开始,为每一条分组记录返回一个数字,这里的ROW_NUMBER() OVER (ORDER BY xlh DESC) 是先把xlh列降序,再为降序以后的没条xlh记录返回一个序号。
示例:
xlh           row_num
1700              1
1500              2
1085              3
710                4

row_number() OVER (PARTITION BY COL1 ORDER BY COL2) 表示根据COL1分组,在分组内部根据 COL2排序,而此函数计算的值就表示每组内部排序后的顺序编号(组内连续的唯一的)

实例:

初始化数据

create table employee (empid int ,deptid int ,salary decimal(10,2))
insert into employee values(1,10,5500.00)
insert into employee values(2,10,4500.00)
insert into employee values(3,20,1900.00)
insert into employee values(4,20,4800.00)
insert into employee values(5,40,6500.00)
insert into employee values(6,40,14500.00)
insert into employee values(7,40,44500.00)
insert into employee values(8,50,6500.00)
insert into employee values(9,50,7500.00)

数据显示为

empid       deptid      salary
----------- ----------- ---------------------------------------
1           10          5500.00
2           10          4500.00
3           20          1900.00
4           20          4800.00
5           40          6500.00
6           40          14500.00
7           40          44500.00
8           50          6500.00
9           50          7500.00

需求:根据部门分组,显示每个部门的工资等级

预期结果:

empid       deptid      salary                                  rank
----------- ----------- --------------------------------------- --------------------
1           10          5500.00                                 1
2           10          4500.00                                 2
4           20          4800.00                                 1
3           20          1900.00                                 2
7           40          44500.00                                1
6           40          14500.00                                2
5           40          6500.00                                 3
9           50          7500.00                                 1
8           50          6500.00                                 2

SQL脚本:

SELECT *, Row_Number() OVER (partition by deptid ORDER BY salary desc) rank FROM employee

 
我们再来测试大数据库

几种写法分别如下:

 

 代码如下 复制代码

SELECT TOP 20 * FROM (SELECT
   ROW_NUMBER() OVER (ORDER BY Namec) AS RowNumber,
   *
FROM
   dbo.mem_member) _myResults
WHERE
   RowNumber > 10000

 

SELECT * FROM (SELECT
   ROW_NUMBER() OVER (ORDER BY Namec) AS RowNumber,
   *
FROM
   dbo.mem_member) _myResults
WHERE
   RowNumber between 10000 and 10020
 

WITH OrderedResults AS

(SELECT *, ROW_NUMBER() OVER (order by Namec) as RowNumber FROM dbo.mem_member)

SELECT *

FROM OrderedResults

WHERE RowNumber between 10000 and 10020

不管哪种写法,性能都不理想。在8,9万条数据的情况下要运行6秒左右。


2、使用临时表再加存储过程

 

 代码如下 复制代码

BEGIN
                DECLARE @PageLowerBound int
                DECLARE @PageUpperBound int
               
                -- Set the page bounds
                SET @PageLowerBound = 10000
                SET @PageUpperBound = 10020

                -- Create a temp table to store the select results
                Create Table #PageIndex
                (
                    [IndexId] int IDENTITY (1, 1) NOT NULL,
                    [Id] varchar(18)
                )
               
                -- Insert into the temp table
                declare @SQL as nvarchar(4000)
                SET @SQL = 'INSERT INTO #PageIndex (Id)'
                SET @SQL = @SQL + ' SELECT'
                SET @SQL = @SQL + ' TOP ' + convert(nvarchar, @PageUpperBound)
                SET @SQL = @SQL + ' m_id'
                SET @SQL = @SQL + ' FROM dbo.mem_member'
                SET @SQL = @SQL + ' ORDER BY NameC'
               
                -- Populate the temp table
                exec sp_executesql @SQL

                -- Return paged results
                SELECT O.*
                FROM
                    dbo.mem_member O,
                    #PageIndex PageIndex
                WHERE
                    PageIndex.IndexID > @PageLowerBound
                    AND O.[m_Id] = PageIndex.[Id]
                ORDER BY
                    PageIndex.IndexID
               
drop table #PageIndex           
                END

而使用这种方法,在同样的情况下用时只需1秒,这样看来我们就不要说了ROW_NUMBER的性能显示不如存储过程了。

补充:数据库,Mssql
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,