打印

[asp] asp 超强分页类 比传统分页提高速度100倍

<%
'百万数据超强分页类~
'比传统分页提高速度100倍
'只使用 “select * from xxxx”格式的查询语句
'使用方法:把这个文件 include 到你要进行分页的页面
' Dim Pl 定义变量,任意定义
' Set Pl =  New AoaobPage 建立类的实例
' Pl.pagesize = 25 '每页显示个数
' Pl.SetPageTag = "id" '这是所有数据不会重复的数据库字段,一般为主键
' Pl.getconn = Db.Conn '这是数据库链接对象,根据自己的情况定义。
' Pl.getsql = Sql
' set Rs = Pl.getrs()'返回分页数据集
' For i=1 to 25
' If Rs.Eof Then Exit For
'  ......
' Next
' set Pl = Nothing
'版权所有 http://www.aoaob.com 欢迎转载~
'联系邮件 webmaster@aoaob.com



Const L_First="<font face=""webdings""><img src=""/images/to_r1_c1.gif"" border=""0"" /></font>" '定义第一页按钮显示样式
Const L_Prev="<font face=""webdings""><img src=""/images/to_r1_c2.gif"" border=""0"" /></font>" '定义前一页按钮显示样式
Const L_Next="<font face=""webdings""><img src=""/images/to_r1_c3.gif"" border=""0"" /></font>" '定义下一页按钮显示样式
Const L_Last="<font face=""webdings""><img src=""/images/to_r1_c4.gif"" border=""0"" /></font>" '定义最后一页按钮显示样式
Class AoaobPage
Private Conn
Private Rs
Private Sql
Private PageTag
Private ThisPageSize '一页数据量
Private RsNum '数据总量
Private PageNum '页数
Private ErrInfo
Private PageUrl
Private ThisPage '当前页面值
Private IsRs '数据是否已经初始化
Private ShowPage_info_ '分页导航缓存
Public GetCurPageNum
Public Int_CurPage
Private Sub Class_Initialize '类初始化
  ThisPageSize = 10 '默认每页显示个数
  GetCurPageNum = 10
  PageTag = ""
  ShowPage_info_ = ""
  IsRs = false
  If Request("page")="" Then
   ThisPage=1
  ElseIf not(IsNumeric(Request("page"))) Then
   ThisPage=1
  ElseIf CInt(Trim(Request("page")))<1 Then
   ThisPage=1
  Else
   ThisPage=CInt(Trim(Request("page")))
  End If
  Int_CurPage = ThisPage
End Sub

'-----------------获取分页标识
Public Property Let SetPageTag(intvalue)
  If intvalue <> "" Then
   PageTag = intvalue
  Else
   ErrInfo=ErrInfo & "分页标识的参数不正确"
   ShowError()
  End If
End Property

'-----------------
Public Property Let PageSize(intvalue)
  If IsNumeric(intvalue) Then
   ThisPageSize=CLng(intvalue)
   GetCurPageNum = ThisPageSize
   If ThisPageSize < 1 Then ThisPageSize = 10
  Else
   ErrInfo=ErrInfo & "PageSize的参数不正确"
   ShowError()
  End If
End Property

'---------------返回每页个数
Public Property Get PageSize
  If ThisPageSize="" or (not(IsNumeric(ThisPageSize))) Then
   PageSize=10  
  Else
   PageSize=ThisPageSize
  End If
End Property

'----------------得到数据库链接
Public Property Let GetConn(sconn)
  Set Conn=sconn
End Property

'-----------------------得到Sql语句
Public Property Let GetSQL(svalue)
  Sql=Lcase(svalue)
End Property

'-------------------打开数据库
Private Sub OpenRs()
  Set Rs = Server.CreateObject("Adodb.RecordSet")
  Rs.Open Sql,Conn,1,1
End Sub
  
