,conn.asp
<%
set conn = server.CreateObject("adodb.connection")
on error resume next
conn.Open "driver={microsoft access driver (*.mdb)}; dbq=" & server.MapPath("da##ta.mdb")
%>
,index.asp
<!--#include file = "conn.asp"-->
<%
set rstc=server.createobject("adodb.recordset")
sqltc="select * from url where class='0' order by grade desc"
rstc.open sqltc,conn,1,1
%>
<%do while not rstc.eof%>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td><%=rstc("classid")%> <%=rstc("classname")%></td>
</tr>
<tr>
<%
set rs=server.createobject("adodb.recordset")
sql="select * from url where classid=" & rstc("classid") & " order by grade desc "
rs.open sql,conn,1,1
%>
<%do while not rs.eof%>
<td><%=rs("url")%></td>
<%
rs.movenext
loop
%>
</tr>
</table>
<%
rstc.movenext
loop
%>
<%
rstc.close
set rstc=nothing
rstn.close
set rstn=nothing
conn.close
set conn=nothing
%>
运行后导致整个浏览器速度都很慢了,怎么优化一下?
參考答案:该段代码耗用内存是因为你在第一个循环中
<%do while not rstc.eof%>
。。。。。。
<%
set rs=server.createobject("adodb.recordset")
sql="select * from url where classid=" & rstc("classid") & " order by grade desc "
rs.open sql,conn,1,1
%> 。。。。
<%rstc.movenext
loop%>
循环建立记录集 rs,每次建立都要在内存中开辟一定空间,所以耗内存。
应该先在外面建立记录集,如
<%
set rstc=server.createobject("adodb.recordset")
set rs=server.createobject("adodb.recordset")
rstc.open "select * from url where class='0' order by grade desc",conn,1,1
%>
<%do while not rstc.eof%>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td><%=rstc("classid")%> <%=rstc("classname")%></td>
</tr>
<tr>
<%
rs.open "select * from url where classid=" & rstc("classid") & " order by grade desc ",conn,1,1
%>
<%do while not rs.eof%>
<td><%=rs("url")%></td>
<%
rs.movenext
loop
rs.close
%>
</tr>
</table>
<%
rstc.movenext
loop
%>
<%
rstc.close
set rstc=nothing
rstn.close
set rstn=nothing
conn.close
set conn=nothing
%>
这样系统将始终使用由RS开辟的内存区域,例外,ASP中字符串的相加也相当耗用内存,它用的内存等于各次相加的字符累计占用内存之和。所以就不用变量存放SQL语句。