I was messing around writing a small module that retrieved results
differently than the standard way that AS does. I believe that it is
quite a bit quicker than the standard way (i.e. ns_set), but this is the
first time that I have written anything for AS at the C level, so I am
not sure if I am missing something here. In fact, I am quite surprised
that there hasn't been something like this in the core...which makes me
wonder if there is something inherently wrong with this approach, and I
am missing some vital concepts.

I tested it on a result set of 1000 tuples, with 5 attributes, and it
returns almost immediately (although the browser has trouble rendering
it that quickly). I am on a modest machine as well (dual PII @ 333MHZ
running SuSE Linux). The AS process, and the DB process (PG) are running
on the same machine (PG is not tuned at all); however, there is no other
web/db traffic at the moment. I also tested a result set with 10
attributes and 50000 tuples. It took roughly 3 seconds to retrieve the
results, but of course my browser took quite a long time to render (in
fact I stopped it before it finished).

Anyways, there are alot of variables that might determine the actual
performance, but in general, I think these are quite a bit quicker.

So, why am I writing this?
1) To see if there is something fundamentally wrong with my approach
2) To see if my code makes sense (especially with respect to threading)
3) To share the code for anyone who wishes to use it

There are 3 different ways to gets results (more to come):
1) all within one list.
   EXAMPLE

   set sql "SELECT att1, att2, att3 FROM table1;"
   set db [ns_db gethandle main]
   set handle [ns_db select $db "$sql"]

   if {[catch {set L [ns_result $db $handle -list]} err]} {
       ns_db releasehandle $db
       ns_returnnotice $conn 200 "ERROR" $err
   }

   set t "<table>"
   foreach {a1 a2 a3} $L {
      append t "<tr><td>$a1</td><td>$a2</td><td>$a3</td></tr>"
   }

   append t "</table>"

   ...

2) List of lists
   EXAMPLE

   set sql "SELECT att1, att2, att3 FROM table1;"
   set db [ns_db gethandle main]
   set handle [ns_db select $db "$sql"]

   if {[catch {set L [ns_result $db $handle -llist]} err]} {
       ns_db releasehandle $db
       ns_returnnotice $conn 200 "ERROR" $err
   }

   set t "<table>"
   foreach T $L {
      append t "<tr><td>[join $R "</td><td>"]</td></tr>"
   }

   append t "</table>"

   ...

3) array (with tuple num and attr as index; i.e. myarr(tupno,attr)

   EXAMPLE

   set sql "SELECT att1, att2, att3 FROM table1;"
   set db [ns_db gethandle main]
   set handle [ns_db select $db "$sql"]

   if {[catch {set L [ns_result $db $handle -array myarr]} err]} {
       ns_db releasehandle $db
       ns_returnnotice $conn 200 "ERROR" $err
   }

   set t "<table>"
   foreach R [array names myarr] {
      append ul "<TR><TD> $R </td><td> $myarr($R)</td></tr>"
   }
   append t "</table>"

   ...

I plan to port this to 4.0 as well (i.e. objectify the commands), since
I want to start messing with 4.0.

Anyways, if anyone has any comments, I would appreciate them.

Thanks and happy holidays to everyone,

    --brett


--
Brett Schwarz
brett_schwarz AT yahoo.com

Reply via email to