PostgreSQL & PHP Tutorials - Database Design Issues - Big Query
Using that database design, to get the number of threads and posts per forum, you end up with a complicated query looking something like this:
SELECT f.forumid, f.forumtitle, forumnode, f.createdate, COUNT(threadid) AS threadcount, (SELECT COUNT(postid) FROM myforum_posts p WHERE p.threadid=t.threadid) AS postcount FROM myforum_forums f LEFT OUTER JOIN myforum_threads t ON f.forumid=t.forumid GROUP BY f.forumid, f.forumtitle, f.forumnode, f.createdate, t.threadid ORDER BY forumnode ASC;
Woah!
We have to use a LEFT OUTER JOIN between the forum and thread tables in case a forum has no threads. If we used a straight or inner join, the forum would not show up if there were no threads - which means we can't go in and add a thread.
Then we have to do a subquery using that threadid against the posts table to see how many posts are in a thread. This is all added together by the group by clause.
This will become quite slow once a few forums, threads and posts are included because of postgresql's MVCC architecture - it has to go through the table and scan it to see how many rows are in that table, it can't be cached (if you have a lot of rows, a count will be quite slow).
What can we do?
Avg Rating: 4
Vote Count: 13
