Fork me on GitHub

Comment by quy (2013-11-21 15:40:03)

Edited by quy (2014-03-14 23:43:54)

 1:  1:
 Revised code for performance gain when there are no tracked forums/topics: Revised code for performance gain when there are no tracked forums/topics:
 [code]  [code]
 if (!$pun_user['is_guest'])  if (!$pun_user['is_guest'])
 {  {
- $new_topics = $forums = array();  
-  
  $result = $db->query('SELECT f.id, f.last_post FROM '.$db->prefix.'forums AS f LEFT JOIN '.$db->prefix.'forum_perms AS fp ON (fp.forum_id=f.id AND fp.group_id='.$pun_user['g_id'].') WHERE (fp.read_forum IS NULL OR fp.read_forum=1) AND f.last_post>'.$pun_user['last_visit']) or error('Unable to fetch forum list', __FILE__, __LINE__, $db->error());   $result = $db->query('SELECT f.id, f.last_post FROM '.$db->prefix.'forums AS f LEFT JOIN '.$db->prefix.'forum_perms AS fp ON (fp.forum_id=f.id AND fp.group_id='.$pun_user['g_id'].') WHERE (fp.read_forum IS NULL OR fp.read_forum=1) AND f.last_post>'.$pun_user['last_visit']) or error('Unable to fetch forum list', __FILE__, __LINE__, $db->error());
- +
  if ($db->num_rows($result))   if ($db->num_rows($result))
  {   {
 + $forums = $new_topics = array();
  $tracked_topics = get_tracked_topics();   $tracked_topics = get_tracked_topics();
 +
 + while ($cur_forum = $db->fetch_assoc($result))
 + $forums[$cur_forum['id']] = $cur_forum['last_post'];
   
  if (empty($tracked_topics['forums']) && empty($tracked_topics['topics']))   if (empty($tracked_topics['forums']) && empty($tracked_topics['topics']))
- {  + $new_topics $forums;
- while ($cur_forum = $db->fetch_assoc($result))  +
- $new_topics[$cur_forum['id']] = 1;  +
- } +
  else   else
  {   {
- while ($cur_forum = $db->fetch_assoc($result))  + $result = $db->query('SELECT id, last_post, forum_id FROM '.$db->prefix.'topics WHERE forum_id IN('.implode(',', array_keys($forums)).') AND last_post>'.$pun_user['last_visit'].' AND moved_to IS NULL') or error('Unable to fetch new topics', __FILE__, __LINE__, $db->error());
- $forums[$cur_forum['id']] = $cur_forum['last_post']; +
   
- $result = $db->query('SELECT t.forum_id, t.id, t.last_post FROM '.$db->prefix.'topics AS t INNER JOIN '.$db->prefix.'forums AS f ON f.id=t.forum_id LEFT JOIN '.$db->prefix.'forum_perms AS fp ON (fp.forum_id=f.id AND fp.group_id='.$pun_user['g_id'].') WHERE (fp.read_forum IS NULL OR fp.read_forum=1) AND t.last_post>'.$pun_user['last_visit'].' AND t.moved_to IS NULL') or error('Unable to fetch new topics', __FILE__, __LINE__, $db->error());  
  while ($cur_topic = $db->fetch_assoc($result))   while ($cur_topic = $db->fetch_assoc($result))
  {   {
  if (!isset($new_topics[$cur_topic['forum_id']]) && (!isset($tracked_topics['forums'][$cur_topic['forum_id']]) || $tracked_topics['forums'][$cur_topic['forum_id']] < $forums[$cur_topic['forum_id']]) && (!isset($tracked_topics['topics'][$cur_topic['id']]) || $tracked_topics['topics'][$cur_topic['id']] < $cur_topic['last_post']))   if (!isset($new_topics[$cur_topic['forum_id']]) && (!isset($tracked_topics['forums'][$cur_topic['forum_id']]) || $tracked_topics['forums'][$cur_topic['forum_id']] < $forums[$cur_topic['forum_id']]) && (!isset($tracked_topics['topics'][$cur_topic['id']]) || $tracked_topics['topics'][$cur_topic['id']] < $cur_topic['last_post']))
  $new_topics[$cur_topic['forum_id']] = 1;   $new_topics[$cur_topic['forum_id']] = 1;
  }   }
  }   }
  }   }
 }  }
-  
 [/code] [/code]