Fully optimized ASP Application Performance

Source: Internet
Author: User
ASP is not a scripting language. It only provides an environment for running scripts embedded in HTML pages. VBScript is the most commonly used scripting language in ASP. Although the ASP script language is very simple, it is not a simple task to optimize the operation of an ASP program.

At present, the domestic network bandwidth is very limited and the network is very crowded. How to make your ASP application run quickly has become the dream of every ASP programmer. Follow me to accelerate your ASP program!

I. Optimization Methods for database operations

The main purpose of ASP is to operate databases. How can we perform these operations more quickly?

1. Do not use "select *......"

Please try to pick up the fields you need. For example, a table contains 10 fields, but you only use one of the fields (name ), select name from yourtable instead of select * From yourtable ". You may say that I did this. But if a table contains 50 fields and you need to use 23 of them, what would you do? To save the trouble of typing and searching for the corresponding field names, you may not always use "Select name, sex, age... from yourtable!

It turns out that it is best to pick up the fields you need to use the SELECT statement, which will accelerate your ASP program by at least 5%.

2. Try to use system stored procedures (for ms SQL Server)

Sometimes you can complete a read operation by using SQL statements and stored procedures, but using stored procedures will greatly speed up reading operations, this increases the speed of your ASP program running.

3. Pay attention to the usage of your cursor

If you only read a table, use the forward-only and read-only cursbecause the cursor is the fastest way to read the database, especially when you read a large amount of data.

4. Do not open useless independent Record Sets

Maybe you are smiling. Will I open a useless record set? Yes, of course you will. For example, when you generate a tree record set, you have to open the parent record set and the corresponding sub-record set, and even the sun record set, in fact, you can use the data shaping technology provided by ADO to open multiple independent record sets, which will speed up program operation. (For data shaping usage, refer to ADO help)

5. Remember to close the opened record set object and connection object.

Some of my friends are wondering why my ASP program runs fast at the beginning, but it gets slower and slower when I run it several times? Even the server crashes. In this case, it is likely that you opened too many record set objects and connection objects, but did not close them at last. Use the following method to disable it:

Yourrecordset. Close

Set yourrecordset = nothing

Set yourconnection = nothing

6. Comparison of Methods for obtaining database data

How did you get the record set data? Do you want to use yourrecordset (field number) or
Yourrecordset ("field name ")? In fact, there are other usage methods. Now let's make a comparison (100 records ):

RS ("field name ")

RS ("field name"). Value

RS ("field number ")

Set Method

Database Response Time

2.967 seconds

2.936 seconds

1.650 seconds

0.586 seconds

2.824 seconds

2.914 seconds

1.611 seconds

0.602 seconds

2.893 seconds

2.943 seconds

1.613 seconds

0.594 seconds

Average Response Time

2.895 seconds

2.931 seconds

1.625 seconds

0.594 seconds

Now, you can understand the first three methods. I will explain how to use the fourth method (Set Method:

Dim strsql

Strsql = "Select name, sex, age from yourtable"

Dim rs

Set rs = server. Createobject ("ADODB. recordset ")

Rs. Open strsql, Conn, 1, 1

Const fieldsorder = 2

Dim objorder

Set objorder = RS (fieldsorder)

Response. Write objorder 'set Method

Ii. Optimization Methods for using ASP built-in objects

1. Minimize the use of session objects and application objects

Although the two objects provided in ASP are of great help to our programming, they should be reasonably used and should not be abused. Because a large amount of use of these two objects will greatly increase the burden on the server and seriously consume system resources. It will also make your ASP program run slowly.

2. Close unused objects (especially session and Application) in time)

If you do not close your objects in time, the system will slow down. You may ask,
Can the session and application disappear automatically? Completely correct. The system automatically triggers session_onend and application_onend events without any operation within 30 minutes by default, but a large number of users frequently read servers, the server will keep those that are useless for a long time
Session, application object. It would be unimaginable if the used session and application are not closed in time.

The closing method is as follows:

Set object = nothing

Iii. Rational Use of include files

What we are talking about here is to use <! -- # Include file = "XXX" --> contains files in the form of ASP programs. In other words, you can put some common functions into a file, and include it on other pages that may call the function.

We recommend that you do not put all functions in one include file, because when you include this file on other pages, the server needs to be precompiled, it is very likely that there are hundreds of functions in a file, and you just want to use one of the functions, which is worth the candle. Therefore, try to split your contained files into multiple small contained files. In this way, the program running speed can also be improved.

Iv. Optimization Methods for VBScript language

1. Try to use system functions instead of functions compiled by yourself.

For example, if you want to split a regular string ("Sss, DDD, fff, ggg"), you don't have to use any mid (), instr, and other functions for analysis, in fact, VBScript provides a function split (), which saves time and increases the speed. Why not?

2. Reduce the use of dynamic arrays

3. Try to develop the habit of declaring variables in advance

Don't underestimate this article. Declaring variables in advance will speed up the execution time of the program's interpretation. On the contrary, it is difficult to read programs without declaring variables, and the execution efficiency of the entire program on the server will be greatly reduced.

V. Other Optimization Methods

1. Try to embed <%> in an ASP file into an HTML Tag instead of using the response. Write method, for example:

<HTML>

<Body>

<% If OK = 1 then %>

Hello! World!

<% End if %>

</Body>

</Html>

Far more:

<%

Response. Write "<HTML>"

Response. Write "<body>"

If OK = 1 then

Response. Write "Hello! World! "

End if

Response. Write "</body>"

Response. Write "

%>

Especially when your ASP file is large. Because the second method increases the server interpretation time, it also reduces the performance of ASP programs.

2. Try to use an ASP file to complete an action

Many people like to add, delete, search, and other actions in an ASP program at the same time. Do not think this is an effective way to use files. On the contrary, the result is that the application runs much slower.

The add, delete, and search operations should be divided into individual ASP files. In this way, the file will not be too large, reduce the burden of server-side interpretation and execution, and read the program very quickly.

Well, after our comprehensive optimization, is your ASP application running more stable and faster?

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.