943,175 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Marked Solved
  • Views: 203
  • PHP RSS
Sep 3rd, 2010
0

num_rows Faster than Counter?

Expand Post »
I have a MySQL database that stores all messages for all topics and a database that stores information about the topics...

On the any given topic page I want it to display the number of messages in that topic, this value would only need to be retrieved once when the page is first loaded.

Would it be faster and more efficient to:
  • Every time a user requests the topic page it does a query to count the number of messages that are in that topic. Such as:
    PHP Syntax (Toggle Plain Text)
    1. $messages=mysql_query("SELECT * FROM messages WHERE topic='$topic'");
    2. $numMessages=mysql_num_rows($messages);
  • Or store the number of messages in the topics database? Such as:
    PHP Syntax (Toggle Plain Text)
    1. $topic=mysql_query("SELECT nummessages FROM topics WHERE id='$topicid'");
    2. $numMessages=mysql_result($topic,0);
    And then update this value every time a message is posted.

I would prefer the first message because messages are posted live and I would like this to happen as quickly as possible without having to update the nummessages value of the topics database, but if it is a slow method then I wouldn't mind doing the other one.

Also, I an expecting there to be around a million messages in total, a thousand messages for each topic and so a thousand topics.

Thanks for any help and recommendations.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
avario is offline Offline
13 posts
since Apr 2009
Sep 3rd, 2010
0
Re: num_rows Faster than Counter?
Your first example would require returning a potentially massive data set from the database simply for a count so I wouldn't suggest that.

I've seen the second one mentioned when dealing with massive datasets as well. Usually in conjunction with a set of triggers that when an row is inserted automatically increment the count and when a row is removed automatically decrement the count. This would be best stored in a separate table that contained only count values in my opinion.

Your other option would be the COUNT() function. Although with out benchmarking it I don't know if it would be any faster or slower than your first proposal.
Sponsor
Reputation Points: 265
Solved Threads: 126
Practically a Master Poster
mschroeder is offline Offline
623 posts
since Jul 2008
Sep 3rd, 2010
0
Re: num_rows Faster than Counter?
Thanks for the reply mschroeder,
I think that the COUNT() function is the best way to go because unlike SELECTing all of the rows and then num_rowing them the COUNT() function does not need to actually select the data, only count it.

Thanks.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
avario is offline Offline
13 posts
since Apr 2009
Sep 3rd, 2010
0
Re: num_rows Faster than Counter?
Except count() is approximate when used on innodb tables, possibly all transactional table types. Just something to keep in mind.
Sponsor
Reputation Points: 265
Solved Threads: 126
Practically a Master Poster
mschroeder is offline Offline
623 posts
since Jul 2008
Sep 3rd, 2010
0
Re: num_rows Faster than Counter?
Ok, thanks.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
avario is offline Offline
13 posts
since Apr 2009
Sep 3rd, 2010
0
Re: num_rows Faster than Counter?
Sure The first way >>>>
Look There is Rule in DBMS (data bas management system) as follow : -
The fields like number of students , numbe of message , age and so on
recommended to calculated , not to considered as fields in table
thanks
Reputation Points: 10
Solved Threads: 1
Newbie Poster
mohammed yaghi is offline Offline
9 posts
since Sep 2010
Sep 4th, 2010
0
Re: num_rows Faster than Counter?
Thanks mohammed yaghi,

I think that that is completely correct, the COUNT function should comply with this rule.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
avario is offline Offline
13 posts
since Apr 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: apache http server stopped working and was closeed
Next Thread in PHP Forum Timeline: Login System





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC