When opening an asp page, the page needs to access the database, and then display the content that meets the requirements on the page. If there are many records in the database, the access speed of the page will be relatively slow. The following methods can be used to improve the access speed of the page.
Tip one: Improve the efficiency of using the Request collection
Accessing an ASP collection to extract a value is a time-consuming, computationally intensive process. Because this operation involves a series of searches through related collections, it is much slower than accessing a local variable. Therefore, if you plan to use a value in the Request collection multiple times on the page, you should consider storing it as a local variable.
For example, write the code in the following form to speed up script engine processing:
Copy the code code as follows:
strTitle=Request.Form("Title")
strFirstName=Request.Form("FirstName")
strLastName=Request.Form("LastName")
If Len(strTitle) Then strTitle=strTitle & " "
If strFirstName="" Then strFullName=strTitle & " " & strLastName
Elseif Len(strFirstName)=1 Then
strFullName=strTitle & strFirstName & ". " & strLastName
Else
strFullName=strTitle & strFirstName & " " & strLastName
End If
Tip 2: Access the appropriate collection directly
If you have no choice, do not use strPage=Request("page") to get parameters, because this will search the entire collection in order - QueryString, Form, Cookies, ClientCertificate, ServerVarible until the first match is found The name of the value. Doing so is less efficient than accessing the appropriate collection directly, and is unsafe unless there is absolute guarantee that the value will not appear in another collection.
For example, you might want to search for the name of a WEB server that fulfills a client's request. This is accomplished by looking for "SERVER_NAME" in the Request.ServerVarables collection that appears in each query. However, if other collections also contain values named "SERVER_NAME" (key names are not case-sensitive), you will get incorrect results when using Request("server_Name"). In summary, the appropriate collection should be accessed directly whenever possible.
Tip 3: Use the Response.IsClientConnected property before time-consuming operations
Using Response.IsClientConnected is a useful way to observe whether the user is still connected to the server and loading the web page created by ASP. If the user disconnects or stops downloading, we no longer have to waste server resources creating the web page because the buffer contents will be discarded by IIS. So, for pages that require a lot of time to compute or use a lot of resources, it's worth checking at every stage whether the visitor is offline:
Copy the code code as follows:
… Code to create first part of the page
If Response.IsClientConnected Then
Response.Flush
Else
Response.End
End If
… Code to create next part of page
Tip 4: Optimize ADO operations in ASP
Generally speaking, data constitutes the actual content of a WEB site. Therefore, it is very useful to optimize ADO operations to speed up ASP code execution:
a. Select only the required columns: When opening an ADO recordset, you should not automatically use the table name (ie SELECT *) unless you need to obtain all columns. Using separate columns means that the amount of data sent to or fetched from the server is reduced. Even if you need to use all columns, naming each column individually will achieve the best performance, because the server does not have to interpret the column names.
b. Use stored procedures whenever possible. Stored procedures are precompiled programs that contain a prepared execution plan, so they execute faster than SQL statements.
c. Use appropriate cursor and lock patterns. If all you do is read data from the recordset and display it on the screen, then use the default forward-only, read-only recordset. The less work ADO has to do to maintain the details of records and locks, the higher the performance it performs.
d. Use object variables. A sure way to improve performance when iterating over a recordset is to use object variables to point to members of the collection. For example:
Copy the code code as follows:
While Not RsGc.EOF
Response.Write "Project name:" & RsGc("GcMC") & "(Project code:" & RsGc("GcCode") & ")"
RsGc.MoveNext
Wend
This can be rewritten as the following code to speed up execution:
Copy the code code as follows:
set GcMc=RsGc("GcMc")
set GcCode=RsGc("GcCode")
While Not rsGc.EOF Response.Write "Project name:" & GcMc & "(Project code: " & GcCode & ")"
RsGc.MoveNext
Wend
The new code establishes a reference to the object variable, so the object variable can be used instead of the actual variable, which means the script engine has less work because there are fewer indexes into the collection.
Tip 5: Don’t mix script engines
We know that both VBScript and JScript can be used in ASP pages. But it is not advisable to use JScript and VBScript at the same time on the same page. Because the server must instantiate and attempt to cache two (rather than one) script engines, this increases the load on the system to some extent. Therefore, for performance reasons, you should not mix multiple script engines on the same page.