您的位置:首页 > 其它

多种存储过程分页方法的速度对比

2009-11-17 22:00 260 查看
多种存储过程分页方法的速度对比

一.TempTable(临时表)

The methods in both articles could be optimized with just the Primary Key data copied to the temp table and then doing the join with the main query. Therefore, the essence of this method would be the following

CREATE
TABLE #Temp (
ID int
IDENTITY
PRIMARY
KEY,
PK  /* here goes PK type */
)

INSERT
INTO #Temp SELECT PK FROM
Table
ORDER
BY SortColumn

SELECT ... FROM
Table
JOIN #Temp temp ON
Table.PK = temp.PK ORDER
BY temp.ID
WHERE ID > @StartRow AND ID < @EndRow


The method can be optimized further by copying the rows to the temp table until the end paging row is reached (
SELECT TOP EndRow...
), but the point is that in the worst case – for a table with 1 million records you end up with 1 million records in a temp table as well. Considering all this and having looked upon the results in the article above, I decided to discard this method from my tests.

二.Asc-Desc

This method uses default ordering in a subquery and then applies the reverse ordering. The principle goes like this

DECLARE @temp TABLE (
PK  /* PK Type */
NOT
NULL
PRIMARY
)

INSERT
INTO @temp
SELECT
TOP @PageSize PK FROM (
SELECT
TOP (@StartRow + @PageSize)
PK,
SortColumn /*If sorting column is defferent from the PK, SortColumn must
be fetched as well, otherwise just the PK is necessary */
ORDER
BY SortColumn /* default order – typically ASC */)
ORDER
BY SortColumn /* reversed default order – typically DESC */
SELECT ... FROM
Table
JOIN @Temp temp ON
Table.PK = temp.PK
ORDER
BY SortColumn /* default order */


三.RowCount

The base logic of this method relies on the SQL
SET ROWCOUNT
expression to both skip the unwanted rows and fetch the desired ones:

DECLARE @Sort /* the type of the sorting column */
SET
ROWCOUNT @StartRow
SELECT @Sort = SortColumn FROM
Table
ORDER
BY SortColumn
SET
ROWCOUNT @PageSize
SELECT ... FROM
Table
WHERE SortColumn >= @Sort ORDER
BY SortColumn


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: