asp中大数据量分页代码示例

对于大量的数据分页传统的asp分页方法(Rs.PageSize)存在很大的弊端:第一次打开页面时,它会预读所有的记录集,这当在数据大的时候,这将是致命的,翻页速度也会非常慢,非常占用资源。

我在操作大量程序的时候用Rs.PageSize方法分页经常出现程序运行超时错误,(数据量在几万左右程序优化不好的情况下经常出现程序运行超时),处理这种大量数据我使用的下面的方法暂时解决:


首先是记录打开时的游标类型,由于只是对记录进行顺序遍历所以我使用用rs.open sql,conn,0,1
0,1,这应是最快的游标类型了(详细
adOpenForwardOnly 0 默认值。只支持向前顺序访问数据,如果只需要顺序遍历全部数据,该方法可以提供较高的执行性能
adLockReadOnly 1 只读方式,不能对数据进行修改
其次是每页只读出需要的记录,不预读所有的记录集

修改后的代码
<% totalrec=0 '总记录数 msg_per_page=20 '每页显示记录数 currentpage=request.QueryString("page")'当前页码 n=0'总页数 if currentpage="" then currentpage=0 '取得总记录数 set rs=server.CreateObject("adodb.recordset") sql="select count(n_id) from view_news where c_flag=5 and n_isdisp=1" rs.open sql,conn,0,1 totalrec=rs(0) rs.close if totalrec mod msg_per_page = 0 then '计算总页数 n = totalrec/msg_per_page 'n:总页数 else n = totalrec/msg_per_page+1 end if '设定当前页 if not isnumeric(currentpage) or currentpage="" then currentpage=1 If currentpage <> "" then currentpage = cint(currentpage) end if if currentpage < 1 then currentpage = 1 end if if currentpage*msg_per_page > totalrec and not((currentpage-1)*msg_per_page < totalrec) then currentPage=1 end if if currentpage>1 then '当前不是第一页 sql="select top "&msg_per_page&" n_id,n_title,n_istop,n_date,n_ispic,n_isred from view_news where c_flag=5 and n_isdisp=1 and n_id not in (select top "&(currentpage-1)*msg_per_page&" n_id from view_news where c_flag=5 and n_isdisp=1 order by n_istop desc, n_date desc,n_id asc) order by n_istop desc, n_date desc,n_id asc" else'当前是第一页 sql="select top "&msg_per_page&" n_id,n_title,n_istop,n_date,n_ispic,n_isred from view_news where c_flag=5 and n_isdisp=1 order by n_istop desc, n_date desc,n_id asc" end if rs.open sql,conn,3,1 while not rs.eof '输出信息 rs.movenext wend rs.close set rs=nothing %>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值