PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
I am building something very similar to a message forums like this, where users post many times. I want to make visible each user's "# of posts:" in each post they make. Here is my question,
Should I create a column in the 'users' table called 'num_posts' that increments with each post made by the user?
within the loop that prints out all the posts/threads. I am afraid this method of calculating the number of posts is processing intensive... but I dunno. What would you suggest for this? Thanks for your help! Take care.
If you want your forum to be scalable, use the second option. This is what phpbb uses, and most other forum systems that I've come across. As long as you perform all postings and deletions within the forum system, you should be fine. This fails when someone decides to screw around with the database manually, which they probably shouldn't be doing anyway.
It seriously comes down to your preference. phpBB dynamically calculates the posts via count() while other forum softwares such as invisionboard store the post count in the database.
count() is extremely fast, so I would probably go that route, and in doing so you avoid having to query increment the post down up or down when posting or deleting posts
Jcart wrote:It seriously comes down to your preference. phpBB dynamically calculates the posts via count() while other forum softwares such as invisionboard store the post count in the database.
count() is extremely fast, so I would probably go that route, and in doing so you avoid having to query increment the post down up or down when posting or deleting posts
What JCart says is true. I would only add that if activity approaches high levels of traffic doing count() will eat a lot of system resources. I read a thread on the phpBB board that talked about what someone had to do when traffic on the board reached 10000 or so... One of the things recommended was to get rid of totaling the posts with a query on all posts... Storing it in a field would be more efficient on system resources. IMHO...
Well with this being said, is it still efficient to use COUNT() for my purposes? Ideally I would love to use count() just because it is so easy and will always return the right answer. Thanks for the good input guys.
It will basically come down to this, count() will be initially faster but as your database grows it will eventually become slower than using a seperate column storing post count. I'm going to run a quick benchmark and will get back to you soon.