Public Property Get GetRs()
  If Sql = "" Then
   ErrInfo=ErrInfo & "Sql语句错误"
   ShowError()
  End If
  Dim RsNumSql,RsNum_
  RsNumSql = Replace(Sql,"*","count(*)")
  If InStr(RsNumSql," order by ") > 0 Then RsNumSql = left(RsNumSql,InStrRev(RsNumSql," order by ") -1)
  If Not IsObject(Conn) Then
   ErrInfo=ErrInfo & "数据库链接对象错误!"
   ShowError()
  End If
  
  
  set RsNum_ = Conn.Execute(RsNumSql)
  RsNum = RsNum_(0)
  RsNum_.Close
  Set RsNum_ = Nothing
  If RsNum mod ThisPageSize = 0 Then
   PageNum = RsNum \ ThisPageSize
  Else
   PageNum = RsNum \ ThisPageSize + 1
  End If
  If ThisPage > PageNum Then ThisPage = PageNum
  If(PageTag = "") Then
   ErrInfo=ErrInfo & "分页标识的参数不正确!"
   ShowError()
  End If
  If ThisPage = 1 Then
   Sql = Replace(Sql,"*","Top "&ThisPageSize&" *")
  ElseIf ThisPage > 1 Then
   Sql = Replace(Replace(Sql,"*","Top "&ThisPageSize&" *"),"where","where "&PageTag&" not in ("&Replace(Sql,"*","Top "&(ThisPageSize*(ThisPage-1))&" "&PageTag)&") and")
  End If
  Call OpenRs()
  IsRs = true
  Set GetRs = Rs
End Property


