首页 > 新闻

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

时间:2009-11-17 23:30:56  作者:  我要投稿
Linux初探欢迎您的投稿,投放方法请点击这里查看,我们会定期赠送精美小礼品给优秀的投稿作者。海纳百川 取则行远!LinuxGoo欢迎您的到来。
多种存储过程分页方法的速度对比一.TempTable(临时表)The methods in both articles could be optimized with just the Primary Key data copied to the temp ta......

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

一.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
如果您需转载 多种存储过程分页方法的速度对比,请注明来自LinuxGoo.com,其版权归原作者所有。请广大网友留言时遵纪守法,使用文明用语。如果您在应用中有什么问题,请在下面留言,我们会尽快解答。
来顶一下
近回首页
返回首页
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表
相关文章
栏目热门