Forum OpenACS Q&A: speed of different counts

Collapse
Posted by David Kuczek on
I would like to count all messages inside every bboard on
/bboard/index.tcl. Then I would like to display, when the newest
message has been submitted to a bboard (this code is not part of my
example, because I am not that far, yet)

Which count is quicker?

"select bt.moderation_policy, bt.topic, bt.topic_id,
bt.presentation_type, count(b.msg_id) as n_messages

from bboard_topics bt, bboard b

where (bt.active_p = 't' or bt.active_p is null)

and (bt.group_id is null

    or ad_group_member_p ( $user_id, bt.group_id ) = 't' )

and bt.topic_id = b.topic_id

group by bt.moderation_policy, bt.topic, bt.topic_id,
bt.presentation_type

order by $order_by"

OR

"select bt.moderation_policy, bt.topic, bt.topic_id,
bt.presentation_type, (select count(*) from bboard b where b.topic_id
= bt.topic_id) as n_messages

from bboard_topics bt

where (bt.active_p = 't' or bt.active_p is null)

and (bt.group_id is null

    or ad_group_member_p ( $user_id, bt.group_id ) = 't' )

order by $order_by"

--I will util_memoize this additionally...

Collapse
Posted by Jonathan Ellis on
I would guess the first, since you only have to make one pass through the table / index.  I don't know that it matters too much at this point, though -- pick one, and if it turns out later that it is killing you, then optimize it. :)
Collapse
Posted by Stephen van Egmond on
"explain" is your friend.  Put the word "explain" before each SQL statement and feed it to pgsql.  You'll get the query plan that it will execute to answer your question. It may have very little to do with how you laid out your SQL.If you have trouble decrypting the EXPLAIN output, post it here.