Forum OpenACS Q&A: db_foreach or db_multirow

Collapse
Posted by Jay Dubanik on
What is the best practise for selecting only few rows of data in a procedure?
(eg. a widget to display newest posts to the forum on main page).
Should I use db_foreach or db_multirow.
Are there any differences in performance between those two?
Collapse
Posted by Jeff Davis on
you should write the query so that is does not return rows you do not want to display. It does not really matter much which of db_foreach or db_multirow you use (the performance difference between the two is almost certainly dwarfed by the cost of going to the db in the first place).

If you are using postgres you can use the "limit 10" statement to only return 10 rows and in oracle you can use rownum < 10 (rownum is a psuedocolumn in all oracle queries). So the trick is to do the query, sort it, and only return the rows you want to display.

Collapse
Posted by Don Baccus on
If you use db_foreach you'll probably be tempted to generate HTML code in your Tcl file (i.e. strings like "<tr><td>my data ...").  db_multirow interfaces with the <multirow> tag in the templating system, which makes it easy to keep all your HTML stuff in your template file.