The ASP program performs a good efficiency of the database.

xiaoxiao2021-03-31  199

This article hopes to help you :)

Many netizens don't like to use ASP to program, they always complain that the ASP program is too slow, the efficiency is too low. I want to write programs with PHP, JSP, etc.. In fact, you can't see the problem from "think" this point, but should look from the actual situation. Is the ASP really slow, then why Microsoft stations use ASP not slow? Is PHP really fast, in fact it is still an explanatory language. It is only better to combine under Linux. The development of JSP does not simply, and it is not possible to connect Access libraries through the JDBC-ODBC bridge. This situation is not high.

In fact, the three languages ​​have its own characteristics, it is excellent, shortcomings. As a programmer, it should not always blame the language, and it should be its own technology. So today we will not compare these three web languages ​​who are good, who is bad, but explain how to improve the performance of the program.

First article: SQL statement

The most striking thing in the web program should be database operations. Today we will tell how to improve the efficiency of SQL statements. It is assumed that there is a table of the USERS below, where ID is the primary key. There have been 10,000 records.

ID INT 4

Name Char 10 // Name

Age Int 2 // Age

SEX Char 2 // Gender default is "male"

Address Nvchar 255 // Address

Cash Int 4 // Points

Photo char 2 // Is there a photo default value is "no"

Photourl nvchar 255 // photo path

Home Let's talk about the problem of the database in the database and fields. In the database object for the web, the fewer the number of natural tables, the better, the less the number in the table, the better, the field is occupied; The less byte, the better.

For example, the field of SEX can be used in a Bit field, and "0" is male, and "1" is female, so that the number of bytes accounted for CHAR can be reduced, so that one record Many still considerable.

Anothertery, for example, the field of the PHOTO can be removed at all, so that the field can be reduced, thereby speeding up the speed of reading. Because the Photo field is just used to determine if the user has a photo, and we can safely use the photourl field to determine. If the value of the PHOTOURL field is empty, it means that this user does not have a photo, otherwise, the photour field must be valued, and the corresponding function can also be completed.

For example, we have to display a record of the top 10 usernames. Some netizens will do the following:

I = 0

SET RS = conn.execute ("SELECT * from users")

Do While Not Rs.eof and i <= 10

I = i 1

Response.write ("Name") & "
"

Rs.movenext

Loop

This code can complete the above functions, but if the true places are in the program, the efficiency is too low. Because the following program reads all records, 10,000, but we only need 10, if this is too wasteful, of course, we can use the TOP command in MS SQL to complete this. Or the limit command in MySQL.

In addition, we only need the field of the username to display, and the other is not needed, and this netizen has used "select *" command, and I read all the fields of all fields, which is very unnecessary, and In the case of a lot of fields, it has aggravated the burden on the execution. So, you should use the "SELECT field" to perform the appropriate operation. So the above program can be modified to: SET RS = conn.execute ("SELECT TOP 10 Name from Users")

Do While Not Rs.eof

Response.write RS ("Name") & "
"

rs.movenext

loop

In this way, in the case of many records, the operation will be a lot quickly.

In addition, when the operation is completed, it is necessary to turn off the connection of the database when operating again.

SET RS = Nothing

Set conn = Nothing

Some netizens said, it turned out to be like this, then use the following code to view the UserS table to complete it.

Set = conn.execute ("SELECT NAME AERS")

I = 0

Do While Not Rs.eof

I = i 1

Rs.movenext

loop

SET RS = Nothing

In fact, the efficiency of this code is still very low. In order to know how many records in the table, all records are traversed, it is very uncertain. We can use the following code:

SET RS = Conn.execute ("Select Count (ID) AS Num from Users")

I = rs ("num")

SET RS = Nothing

Is it very simple, in fact, the count () command can count the number of records you want to query, using where add conditions.

If a user wants to perform several operations at a time of the database, we must also pay attention to the lock table after the operation, complete the step of unlocking with the LOCK operation, using unlock, because if a user is operating the database, another user is also Operation, it is easy to cause confusion of data, even make data insertion mark, produce very serious consequences.

In ASP operation, there are two methods for the operation of the database, one is to directly reference the conn.execute stream, and the other is to use the RS.Open operation. The two operations have their benefits, and in the MS SQL operation, the former is more used because it is a single current operation, which generally cannot use rs.addnew or rs.update, because this action is to open the table. The write operation runs, such as RS.Open SQL, CONN, 3, 3, and 3, indicates that the write operation can be performed, and 1 is a read operation.

Finally, you should use the WHERE condition as much as possible to bring multiple statements together. For example: display the username of the first 10 male user who is aligned according to the integration.

Take a look at the code below a netizen:

SET RS = conn.execute ("SELECT * from users Order by Cash Desc")

I = 0

IF i <= 10 and not rs.eof then

IF RS ("SEX") = 'male' TenResponse.write RS ("Name") & "
"

I = i 1

END IF

Rs.movenext

END IF

SET RS = Nothing

This code can also complete the above tasks, but the efficiency is too low, and each sentence read from the database will be judged. You can put it in the WHERE statement for gender judgment, look at the modified code:

SET RS = conn.execute ("SELECT TOP 10 Name from Users Where Sex = 'Male' Order By Cash Desc")

Do While Not Rs.eof

Response.write RS ("Name") & "
"

rs.movenext

loop

SET RS = Nothing

Plus the top commands mentioned above, the SELECT field name, and WHERE conditions, not only make the program code, but also make the execution efficiency of the program.

In fact, there is still a lot of efficiency in the SQL language, but these are both beginner friends or intermediate friends will often encounter, and they don't care. I hope everyone can learn more, open ideas, and constantly get experience from practice.

转载请注明原文地址:https://www.9cbs.com/read-130813.html

New Post(0)