DB2高效分页算法

例:

select * from(select a.*,row_number() over(order by a.tableid) as rn from t1 a where exists (select 1 from t2 b where b.tabschema=a.tabschema and b.tabname=a.tabname))  where rn>10 fetch first 10 rows only optimize for 10 rows

高效的分页算法:

1. order by列必须有索引  (上面的SQL指的是tableid列)
2. 如果多表关联, 关联列上必须要有索引  (t2表:tabschema,tabname列上必须有索引)
3. 分页限制条件: rn > :x fetch first N rows only optimize for N rows

The OPTIMIZE FOR n ROWS clause indicates to the optimizer that the application intends to retrieve only n rows, but the query will return the complete result set. The FETCH FIRST n ROWS ONLY clause indicates that the query should return only n rows.

The DB2 data server does not automatically assume OPTIMIZE FOR n ROWS when FETCH FIRST n ROWS ONLY is specified for the outer subselect. Try specifying OPTIMIZE FOR n ROWS along with FETCH FIRST n ROWS ONLY, to encourage query access plans that return rows directly from the referenced tables, without first performing a buffering operation such as inserting into a temporary table, sorting, or inserting into a hash join hash table.

Applications that specify OPTIMIZE FOR n ROWS to encourage query access plans that avoid buffering operations, yet retrieve the entire result set, might experience poor performance. This is because the query access plan that returns the first n rows fastest might not be the best query access plan if the entire result set is being retrieved.

时间: 2024-08-02 04:44:51

DB2高效分页算法的相关文章

C#高效分页代码(不用存储过程)

首先创建一张表(要求ID自动编号): create table redheadedfile ( id int identity(1,1), filenames nvarchar(50), senduser nvarchar(50), primary key(id) ) 然后我们写入50万条记录: declare @i int set @i=1 while @i<=500000 begin insert into redheadedfile(filenames,senduser) values(&qu

高效分页 asp

<% '每页的记录数 dim pagesize pagesize= "30" '读出总记录数,总页数,作者注 Dim TotalRecords,TotalPages SQLstr="Select count(id) As RecordSum From table1" Set Rs=conn.Execute(SQLstr,0,1) TotalRecords=Rs("RecordSum") if Int(TotalRecords/pagesiz

MySQL分页优化中的“INNER JOIN方式优化分页算法”到底在什么情况下会生效?

本文出处:http://www.cnblogs.com/wy123/p/7003157.html 最近无意间看到一个MySQL分页优化的测试案例,并没有非常具体地说明测试场景的情况下,给出了一种经典的方案,因为现实中很多情况都不是固定不变的,能总结出来通用性的做法或者说是规律,是要考虑非常多的场景的,同时,面对能够达到优化的方式要追究其原因,同样的做法,换了个场景,达不到优化效果的,还要追究其原因.个人对此场景在不用情况表示怀疑,然后自己测试了一把,果然发现一些问题,同时也证实了一些预期的想法.

Access大数据高效分页语句

oracle的分页查询可以利用rowid伪列. db2的分页查询可以利用row_number() over()聚合函数. mysql有limit. access仿佛先天缺陷,仅提供了top n.那如何利用top来实现分页查询呢? 假设在access中有表t1 createtable t1( tc1 varchar(50)notnullprimarykey, tc2 varchar(30), tc3 varchar(30) ) 随机插入20条数据.如果以每页5条来显示数据,如果要显示11至15条如

ListView的高效分页

需要在DataSet中设定两个sql查询 1.根据参数查询指定页的数据 select *from ( select id,name,age,over(order by id)rownam from table where id>30 )t where t.rownam>@startRowIndex and t.rownam<[email protected][email protected] 这里注意!因为DataSet编辑器对over()函数不支持,所以要手动添加parameter设定

在 asp.net mvc中的简单分页算法

//第一步:建立如下分页实体类:namespace MVCPager.Helpers { /// <summary> /// 简单分页算法类 /// </summary> public class Pager { public int RecordCount { get; set; } public int PageIndex { get; set; } public int PageSize { get; set; } public int PageCount { get { r

针对范围对的高效查找算法设计(不准用数组)

题目链接在:针对一群范围对的最快查找算法设计(不要用数组),是我目前遇到的一个较棘手的问题. 描述如下: 假如有一群范围对,格式为:<范围表示,该范围对应的结果值>,设计一个最快查找算法,使得给定一个值,输出该值所在范围对的结果值. 注意:范围对之间没有交集,即不可能存在<1, 10>和<2, 11>这样的两个范围对. 例如有以下几个范围对: <<1, 2>, 20> <<3, 37>, 27> <<48, 5

sql server 2000 单主键高效分页存储过程 (支持多字段排序)

sql server 2000 单主键高效分页存储过程 (支持多字段排序) Create PROC P_viewPage             /*              nzperfect [no_mIss] 高效通用分页存储过程(双向检索) 2007.5.7  QQ:34813284              敬告:适用于单一主键或存在唯一值列的表或视图              ps:Sql语句为8000字节,调用时请注意传入参数及sql总长度不要超过指定范围            

常用的SQL分页算法及对比

SQL Server 2005引入的新方法. 1 SELECT * FROM (SELECT ROW_NUMBER() OVER(ORDER BY keyField DESC) AS rowNum, * FROM tableName) AS t WHERE rowNum > start[比如:90] AND rowNum <= end[比如:100]=>[返回91-100] 2 3 SELECT top (PAGESIZE[比如:10]) FROM (SELECT ROW_NUMBER(