I’m creating a forum (for fun) in php and I want too show the amount of posts and topics per forum. Is it better to add a column to my forums in the database showing the number of thread/topics and whenever someone creates/deletes a topic it will get updated OR counting the amount of topics/threads everytime the forum page is loaded? What is common practice in this case?
Share
Counting records is the only reliable way to do this; if you store counts then you will have a concurrency issue to address; give the database a chance and only fix it if it becomes a real problem. My experience is that COUNT(*) can be surprisingly quick.
I’ve got a table with 1.2M records (and it has the right indexes); just tried
select count(*) from table_name where field=11;takes 0.02 seconds and returns 104, to count 500k records takes 0.15 seconds. This is using mysql on a fairly low spec VPS.The key thing is to do some performance tests and to only optimize away from the easiest most reliable solution when there is a genuine performance issue.