From 8a7690af5723ec22fccd450097cc8fb0e8216649 Mon Sep 17 00:00:00 2001
From: Dries Buytaert <dries@buytaert.net>
Date: Wed, 21 Jun 2000 11:03:54 +0000
Subject: [PATCH] Wheep! Wheep! Major performance optimization for index.php:
 we reduced the number of SQL on the mainpage with factor 10 and shortened the
 code with 4 lines.  There is only *one* optimized SQL query left, being:

$result = db_query("SELECT stories.*, COUNT(comments.sid) AS comments FROM
stories LEFT JOIN comments ON stories.sid = comments.sid WHERE
stories.status = 1 AND stories.time <= $date GROUP BY stories.sid ORDER BY
stories.sid DESC LIMIT $number");

 Note: we use a LEFT JOIN to retrieve the number of comments associated
       with each story.  By retrieving this data now, we elimate a *lot*
       of individual queries that would otherwise be required inside the
       while-loop.  If there is no matching record for the right table in
       the ON-part of the LEFT JOIN, a row with all columns set to NULL
       is used for the right table.  This is required, as not every story
       has a counterpart in the comments table (at a given time).
---
 index.php | 14 ++++++++++----
 1 file changed, 10 insertions(+), 4 deletions(-)

diff --git a/index.php b/index.php
index 70b20ac89c61..ccabdf7d1d2d 100644
--- a/index.php
+++ b/index.php
@@ -10,10 +10,18 @@
 $date = ($date) ? $date : time();
 
 ### Perform query:
-$result = db_query("SELECT * FROM stories WHERE status = 1 AND time <= $date ORDER BY sid DESC LIMIT $number");
+$result = db_query("SELECT stories.*, COUNT(comments.sid) AS comments FROM stories LEFT JOIN comments ON stories.sid = comments.sid WHERE stories.status = 1 AND stories.time <= $date GROUP BY stories.sid ORDER BY stories.sid DESC LIMIT $number");
+  // Note: we use a LEFT JOIN to retrieve the number of comments associated 
+  //       with each story.  By retrieving this data now, we elimate a *lot* 
+  //       of individual queries that would otherwise be required inside the 
+  //       while-loop.  If there is no matching record for the right table in 
+  //       the ON-part of the LEFT JOIN, a row with all columns set to NULL 
+  //       is used for the right table.  This is required, as not every story 
+  //       has a counterpart in the comments table (at a given time).
 
 ### Display stories:
 while ($story = db_fetch_object($result)) {
+
   ### Compose more-link:
   $morelink = "[ ";
   if ($story->article) {
@@ -23,13 +31,11 @@
     $bytes = strlen($story->article);
     $morelink .= "\"><FONT COLOR=\"$theme->hlcolor2\"><B>read more</B></FONT></A> | $bytes bytes in body | "; 
   }
-  $query = db_query("SELECT sid FROM comments WHERE sid = $story->sid");
-  if (!$query) { $count = 0; } else { $count = mysql_num_rows($query); }
   $morelink .= "<A HREF=\"article.php?sid=$story->sid";
   if (isset($user->umode)) { $morelink .= "&mode=$user->umode"; } else { $morelink .= "&mode=threaded"; }
   if (isset($user->uorder)) { $morelink .= "&order=$user->uorder"; } else { $morelink .= "&order=0"; }
   if (isset($user->thold)) { $morelink .= "&thold=$user->thold"; } else { $morelink .= "&thold=0"; }
-  $morelink .= "\"><FONT COLOR=\"$theme->hlcolor2\">$count comments</FONT></A> ]";
+  $morelink .= "\"><FONT COLOR=\"$theme->hlcolor2\">$story->comments comments</FONT></A> ]";
 
   ### Display story:
   $theme->abstract($story->aid, $story->informant, $story->time, stripslashes($story->subject), stripslashes($story->abstract), stripslashes($story->comments), $story->category, $story->department, $morelink);
-- 
GitLab