gpt4 book ai didi

performance - 在经典 asp 中创建对象时性能降低

转载 作者:行者123 更新时间:2023-12-03 16:57:29 24 4
gpt4 key购买 nike

为了衡量网站性能,我创建了一个简单的日志记录 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# 编写此代码,但我必须先研究此代码,然后再继续前进)。

更新:我在收到第一条评论后添加了更多代码以提供更多代码。说明:该代码创建了一个包含检索到的国家/地区的组合框(选择菜单)(它在前一段代码停止的地方继续)。

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 访问权限。这是我所知道的唯一方法,除非有其他我可以看看的方法。

最佳答案

首先,请检查您的连接字符串,我在某些提供商那里遇到过较慢的时间。我找到的经典 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 ....

关于performance - 在经典 asp 中创建对象时性能降低,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25304233/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com