I have a forum that has the thread title, post count, and other info. I’ve managed to optimize the other queries/remove them completely, but the one that counts each thread’s post count is slowing the main board page from loading.
i.e. echo $boards->getPostCount($thread)
inside getPostCount($thread) is just:
$query = $this->db->query("SELECT COUNT(id) FROM forum_posts WHERE threadid = '$thread'
This is a problem because it has to do this for each thread and there are 20 threads per page.
Is there any other way I can get this information beforehand or somehow minimize the queries required to get this information? There are 150 users on at any given time so 3K queries per page load is not something I want.
Thanks.
Edit: the EXPLAIN of the query I was given to use:
mysql explain http://screensnapr.com/e/01hulx.png
Use joins:
However, this still requires the engine to count posts for each thread on each reload.
You could enable query cache (it’s ideal for the queries like that) or just add a
post_countfield tothreadsand update it each time a post is made or deleted.