แต่งโปรแกรม ASP ให้เร็วสุดๆ

อะไรคือความแตกต่างระหว่างโปรแกรม ASP ที่น่าใช้กับที่ไม่น่าใช้ คำตอบคือ "ความเร็ว" บทความนี้จะสอนเคล็ดลับการปรับแต่งโปรแกรม ASP ของคุณให้หลุดพ้นจากข้อจำกัดทางความเร็วทั้งปวง

พิมพ์ครั้งแรกในนิตยสาร QuickPC ฉบับที่ 114

โปรแกรมก่อนปรับปรุงความเร็ว

<HEAD><TITLE>dbtableslow.asp</TITLE></HEAD>
<HTML><body bgcolor="#FFFFFF">
<%
myDSN = "DSN=student;uid=student;pwd=magic"

mySQL= "SELECT * from authors order by Author"
call query2table(mySQL,myDSN)
%>

<!--#include virtual="/learn/test/lib_dbtableslow.asp"-->
</BODY></HTML>

<HEAD><TITLE>dbtableslow.asp</TITLE></HEAD>
<HTML><body bgcolor="#FFFFFF">
<%
myDSN = "DSN=student;uid=student;pwd=magic"

mySQL= "SELECT * from authors order by Author"
call query2table(mySQL,myDSN)
%>

<!--#include virtual="/learn/test/lib_dbtableslow.asp"-->
</BODY></HTML>

<%
sub query2table(inputquery, inputDSN)
   dim conntemp, rstemp
   set conntemp=server.createobject("adodb.connection")
   conntemp.open inputDSN
   set rstemp=conntemp.execute(inputquery)
   howmanyfields=rstemp.fields.count -1%>

   <table border=1><tr>
   <% 'Put Headings On The Table of Field Names
   for i=0 to howmanyfields %>

             <td><b><%=rstemp(i).name%></B></TD>
   <% next %>
   </tr>
   <% ' Now lets grab all the records
   DO UNTIL rstemp.eof and response.isclientconnected()%>

      <tr>
      <% for i = 0 to howmanyfields
         thisvalue=rstemp(i)
         If isnull(thisvalue) then
            thisvalue="&nbsp;"
         end if%>

             <td valign=top><%=thisvalue%></td>
      <% next %>
      </tr>
      <%rstemp.movenext
   loop%>

   </table>
   <%
   rstemp.close
   set rstemp=nothing
   conntemp.close
   set conntemp=nothing
end sub%>

<%
sub query2table(inputquery, inputDSN)
   dim conntemp, rstemp
   set conntemp=server.createobject("adodb.connection")
   conntemp.open inputDSN
   set rstemp=conntemp.execute(inputquery)
   howmanyfields=rstemp.fields.count -1%>

   <table border=1><tr>
   <% 'Put Headings On The Table of Field Names
   for i=0 to howmanyfields %>

             <td><b><%=rstemp(i).name%></B></TD>
   <% next %>
   </tr>
   <% ' Now lets grab all the records
   DO UNTIL rstemp.eof and response.isclientconnected()%>

      <tr>
      <% for i = 0 to howmanyfields
         thisvalue=rstemp(i)
         If isnull(thisvalue) then
            thisvalue="&nbsp;"
         end if%>

             <td valign=top><%=thisvalue%></td>
      <% next %>
      </tr>
      <%rstemp.movenext
   loop%>

   </table>
   <%
   rstemp.close
   set rstemp=nothing
   conntemp.close
   set conntemp=nothing
end sub%>

 

โปรแกรมเมื่อปรับปรุงความเร็วแล้ว

<%response.buffer=true%>
<HEAD><TITLE>dbtablefast.asp</TITLE></HEAD>
<HTML><body bgcolor="#FFFFFF">
<%
myDSN = "DSN=student;UID=student;pwd=magic"

mySQL="select * from authors order by author"
call query2table(mySQL,myDSN)
%>

<!--#include virtual="/learn/test/lib_dbtablefast.asp"-->
</BODY></HTML>


<%
sub query2table(inputquery, inputDSN)
   dim conntemp, rstemp
   set conntemp=server.createobject("adodb.connection")
   ' 0 seconds means wait forever, default is 15
   conntemp.connectiontimeout=0
   conntemp.open inputDSN
   set rstemp=conntemp.execute(inputquery)
   howmanyfields=rstemp.fields.count -1
   tablestart="<table border=1 cols=3><col width='15%'><col width='70%'><col width='15%'><tr>"
   response.write tablestart
   for i=0 to howmanyfields %>

             <td><b><%=rstemp(i).name%></B></TD>
   <% next %>
   </tr>
   <% ' Now lets grab all the records
   DO UNTIL rstemp.eof
      counter=counter+1
      response.write "<tr>"
      for i = 0 to howmanyfields
         thisvalue=rstemp(i)
         If isnull(thisvalue) then
            thisvalue="&nbsp;"
         end if
      response.write "<td valign=top>" & thisvalue & "</td>" & vbcrlf
      next
      response.write "</tr>"
      rstemp.movenext
      IF counter mod 50=0 THEN
         If response.isclientconnected()=false THEN
            EXIT DO
         END IF
         response.write "</table>" & TableStart
         response.flush         
      END IF

   loop%>

   </table>
   <%
   rstemp.close
   set rstemp=nothing
   conntemp.close
   set conntemp=nothing
end sub%>

สารบัญ