Public Function ShowPage_info()
  Dim result,i,m
  PageUrl = GetUrl()
  If Not IsRs Then
   ErrInfo=ErrInfo & "数据执行错误!"
   ShowError()
  End If
  If ShowPage_info_ <> "" Then
   ShowPage_info = ShowPage_info_
   Exit Function
  End If
  result = ""
  If PageNum > 0 Then
   If PageNum = 1 Then
   Else
    '第一页按钮
    If ThisPage = 1 Then
     result = result & L_First & vbCrLf
    Else
     result = result & "<a href="""&PageUrl&"1"">"&L_First&"</a>" & vbCrLf
    End If
    '上一数字列按钮
    If ThisPage - 9 > 0 Then
     result = result & "<a href="""&PageUrl&ThisPage-9&""">"&L_Prev&"</a>" & vbCrLf
    Else
     If ThisPage = 1 Then
      result = result & L_Prev & vbCrLf
     Else
      result = result & "<a href="""&PageUrl&"1"">"&L_Prev&"</a>" & vbCrLf
     End If
    End If
    '数字列按钮
    If ThisPage > 5 and ThisPage + 5 < PageNum Then
     For i = 1 to 9
      m = ThisPage - 5 + i
      If m > 0 and m <= PageNum Then
       If m = ThisPage Then
        result = result & "<strong  class=""divline_hover"">&nbsp;"&m&"&nbsp;</strong>&nbsp;"&vbCrLf
       Else
        result = result & "<a href="""&PageUrl&m& """ class=""divline"">&nbsp;"&m&"&nbsp;</a>&nbsp;"&vbCrLf
       End If
      End If
     Next
    ElseIf ThisPage <= 5 and ThisPage + 5 < PageNum Then
     For i = 1 to 9
      If i <= PageNum Then
       If i = ThisPage Then
        result = result & "<strong  class=""divline_hover"">&nbsp;"&i&"&nbsp;</strong>&nbsp;"&vbCrLf
       Else
        result = result & "<a href="""&PageUrl&i& """ class=""divline"">&nbsp;"&i&"&nbsp;</a>&nbsp;"&vbCrLf
       End If
      End If
     Next
    ElseIf ThisPage + 5 >= PageNum Then
     For i = 1 to 9
      If PageNum-9+i > 0 Then
       If PageNum-9+i = ThisPage Then
        result = result & "<strong  class=""divline_hover"">&nbsp;"&PageNum-9+i&"&nbsp;</strong>&nbsp;"&vbCrLf
       Else
        result = result & "<a href="""&PageUrl&PageNum-9+i& """ class=""divline"">&nbsp;"&PageNum-9+i&"&nbsp;</a>&nbsp;"&vbCrLf
       End If
      End If
     Next
    End If
    '下一数字列按钮
    If ThisPage + 9 < PageNum Then
     result = result & "<a href="""&PageUrl&ThisPage+9&""">"&L_Next&"</a>" & vbCrLf
    Else
     If ThisPage = PageNum or ThisPage + 5 > PageNum Then
      result = result & L_Next & vbCrLf
     Else
      result = result & "<a href="""&PageUrl&PageNum&""">"&L_Next&"</a>" & vbCrLf
     End If
    End If
    '最后一页按钮
    If ThisPage = PageNum or ThisPage + 5 > PageNum Then
     result = result & L_Last & vbCrLf
    Else
     result = result & "<a href="""&PageUrl&PageNum&""">"&L_Last&"</a>" & vbCrLf
    End If
    result = result & "&nbsp;"&ThisPage&"/"&PageNum&"&nbsp;总共:"&RsNum&"&nbsp;"&ThisPageSize&"/页"&vbCrLf
   End If
  Else
   result = "没有您要找的数据"
  End If
  ShowPage_info_ = result
  ShowPage_info = result
End Function


Private function GetURL()
  Dim strUrl,tmp_URL,i,j,search_str,result_url,str_params,str_lparams,str_rparams
  search_str="page="
  strUrl=Request.Servervariables("URL")
  strUrl=split(strUrl,"/")
  i=UBound(strUrl,1)
  tmp_URL=strUrl(i)'得到当前页文件名
  str_params=replace(Trim(Request.Servervariables("QUERY_STRING")),"&_=","")
  If str_params="" Then
   result_url=tmp_URL & "?page="
  Else
   If InstrRev(str_params,search_str)=0 Then
    result_url=tmp_URL & "?" & str_params &"&page="
   Else
    j=InstrRev(str_params,search_str)-2
    If j=-1 Then
     result_url=tmp_URL & "?page="
    Else
     str_lparams=Left(str_params,j)
     str_rparams=right(str_params,len(str_params)-j-1)
     if InStr(str_rparams,"&")<>0 then
      str_rparams=right(str_rparams,len(str_rparams)-InStr(str_rparams,"&")+1)
     else
      str_rparams = ""
     end if
     result_url=tmp_URL & "?" & str_lparams&str_rparams&"&page="
    End If
   End If
  End If
  GetURL=result_url
End function

'--------------------销毁类
Private Sub Class_Terminate  
  Obj_Rs.close
  Set Obj_Rs=nothing
  Obj_Conn.close
  set Obj_Conn = nothing
End Sub

'------------------错误提示
Private Sub ShowError()
  If ErrInfo <> "" Then
   Response.Write("" & ErrInfo & "")
   Response.End()
  End If
End Sub

End Class
%>
www.aoaob.com

TOP

顶~~~~~~~
www.aoaob.com

TOP

真有这么强?
已经保存下来了,一会试试

TOP

select * from

这样的速度最慢也叫强????

演示呢?

把记录加到500万看看,这样只针对MSSQL对ACCESS就不行了

TOP

www.aoaob.com

TOP

楼主给的链接看不出来有多快,不过 top , not in 等sql关键字通常都是需要优化的。
修行的魔法师

TOP

那是网速的问题~!
我这百兆独享带宽 比以前传统的 方式 速度提高了 100 倍!
用timer 计算的
www.aoaob.com

TOP

select * 号是比较慢,但这只涉及到 每页显示条数的 * 数据,所以可以忽略
要不再快,你啥都没提取,要这干啥!
www.aoaob.com

TOP

后面的 not in 没有涉及 select *
www.aoaob.com

TOP

分页我个人认为主要是化整为零,

百万化成10000多个数据表,每个数据表100条,然后用程序连起来
[wma]http://jie.splan.cn/ggldl/梅艳芳.mp3[/wma]

TOP

分页类貌似不适合多表联合用...

TOP

而且特别讨厌把html代码嵌入到asp代码里的,特别是在分页函数或分页类里....

TOP

引用:
分页类貌似不适合多表联合用...
可以的,比如1,2,3,4表

1表分页(10条),分到最后不够10条自动打开2表补够10条

也就是说打开的表最多只有2个,200条记录而以
[wma]http://jie.splan.cn/ggldl/梅艳芳.mp3[/wma]

TOP

这样做要做个统计表,根据统计表里的数据分页,打开指定的表
[wma]http://jie.splan.cn/ggldl/梅艳芳.mp3[/wma]

TOP

此分页类 不适合多表联用
www.aoaob.com

TOP

谁有更成熟的分页方法,不妨分享出来!
www.aoaob.com

TOP

楼上所说比传统快100倍,如何测得?

TOP

以前做过,数据库 是20000条记录,时间相差100倍 ,不信自己试试!
www.aoaob.com

TOP

我知道有个叶子分页类,你们怎么看。
http://www.eastled.com   东方LED网

TOP

看下效果如何
7cn7.com

TOP

楼主的分享精神非常好。但很不务实和严谨,一般不爱去批评人,但这次要批评一次。

1、640条的记录,和百万记录,是天堂和地狱的差别,请真正引入百万数据后再喊出“100倍”。

2、不要再去讨论“ select * ” 的效率了,几乎全中国的网页技术人员都同意这个是最没效率的,除了楼主你一个认为这个有效率——当然,你可能是基于640条记录来说,看不出差别。想要效率,最起码得指定字段“select A,b。。。FROM..”,等数据到了百万后,用union都比你这个select *快的多。

3、之前已经有“叶子分页法”和其它出色的分页法,不妨参考一下人家的那个效率。

4、在你做这个实验时,建议先去掉图片,图片载入的速度,是网速问题,不是程序问题。把图片去掉后,就可以真刀实枪地测程序。

TOP

没什么特殊的 用access实现百万数据 真有想法 针对分页我想说几句,几乎所有的分页方法在翻过很多页后会引起效率降低,google也是一样 只不过反向代理后缓存了结果

TOP

引用:
原帖由 gjr1 于 2007-9-10 01:14 发表
楼主的分享精神非常好。但很不务实和严谨,一般不爱去批评人,但这次要批评一次。

1、640条的记录,和百万记录,是天堂和地狱的差别,请真正引入百万数据后再喊出“100倍”。

2、不要再去讨论“ select * ...
你用百万条记录测试了吗?我测试了。老毛说的,没有调查就没有发言权。我所说的100倍,是至少有20000条记录的情况下,和最传统的asp分页方法的比较。或许你有更好的方法,那就不是100倍了,或许是1000倍。我只要求100倍。而已。
www.aoaob.com

TOP

这个类,最后形成的sql语句是
select Top 25 * from xxxx where 1 = 1 order by coolsite_id desc
这是第一页的情况
select Top 25 * from xxxx where CoolSite_id not in (select Top 125 CoolSite_id from aoaob_coolsite where 1 = 1 order by coolsite_id desc) and 1 = 1 order by coolsite_id desc
这是其他页的情况,
现在就讨论这两个语句的效率就可以了!
www.aoaob.com

TOP

引用:
原帖由 aoaob 于 2007-9-10 09:45 发表
这个类,最后形成的sql语句是
select Top 25 * from xxxx where 1 = 1 order by coolsite_id desc
这是第一页的情况
select Top 25 * from xxxx where CoolSite_id not in (select Top 125 CoolSite_id fro ...
这是这个类,最核心的部分。欢迎讨论!
www.aoaob.com

TOP

呵呵,很久没动asp了,分页性能提高后整个网页显示能快多少?
回去试试
代码要像美女的裙子

TOP

引用:
原帖由 hutchin 于 2007-9-10 09:57 发表
呵呵,很久没动asp了,分页性能提高后整个网页显示能快多少?
回去试试
要是没图片的话,感觉不到卡
www.aoaob.com

TOP

如果每页只显示10条,那么对性能的提升没有多大效果,每页100条的时候才可能看出来速度的提升
代码要像美女的裙子

TOP

复制内容到剪贴板
代码:
If ThisPage = 1 Then
   Sql = Replace(Sql,"*","Top "&ThisPageSize&" *")
  ElseIf ThisPage > 1 Then
   Sql = Replace(Replace(Sql,"*","Top "&ThisPageSize&" *"),"where","where "&PageTag&" not in ("&Replace(Sql,"*","Top "&(ThisPageSize*(ThisPage-1))&" "&PageTag)&") and")
这个代码思路,所谓的:select top xxxxxxx where xx not in(select top xxxx)吗?
嗯,楼主,你的2万条记录还不够多。
新一代四无新人……博客 | 相册
努力三件事:赚钱,技术,语言。
缺钱花了,求项目做,想低价或想比价的请勿扰。

TOP

思路是这样的,
www.aoaob.com

TOP