I just posted an article I wrote called ASP Speed Tricks. It covers
techniques to optimize output of database data in HTML, for
both simple tables and complex tables. More advanced ASP authors
might
be interested in the complex table optimizations. Please check it out
at:
http://www.somacon.com/aspdocs/
Hope you enjoy,
Shailesh
Please reply at
http://www.somacon.com/contact.php
Taking into account the excellent points made in this thread, here's
some code in defense of GetString
<%
Dim sql,cn,rs,timeGS,timeRS
Dim fld0,fld1,fld2,fld3
sql = "tblData"
timeGS=Timer
Response.Write "<pre>"
Set cn = CreateObject("ADODB.Connection")
cn.Open gConn '<-- Your OLEDB DSN-Less Connection String Here
Set rs = cn.Execute(sql,,&H2)
Do While Not rs.EOF
Response.Write rs.GetString(2,30,vbTab,vbCRLF)
Loop
rs.Close : Set rs = Nothing
cn.Close : Set cn = Nothing
Response.Write "</pre>"
timeGS = Timer - timeGS
timeRS = Timer
Set cn = CreateObject("ADODB.Connection")
cn.Open gConn '<-- Your OLEDB DSN-Less Connection String Here
Set rs = cn.Execute(sql,,&H2)
Set fld0 = rs(0)
Set fld1 = rs(1)
Set fld2 = rs(2)
Set fld3 = rs(3)
Response.Write "<pre>"
Do While Not rs.EOF
Response.Write rs(0)
Response.Write vbTab
Response.Write rs(1)
Response.Write vbTab
Response.Write rs(2)
Response.Write vbTab
Response.Write rs(3)
Response.Write vbTab
Response.Write vbCRLF
rs.MoveNext
Loop
Response.Write "</pre>"
rs.Close : Set rs = Nothing
cn.Close : Set cn = Nothing
timeRS = Timer - timeRS
Response.Clear
Response.Write "<br>GetString: " & timeGS
Response.Write "<br>Recordset: " & timeRS
%>
Results:
GetString: 2.324219
Recordset: 4.566406
The GetString method ran nearly twice as fast (1.965x) as the Recordset
method in the 20,000 record scenario.
Notes:
1. The key thing to note is that you can use the rows parameter of the
GetString method to "burst" output in chunks. A little empirical testing
showed that in this scenario, 30 rows at a time seemed optimal. I'm sure
there's some black magic formula that defines the ideal size to make
best use of the Response.Write optimizations available in VBScript, I
just have no idea what that formula is. If someone could provide insight
into this, I'd be much obliged. Sorry, no such optimization exists in
JScript (early bound vs. late bound).
2. I let the Recordset method go second to insure it had the benefit of
connection pooling. But it's a small point and not worth quibbling over.
3. There are ways to apply additional formatting at the cell level when
using the GetString method. Namely, use set the DataFormat property of
each field using the StdDataFormat object available via the
MSSTDFMT.DLL. However, I'm of the opinion that formatting should be
handles via stylesheets, i.e.. declare a class attribute for the
embodying table/block tag and take care of formatting in an external
CSS.
4. Your complex table example was a little difficult to follow, however
if I understand it correctly you wanted to present both detailed data
and summary/aggregate data in the same listing. One solution that you
did not investigate would be to return a result set which is the union
of the detail data and the summary/aggregate data. This has the
following consequences. First it eliminates the redundant data
associated with an joined result set. Secondly, it keeps the data
processing where it belongs, namely at the database. Thirdly, is does
not change the nature of what's returned (a result set is a result set
is a result set). As such, all the same benefits/optimizations of the
GetString method continue to apply.
5. I would have liked to have used sp as connection, but in this case
that would actually represent an additional translation layer. Sorry
Bob.
6. Not that is should matter, since the recordset is being called in
exactly the same way in both scenarios, but for completeness here's what
I used to represent tblData. Note, Numbers is a table which contains
numbers from 1 to 8000 (inclusive). The database was SQL Server 2000.
CREATE TABLE [dbo].[Numbers]( [Number] [smallint] IDENTITY (1, 1) NOT
NULL)
CREATE VIEW dbo.tblData
AS
SELECT TOP 20000
N1.Number AS Field1,
N2.Number AS Field2,
CAST('FOO' AS TEXT(50)) AS Field3,
CAST('BAR' AS TEXT(50)) AS Field4
FROM
dbo.Numbers N1 CROSS JOIN
dbo.Numbers N2
HTH
-Chris