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:
$messages=mysql_query("SELECT * FROM messages WHERE topic='$topic'"); $numMessages=mysql_num_rows($messages);
- Or store the number of messages in the topics database? Such as:
$topic=mysql_query("SELECT nummessages FROM topics WHERE id='$topicid'"); $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.