一个数据库表要是数据超过50万条,要做分页的话处理不好,对服务器压力很大,经常会卡死IIS,因为传统的分页办法是 读取出所有的50万条数据再去分配这些页面的分页,这样效率很低,我的办法是分页读取,比如一页是100条,那么每一页我只读出条件下的100条,这是select top 50万 和select top 100的区别,我在别人成就的基础上做了一些延伸,加上了几个条件,减少读取数据量,优化了流程。 函数如下:
<%
Function GetPageSQL(tblName,fldName,PageSize,PageIndex,OrderType,strWhere,Ziduan)
Dim strTemp,strSQL,strOrder
if OrderType=0 then
strTemp=">(select max([" & fldName & "])"
strOrder=" order by [" & fldName & "] asc"
else
strTemp="<(select min([" & fldName & "])"
strOrder=" order by [" & fldName & "] desc"
end if
if Ziduan="" then
ziduan="*"
end if
if PageIndex=1 then
strTemp=""
if strWhere<>"" then
strTmp = " where " + strWhere
end if
strSQL = "select top " & PageSize & " " & ziduan & " from [" & tblName & "]" & strTmp & strOrder
else
strSQL="select top " & PageSize & " " & ziduan & " from [" & tblName & "] where [" & fldName & "]" & strTemp & _
" from (select top " & (PageIndex-1)*PageSize & " [" & fldName & "] from [" & tblName & "]"
if strWhere<>"" then
strSQL=strSQL & " where " & strWhere
end if
strSQL=strSQL & strOrder & ") as tblTemp)"
if strWhere<>"" then
strSQL=strSQL & " And " & strWhere
end if
strSQL=strSQL & strOrder
end if
GetPageSQL=strSQL
End Function
%>
用法:
Function GetPageSQL(tblName,fldName,PageSize,PageIndex,OrderType,strWhere,Ziduan)
tblName——数据表名
fldName——排序字段(非常关键,最好是主键)
PageSize——每页的显示数据条数
PageIndex——本页的页码
OrderType——排序规则0为正序asc,1为倒序desc
strWhere——条件,就是where后面的语句
Ziduan——需要读取出来的字段
详细可看下面例子,这个例子可以单独作为文件显示出sql语句。
<%
pageno=Request.QueryString("page")
if pageno="" then pageno=1 else pageno=cint(pageno)
page=pageno
sqltext=getpagesql("comminfo","updatetime",40,page,1,"commtype='0' and state='1'","")
response. write sqltext
%>
先这样写些,函数是别人以前就有的了,我在基础上加了一些改动,对范围更缩小了一些,希望有大数据要分页的朋友能用上
注:本文发自云南电子商务研究站(http://www.xiongmaotou.com),转载请附带本说明,谢谢