ASP and SQL database linking

ASP and SQL database is now closely linked, including the Access database, the efficiency of the database is directly related to the quality of your operation system, so you better optimize the database, it is necessary, following several SQLSERVER / ACCESS optimization method introduced for use ASP friends, there may be no small help.

Method One: To use complex SQL queries, do not use a lot of single bunch of simple SQL Toru, as a complex SQL completed more efficient than a bunch of simple SQL efficiency. When there are multiple queries to good use JOIN.

The following query is not efficient:

oRs=oConn.Execute("SELECT * FROM Books")
while not oRs.Eof
strSQL = "SELECT * FROM Authors WHERE AuthorID="&oRs("AuthorID") oRs2=oConn.Execute(strSQL)
Response.write oRs("Title")&">>"&oRs2("Name")&"
&q uot;
oRs.MoveNext()
wend

The following code will be higher than the above much more efficient:

strSQL="SELECT Books.Title,Authors.Name FROM Books JOIN Authors ON Authors.AuthorID=Books.AuthorID"
oRs=oConn.Execute(strSQL)
while not oRs.Eof
Response.write oRs("Title")&">>"&oRs("Name")&"
&qu ot;
oRs.MoveNext()
wend

Method two: not a last resort, do not use an updatable Recordset

oRs=oConn.Execute("SELECT * FROM Authors WHERE AuthorID=17",3,3)
oRs("Name")="DarkMan"
oRs.Update()

The above code slower than the following code:

strSQL = "UPDATE Authors SET Name='DarkMan' WHERE AuthorID=17"
oConn.Execute strSQL

Method three: When updating the database, try to use batch updates, which is composed of a large SQL to SQL batch, run; much better than one update:

strSQL=""
strSQL=strSQL&"SET XACT_ABORT ON\n";
strSQL=strSQL&"BEGIN TRANSACTION\n";
strSQL=strSQL&"INSERT INTO Orders(OrdID,CustID,OrdDat) VALUES('9999','1234',GETDATE())\n";
strSQL=strSQL&"INSERT INTO OrderRows(OrdID,OrdRow,Item,Qty) VALUES('9999','01','G4385',5)\n";
strSQL=strSQL&"INSERT INTO OrderRows(OrdID,OrdRow,Item,Qty) VALUES('9999','02','G4726',1)\n";
strSQL=strSQL&"COMMIT TRANSACTION\n";
strSQL=strSQL&"SET XACT_ABORT OFF\n";
oConn.Execute(strSQL);

Which, SET XACT_ABORT OFF statement tells SQL Server, if the transaction process encounters an error, to cancel already completed transactions.

Method four: Avoid Text field too. When the value of the size of the string is not fixed, with varchar effect is better than with the char. I once saw an example of the program, the field is defined as TEXT (255), but his value is often only 20 characters. This data table has 50k records, so this is a big databases, large database inevitably slow.

Method five: database indexes. Where clause in the field need arise, the best indexing; need to sort fields, should do the same.

Guess you like

Origin www.cnblogs.com/lovefans168/p/10873491.html