ASP's efficient paging algorithm [one is the traditional ADO paging, the second is the select top paging algorithm. ]__ algorithm

Source: Internet
Author: User
Tags php language

There are also contacts for the program paging algorithm. In general, there are two kinds of paging algorithms, one is the traditional ADO paging, the other is the select top paging algorithm. For small data tables, such as ten thousand or twenty thousand of the amount of data table, I tend to use the ADO algorithm, for large data tables, you must adopt the latter algorithm.     Powered by 25175.net First, the traditional ADO paging algorithm.     This algorithm, easy to use, easy to start, for the careful database is preferred, its implementation efficiency is very high, the database with the function of the cursor when the page is also very convenient. The code typically used is as follows:

<%
Dim recordcountnum,page,i,j
listnum =     ' show number of records per page
sql= ' Select  id,title,time from table1 order by time desc "
Set rs =  server.createobject  ("ADODB.") Recordset ")
Rs. open sql,conn,1,1
if rs.eof and rs.bof then 
Else
recordcountnum= Rs.recordcount
rs.pagesize = listnum  
Page = request ("page")
If   page =  ""  or page < 1 then
page=1
end if
if  ( Page-rs.pagecount)  > 0 then
Page=rs.pagecount
end if
Rs.absolutepage=page
J =rs.recordcount
j=j-(page-1) *listnum
i=0
Do while not rs. Eof and i<listnum
response.write  per record information: &rs ("id") & "<br>"
i=i+1
Rs.movenext
Loop
' page-Flip code ...
%>

The paging algorithm of this ADO cursor, the use of this paging method is extremely inefficient when the data is very large, because the data table's full data will be re-read every time the page is loaded, although the cursor is used very simply and can be used when the data table is small in size.

So, we need to introduce another efficient select top paging algorithm. The code is as follows:


<%
' Number of records per page
Dim pagesize
Pagesize= "30"

' Read the total number of records, total pages, floating easy note
Dim totalrecords,totalpages
Sqlstr= "Select count (ID) as recordsum from table1"
Set Rs=conn. Execute (sqlstr,0,1)
Totalrecords=rs ("Recordsum")
If Int (totalrecords/pagesize) =totalrecords/pagesize Then
Totalpages=totalrecords/pagesize
Else
Totalpages=int (totalrecords/pagesize) +1
End If
Rs.close
Set rs=nothing

' current page number, floating note
Dim page
page=request (' page ')
If isnumeric (page) =false then
response.write  "<script language=javascript>alert (' parameter error. ');"
response.write  "Window.close ();</script>"
Response.End
end if
if page= ""  or page<1 then page=1
If page-totalpages>0 then page=totalpages
Page=int (page)

If Page=1 Then
Sql= ' select top ' &pagesize& ' id,title,time from table1 ORDER by time Desc '
Else
Sql= ' select top ' &pagesize& ' id,title,time from table1 where time< (select Min. from (select top) &page Size* (page-1) & "time from table1 ORDER by time Desc" as T) Order by time Desc "
End If
Set rs = Server.CreateObject ("ADODB.") Recordset ")
Rs. Open sql,conn,1,1
Do but not Rs. Eof
Response.Write per record information: &rs ("id") & "<br>"
Rs.movenext
Loop
Rs.close
Set rs=nothing
' Page-flipping code omitted ...
%>

This is a very efficient page-splitting algorithm. When the data in the data table is hundreds of millions of times, the response time for this paging algorithm is very short, usually within dozens of milliseconds. The principle is very simple, that is, every paging, I only take the required dozens of records, the use of select top is based on this consideration.

In the example of the two pagination algorithms above, Flymorn uses time field times to sort by, because in most of the systems I've contacted, we need to show the user's recent updates (including newly added records and newly modified old records) in the front, If you only use an AutoNumber ID as the sort field, the old information that the user edits will not be displayed in front of you. That's why Flymorn uses the Time field.

This also involves the problem of aggregating indexes. By default, we are using an AutoNumber ID as the primary key, and as an aggregation index column, if the above algorithm uses such an ID column to sort, the efficiency will be higher, the database response time will be less; however, I mentioned that the recently updated content needs to be shown in front of the question, so, We must use the Time field to sort. Therefore, for a higher paging efficiency, we can design this time field as an aggregated indexed column when designing the database.

Through this design, the entire paging efficiency will be very high increase.

However, there is another small problem with this time field as an aggregated indexed column. Because the data table is in the arrangement of data, is sorted according to the aggregated indexed columns, when the user adds the data, there is no problem at the end of the datasheet; When the user edits the information, the database needs to follow the aggregated index column, and the information that has just been edited is also mentioned at the end of the table. It's going to take some time. That is, when we use the Time field to aggregate indexed columns, we need to spend a little more time on the UPDATE data.

However, in terms of comprehensive comparisons, drift easy to think, SELECT top efficient paging algorithm is the key to avoid full table scan, as far as possible to get only the required fields, sorted field is best to aggregate indexed columns, practice shows that the aggregated indexed columns to sort the SQL statement response time is the fastest. After this processing, for the SQL Server database, even tens of millions of data, there is no need to fear the paging algorithm has lost its response.

The above is an example of an ASP language to write algorithms, of course, can also be transformed into other such as asp.net,php language used. In order to better use such paging code, you can also rewrite the above algorithm into a stored procedure.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.