How to Speed up Drupal Forum Pages on a Busy Site

Drupal 5's forum module is OK. It lets you create multiple forums with taxonomy terms. Forum posts are true nodes so you get all the benefits of nodeness. And Drupal keeps track of which posts you've read. All that is great. But I've had some trouble scaling up a site that relies heavily on the forum module, even when using advcache. Investigating how the database was spending its time, I found lots of queries like this:

SELECT n.nid, n.title, n.sticky, l.comment_count, l.last_comment_timestamp FROM node n INNER JOIN node_comment_statistics l ON n.nid = l.nid INNER JOIN term_node r ON n.nid = r.nid AND r.tid = 123 WHERE n.status = 1 AND n.type = 'forum' ORDER BY n.sticky DESC, l.last_comment_timestamp desc

An EXPLAIN shows how nasty this is:

id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE n ref PRIMARY,node_type,status,node_status_type,nid node_status_type 102 const,const 30679 Using where; Using temporary; Using filesort
1 SIMPLE l eq_ref PRIMARY PRIMARY 4 drupal.n.nid 1  
1 SIMPLE r eq_ref PRIMARY,nid,tid PRIMARY 8 const,drupal.l.nid 1 Using where; Using index

Searching Drupal's code turns up this query in theme_forum_topic_navigation(). It turns out the database is smoking, grinding away, eating up cycles generating previous and next links for forum topics. I thought about it for a minute, and realized that I don't think I've ever used those links. Which would you rather have? Blazing speed or a link to the next forum topic? Fortunately, the slow query is in a themable function, which means we can get our performance back with a few lines added to our theme's template.php:

// No previous/next links for forum topics.
function phptemplate_forum_topic_navigation($node) {
  return '';

In my case, server load dropped from 46 to 0.5. Sometimes it's the little things.



I think this done on months ago.


this is brilliant - thanks a million. I'll implement this on all my forum based sites right now. All one of them! ;)


John, what a brilliant tip! I often struggle with those links at the theme layer, since it's actually hard to make them look good and appear correctly.

But just getting rid of the buggers completely would make the designs cleaner, and now, apparently much faster.


Hi John,

Thanks very much! I was struggling with that problem.


I did this on my own site ages ago, and it's already in advanced forum. But I only did it because I couldn't stand the looks of it. I didn't realize I was speeding things up as well. Cool!


May be:
// No previous/next links for forum topics.
function phptemplate_forum_topic_navigation($node) {
return '';

In D6 forummodule this is much improved I believe? Please fill me in if I'm wrong here.