我用ASP编写了一个web应用程序,它从MySQL数据库(v5.1.63)读取3600行,并将数据输出到HTML表中。我使用记录集来获取数据,使用pagesize/cursorlocation让用户在页面之间前进/后退。该数据库目前包含约200.000行,但仍在不断增长。
页面加载时间越来越长(现在大约需要15-20秒),如果可能的话,我想对它进行优化。
我很想知道如何提高性能的技巧。
以下是数据库结构:
# Col Type Collation Attributes Null Default 1 ID int(11) No None AUTO_INCREMENT 2 mean varchar(5) utf8_general_ci No None 3 max varchar(5) utf8_general_ci No None 4 min varchar(5) utf8_general_ci No None 5 dt varchar(20) utf8_general_ci No None 6 dir varchar(2) utf8_general_ci No None 7 log text utf8_general_ci No None
Here is the code I'm using:
' Opening the db
Set oConn = Server.CreateObject("ADODB.Connection")
Set oRS = Server.CreateObject("ADODB.Recordset")
oConn.ConnectionString =
"DRIVER={MySQL};SERVER=<server>;DATABASE=<database>;UID=<uid>;PWD=<pwd>;"
oConn.Open
' Retrieve 3600 records
sSQL = "SELECT * FROM mytable ORDER BY id DESC"
oRS.CursorLocation = adUseServer
oRS.PageSize = 6*600
oRS.Open sSQL, oConn, adOpenForwardOnly, adLockReadOnly
nPageCount = oRS.PageCount
...code to set the page selected by the user (nPage)
oRS.AbsolutePage = nPage
Do While Not (oRS.EOF Or oRS.AbsolutePage <> nPage)
...
Response.Write("<td>" & oRS("dt") & "</td>")
Response.Write("<td>" & oRS("mean") & "</td>")
Response.Write("<td>" & oRS("min") & "</td>")
Response.Write("<td>" & oRS("max") & "</td>")
...
oRS.MoveNext
Loop
oRS.Close
最佳答案
如果使用MySQL作为RDMS,那么如果使用mySqlsLIMIT
子句执行分页,效率会高得多:
即,对于给定的查询字符串页码,每页获取10个项目:
ItemsPerPage = 10
PageNumber = request.querystring("Page")
if PageNumber = "" then
PageNumber = 0
end if
Limit = PageNumber * ItemsPerPage
Query = "SELECT * FROM mytable ORDER BY id DESC LIMIT " & Limit & ", " & ItemsPerPage