尝试衡量网站性能我创建了一个简单的日志记录sub
,以查看在执行经典asp页面时哪些部分很慢。
sub log(logText)
dim fs, f
set fs = Server.CreateObject("Scripting.FileSystemObject")
set f = fs.OpenTextFile("log.txt", 8, true)
f.WriteLine(now() & " - " & logText)
f.Close
set f = Nothing
set fs = Nothing
end sub
log "Loading client countries"
set myR = Server.CreateObject("ADODB.RecordSet")
myR.ActiveConnection = aConnection
myR.CursorLocation=2
myR.CursorType=3
myR.LockType=2
myR.Source = "SELECT * FROM CC ORDER BY ccName ASC"
log "Opening db connection"
myR.open()
log "Opened db connection"
以下是结果(仅显示时间部分):
...
11:13:01 - Loading client countries
11:13:06 - Opening db connection
11:13:06 - Opened db connection
...
创建ADODBRecordSet
并设置一些属性大约需要5秒钟。这并不总是发生,有时代码执行得很快,但通常在我重新加载页面时
几分钟后,加载时间或多或少与示例相同。这真的是一个服务器/资源问题,还是我应该考虑重写代码? (我最好的选择是在C#
中写这个,但我必须先研究这段代码,然后继续前进。)
更新 :我在收到第一条评论后添加了更多代码,以提供更多代码。说明:代码使用检索到的国家/地区创建combobox
(选择菜单)(它继续前一段代码停止的位置)。
if not myR.eof then
clientCountries = myR.getrows
send("<option value='0'>Select country</option>")
log "Creating combobox options"
for i = 0 to ubound(clientCountries, 2)
if cstr(session("clientCountry")) <> "" then
if cstr(clientCountries(1, i)) = cstr(session("clientCountry")) then
isSelected = "selected" else isSelected = ""
end if
end if
if cstr(session("clientCountry")) = "" then
if cstr(clientCountries(1, i)) = "23" then
isSelected = "selected"
else
isSelected = ""
end if
end if
optionString = ""
optionString = clientCountries(2, i)
send("<option value='" & clientCountries(1, i) & "' " & isSelected & ">" & convertToProperCase(optionString) & "</option>")
next
log "Created combobox options"
end if
myR.Close
myR.ActiveConnection.close
myR.ActiveConnection = nothing
set myR = nothing
log "Loaded client countries"
接下来的两个日志条目如下:
11:13:06 - Creating combobox options
11:13:06 - Created combobox options
...
到目前为止,SELECT
查询作为页面的其余部分(2或3个查询更多)在下一秒内或多或少地执行。减慢页面速度的唯一部分是您可以在日志的第一部分中看到的内容。我不确定是否可以分析 SQLServer ,因为我只有 cPanel 访问权限。这是我所知道的唯一方式,除非我能看到别的东西。
答案 0 :(得分:1)
首先,检查您的连接字符串,我遇到某些提供商的时间较慢。我为经典asp找到的最好的提供者是sqloledb
其次,在我对GetRows()数组执行For
语句之前,我会关闭我的SQL对象。如果你不需要,你不想保持开放。
第三,我使用存储过程。通过节省编译时间,您将获得性能提升。
第四,我会不惜一切代价避免做SELECT *
语句,而是只返回我需要的列。 (您的代码看起来只需要2列)
第五,我会在表格上建立索引的时间超过预期。
如果这不能解决问题,我会考虑其他语言/解决方案。不确定数据集是什么样的,所以不能说是否应该考虑平面文件数据库。
另外,请尝试将其用于记录集,看看会发生什么:
Set myR = Server.CreateObject("Adodb.Recordset")
myR.Open "SELECT * FROM CC ORDER BY ccName ASC", aConnection
If myR.RecordCount > 0 Then clientCountries = myR.GetRows()
myR.Close
Set myR = Nothing
If IsArray(myR) Then
For ....