A theory of the fastest Web database paging method _ Application techniques

Source: Internet
Author: User
Tags web database
In this paper, we have come up with the best paging method in theory, and we'll talk about this best paging method in detail.

One: Idea.
When designing a Web database, if we want to compile every record, then only take the paging mode to make the Web database as soon as possible, rendering it to the end users, and not because of the 8-second principle of the user lost interest in browsing the page. But even with a paging approach, when there are multiple records of the database, it will inevitably make our users feel too slow to page. As I said in my last article, there are some flaws in the three pagination methods of almost every article. So, how do we get the database to fetch the records we need each time, this is a good implementation, and a cursor can return multiple recordsets, but it's hard to get the end of the database to consume a lot of resources just because it needs to retrieve a page of records. Finally, after my continuous rewriting program and test, I finally wrote the Web database paging method that I think is the fastest in theory.

Second: The specific implementation of the stored procedures.
We'll talk about this approach in conjunction with a BBS question. How to make a BBS every page only realistic need one page record? And what do we need to provide the database with those parameters? The following parameters may be available.
The first: is the number of pages we need today.
Second: The number of record sets for each page currently defined. This allows you to modify the number of records per page in the page program as needed. Of course, if you do not consider the scalability of the program, you can also directly in the database to specify that each page has n records.
Third: One output parameter: The number of total records in the current table is obtained from the database. (Note that he is not a record number of pages) he is equivalent to the RecordCount in the ADO paging method. If you don't need a total record number, you don't have to return him.
Let's look at the code for the specific stored procedure ...

CREATE PROCEDURE Dbo.pro_pageview
(

@tint_tableid Tinyint=1, this is the BBS's current layout ID, you can not care about him.
@int_pagenow int=0,
@int_pagesize int=0,
@int_recordcount int=0 Output--is to get the total number of BBS a page.

)

As
SET NOCOUNT ON

DECLARE @int_allid int
declare @int_beginid int, @int_endid int
declare @int_pagebegin int, @int_pageend int

Select @int_allid =count (*) from Tab_discuss where tint_level=0 and tint_tableid= @tint_tableid
Select @int_recordcount = @int_allid--Gets the total number of tiles for the layout

DECLARE cro_fastread cursor Scroll
For the select int_id from Tab_discuss where tint_level=0 and tint_tableid= @tint_tableid ORDER BY int_id Desc--This defines cursor operations, but not The temporary record set, and the cursor does not need to traverse all the recordset.

Open Cro_fastread--opening cursors
Select @int_beginid = (@int_pagenow-1) * @int_pagesize +1 gets the first record ID of the page
Select @int_endid = @int_beginid + @int_pagesize-1 to obtain the ID of the last record of the page

Fetch absolute @int_beginid from Cro_fastread to @int_pagebegin pass his ID to a variable the ID of the page start
If @int_endid > @int_allid-Note that if a page is less than a fixed number of pages. If there is only one page record and the record is less than the number we have defined. Or the last page ...
Fetch last from Cro_fastread into @int_pageend--directly position the cursor absolutely to the final record, and get his ID number ...
Else
Fetch absolute @int_endid from Cro_fastread to @int_pageend

Select Int_id,tint_level,tint_children,var_face,var_subject,datalength (txt_content) as Int_len,sint_hits,var_url, Var_image,var_user,dat_time,tint_tableid,bit_kernul from Tab_discuss where tint_tableid= @tint_tableid and Int_rootid Between @int_pageend and @int_pagebegin order by Int_rootid Desc,num_order desc-We can use the first ID and the last ID of the page to derive the middle ID .... Attention Our digital structure of the BBS uses a very clever algorithm, is to use a ordernum floating-point number can complete the sorting ... )

--Start to clear ...
Close Cro_fastread
Deallocate Cro_fastread

Return


Let's look at the program operation in the ASP page ...

