如何提高记录集处理的性能

时间:2012-09-19 07:17:18

标签: mysql asp-classic ado recordset

我在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 

以下是我正在使用的代码:

' 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

1 个答案:

答案 0 :(得分:2)

如果您使用MySQL作为RDMS - 如果使用mySqls LIMIT子句执行分页,效率会更高:

即。获取给定查询字符串页码的每页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
相关问题