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).3-00
parent
d64e3f6d74
commit
8a7690af57
14
index.php
14
index.php
|
@ -10,10 +10,18 @@ $number = ($user->storynum) ? $user->storynum : 10;
|
|||
$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 @@ while ($story = db_fetch_object($result)) {
|
|||
$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);
|
||||
|
|
Loading…
Reference in New Issue