Pagenow=cint (Request ("Pagenow")--the current page.

If Pagenow<=0 then pagenow=1
pagesize=10

Set Cmd=server. CreateObject ("Adodb.command")
Cmd. Activeconnection=strconn
Cmd.commandtype=4
cmd.commandtext= "Pro_pageview"

Cmd. Parameters.Append cmd. CreateParameter ("Tint_tableid", Adinteger,adparaminput,,tint_tableid)
Cmd. Parameters.Append cmd. CreateParameter ("Int_pagenow", Adinteger,adparaminput,,pagenow)
Cmd. Parameters.Append cmd. CreateParameter ("Int_pagesize", Adinteger,adparaminput,,pagesize)
Cmd. Parameters.Append cmd. CreateParameter ("Int_recordcount", Adinteger,adparamoutput)

Set Rs=cmd. Execute
If Rs.eof Then
Response.Write "now has exceeded the record number or the record set is empty!" "
Response.End
End If

Dim Arrrs
Arrrs=rs.getrows ' can use GetRows to quickly save a recordset to a two-dimensional array to increase speed.

Recordcount=cmd. Parameters ("Int_recordcount")
' Note that when the record is not sufficient to divide the Unit page record, we also define it as a page, if the record number is 2 pages and one record, at this time we also have to 3 pages of page records.
if (RecordCount mod pagesize) =0 Then
Pagecount=recordcount\pagesize
Else
Pagecount=recordcount\pagesize+1
End If


<--Paging Start-->
<!--#include file= "include\tablepage.asp"--> fixed paging function, in fact, is nothing more than pagenow+1 or Pagenow-1,pagenow,pagecount
<!--pagination End-->


<div align= "left" class= "Pblank" >
<%
'---------Show tree structure! -------------
Level=0
Response.Write "<ul>"
For i=0 to UBound (arrrs,2)
If Arrrs (1,i) >level Then
Response.Write "<ul>"
End If
If Arrrs (1,i) <level Then
For J=arrrs (1,i) to Level-1
Response.Write "</ul>"
Next
End If
Int_size=arrrs (5,i)
If Int_size=0 Then
Str_size= "< no content >"
Else
Str_size= ""
End If
Response.Write "<li><a href=showtitle.asp?int_id=" &arrRs ( 0,i) & "&tint_tableid=" &tint_tableid& "Class=ptitle target=boardannounce>" &server. HTMLEncode (Arrrs (4,i)) & "</a>" &str_size
If Arrrs (7,i) <> "" Then Response.Write "< connection >"
If Arrrs (8,i) <> "" Then Response.Write "< image >"
Response.Write "-" "&arrrs (9,i) &" "<font color=444444>" &arrrs (10,i) & "</font> [<font Color=920092>id: "&arrrs (0,i) &" click: "&arrrs (6,i) &" secondary </font>] <font color=444444> (" &int_size& "bytes" </font> <font color=ff0000> ("&arrrs (2,i) &") </font></li> "


Level=arrrs (1,i)

Next

Response.Write "</ul>"
'---------show tree structure finished! -------------

%>
</div>
<div align= "Left" >
<!--paging start-->
<!--#include file= "include\tablepage.asp"-->
<!--pagination End-->
</div>
<!--clearance and error-->
<%
Rs.close
Set rs=nothing
Set cmd=nothing
If Err.number<>0 then Response.Redirect "Bug.asp"
%>

Three: Characteristics
We're going to look at him and the traditional three ways the difference and characteristics
First: Only one page record is returned at a time, and only one record set is formed, and the client can use the fastest FireWire cursor to complete the page output. Instead of using Rs.nextrecordset records to output records like the traditional cursor method.
Second: The database does not use temporary tables, which is significantly faster than the dump record set.
Third: A scrolling cursor is used, and the cursor completes the positioning with only two operations. Speed is also greatly improved.

When I adopt this method of paging, I can already feel the increase of paging speed obviously. Of course, in dealing with tree structures, database operations, I used a number of methods to improve the speed as much as possible, such as: the use of the two-division interval method to compile the calendar tree structure, all the use of stored procedures to achieve all SQL operations, using triggers and database cursors to complete the database algorithm, so as to avoid excessive network transmission. Any operation can be completed only once with a parameter pass to the database.
Interested friends can come to my forum to see: Http://tec.xj169.net/bbs

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.