多种存储过程分页方法的速度对比
2009-11-17 22:00
302 查看
多种存储过程分页方法的速度对比
一.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 ROWCOUNTexpression 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 |
相关文章推荐
- 多种存储过程分页方法的速度对比
- 多种存储过程分页方法的速度对比
- 两种SQL分页方法存储过程和游标存储过程
- 这个存储过程执行的速度还不错.500W速度分页只要2秒
- SQL 简单的存储过程分页 改写方法
- 通用分页存储过程注入问题解决方案:不用存储过程,通用分页查询方法
- 邹建的分页存储过程,呵呵速度不错啊~!
- java+oracle的存储过程开发案例(包含了oracle存储过程的通用分页方法、java的工厂类)
- 原创:SQL Server的通用分页存储过程,未使用游标,速度更快!
- 【转载】Sql Server2005不同分页存储过程的性能对比
- 存储过程分页方法3种(转贴)
- SQL存储过程分页若干方法
- (转)几种常用存储过程分页方法
- SQL存储过程分页另一方法
- 常见存储过程分页PK赛——简单测试分析常见存储过程分页速度
- SQL SERVER 存储过程分页的3种通用方法
- SQL 分页存储过程 以及SQL concatenate 几种方法
- 原创:SQL Server的通用分页存储过程,未使用游标,速度更快!
- 存储过程的分页(对比)