ASP is an environment where script programs embedded in HTML pages can run. It is not easy to make an ASP program run optimally. So how do we fully optimize the performance of ASP applications? Now let’s take a look at the performance optimization of ASP applications.
Nowadays, the network bandwidth in China 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. Then follow me to speed up your ASP program!
1. Optimization method for operating databases
The main purpose of our use of ASP is to operate the database. How to complete these actions more quickly?
1. Do not use "Select *..." arbitrarily
Please try to pick up the fields you need. For example, there are 10 fields in a Table, but you will only use one of them (name), and use "selectnamefromyourtable" instead of "select*fromyourtable". You may say, this is how I did it, but, if there are 50 fields in a table and you need to use 23 fields, what will you do? In order to save the trouble of typing and finding the corresponding field names, you may not be honestly using "selectname, sex, age...fromyourtable"!
Actually, trying to pick up the fields you need to use the select statement will be at least 5% faster for your ASP program.
2. Use system stored procedures as much as possible (for MSSQLServer)
Sometimes a read operation can be completed, using SQL statements and stored procedures, but using stored procedures will greatly speed up the read operation, which will increase the speed of your ASP program running.
3. Pay attention to how you use cursor
If you are just reading a table, please use forward-only, read-only cursors, because this cursor is the fastest to read the database, especially when you read a large amount of data.
4. Do not open useless independent record sets
Maybe you are laughing, will I open a useless record set? Yes, of course, you will, for example, when generating a tree record set, you have to open the parent record set and the corresponding child record set, and even the grand record set. In fact, you can use the DataShaping technology provided by ADO to replace opening multiple independent record sets, which will speed up the program's running speed. (For the usage of DataShaping, please refer to the ADO Help)
5. Be sure to close the open record set object and the connection object.
Some friends always wonder why their ASP program runs very fast at the beginning, but it gets slower and slower after running it a few more times? Even the server crashes. This happens, it is likely that you have opened too many recordset objects and Connection objects and finally not closed them. Use the following method to close:
YourRecordSet.close
SetYourRecordSet=Nothing
SetYourConnection=Nothing
6. Comparison of methods for obtaining database data
How do you get the data from the record set? Is it using YourRecordSet (field number), or
YourRecordSet("field name")? Actually, there are other ways to use it. Let's compare it now (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 I know. Everyone can understand the first three methods. I will talk about how to use the fourth method (Set method):
Program code
DimstrSQL
StrSQL="selectname,sex,agefromyourtable"
Dimrs
Setrs=server.createobject("ADODB.RECORDSET")
Rs.openstrSQL,conn,1,1
ConstfieldsOrder=2
DimobjOrder
SetobjOrder=rs(fieldsOrder)
Response.writeobjOrder 'Set method
2. Optimization methods for the use of built-in ASP objects
1. Minimize the use of Session objects and Application objects
Although these two objects provided in ASP are of great help to our programming, these two objects should be used reasonably and not abused. Because using these two objects in large quantities will greatly increase the burden on the server and seriously consume system resources. It will also make your ASP program run as slow as an old cow.
2. Close objects that are no longer used in a timely manner (especially Session and Application)
Failure to close the objects you are using will cause the system to run slowly. Maybe you will ask,
Can't Session and Application disappear automatically? It's completely correct. The system defaults that the Session_OnEnd and Application_OnEnd events will automatically trigger if there is no operation within 30 minutes. However, a large number of users frequently read the server, and the server will keep those that are useless for a long time.
Session, Application object. If the session and Application that are not closed in time, the consequences will be unimaginable.
The method to close is:
Set object=Nothing
3. Use Include files reasonably
What we are talking about here is using The file included in the form, and the file content is all ASP programs, that is, you put some public functions into a file and include them on other pages that may call the functions.
It is recommended that you do not put all functions in an include file, because when you include this file on other pages, the server side needs to precompile it. It is very likely that there are hundreds of functions in a include file, and you just want to use one of them, which is not worth the effort. So, split your include files as much as possible into multiple small include files. This can also improve the running speed of the program.
4. Optimization methods for VBScript language
1. Try to use system functions instead of functions you wrote
For example, if you want to split a regular string ("sss, ddd, ffff, ggg"), you don't have to use Mid(), Instr and other functions to analyze it. In fact, VBScript provides a function Split(), which saves time and improves speed. Why not do it?
2. Reduce the use of dynamic arrays
3. Develop the habit of declaring variables in advance as much as possible
Don't underestimate this article. Declaring variables in advance will speed up the execution time of the program's interpretation. On the contrary, never declaring variables is not only difficult to read, but the execution efficiency of the entire program on the server will also be greatly reduced.
5. Other optimization methods
1. Try to use <%%> to embed it into HTML tags in ASP files, instead of using Response.write, for example:
Program code
<%Ifok=1then%>
Hello!World!
<%EndIf%>
It's far more than:
<%
Response.write"
"
Response.write"
"
Ifok=1then
Response.write "Hello!World!"
EndIf
Response.write""
Response.write""
%>
The running speed should be fast, especially when your ASP file is relatively large. Because the second method increases the server-side interpretation time, thus reducing the performance of the ASP program.
2. Try to complete an action with an ASP file
Many people like to complete multiple actions such as adding, deleting, searching, etc. in an ASP program at the same time. Don’t think that this is effective in utilizing files. On the contrary, the result of this is to slow down the application's running speed a lot.
Add, delete, find, etc. should be split into single independent ASP files to complete. This will prevent the file from being too large, reduce the burden of server-side interpretation and execution, and read the program very quickly.
The performance optimization of ASP applications has been introduced. After our comprehensive optimization, is your ASP application running more stable and faster?