原因:旧版本每次显示论坛索引页都需要该页的话题全部计算大小,所以会对数据库服务器造成若干的影响。
优点:没有增加任何的数据库查询(query)次数,完全不没有额外增加数据库的负担;顺便还优化了论坛公告的数据库查询。
自评:作出这个HACK是使用循环的方法统计话题所有帖子大小的和,发布的时候使用数据库的统计功能,但还是要额外的增加一次查询,一个话题一次查询,每页显示30个话题的话,就增加了30次的查询,实在不划算。为了奥索的数据库,我就再针对此优化了查询部分,将其浓缩到本身的话题查询当中,这样,这个HACK就没有额外使用任何的查询动作。数据库就此轻松了!
步骤:
一、升级篇(曾经使用过我的话题大小HACK的适用)
⒈打开 forumdisplay.php ,查找“
if ($announcement = $DB_site->query_first(" SELECT announcementid, userid, startdate, title FROM announcement WHERE startdate <= $datenow AND enddate >= $datenow AND (forumid = $forumid OR forumid = -1) ORDER BY startdate DESC LIMIT 1")) {
”,替换为“
if ( $announcement = $DB_site-> query_first(" SELECT announcementid, user.userid, startdate, title, LENGTH(pagetext) AS sizes, user.username AS username, user.usertitle AS usertitle FROM announcement LEFT JOIN user ON ( user.userid = announcement.userid ) WHERE startdate <= $datenow AND enddate >= $datenow AND (forumid = $forumid OR forumid=-1) ORDER BY startdate DESC LIMIT 1")) {
”,查找“
// Hack thread size
$announcements = $DB_site-> query_first (" SELECT SUM( LENGTH(pagetext)) AS sizes FROM announcement WHERE startdate<=$datenow AND enddate >= $datenow AND (forumid = $forumid OR forumid=-1)");
$size=$announcements[sizes];
$size = iif($size>1024,number_format($size/1024,1)."K",$size);
// End Hack thread size
”,替换为“
// Hack thread size
$size=$announcement[sizes];
$size=iif($size>1024,number_format($size/1024,1)."K",$size)."B";
// End Hack thread size
”,查找“
$announceuser = $DB_site->query_first(" SELECT username,usertitle FROM user WHERE userid=$announcement[userid]");
$announceusertitle=$announceuser[usertitle];
$announceposter=$announceuser[username];
”,替换为“
$announceusertitle=$announcement[usertitle];
$announceposter=$announcement[username];
”,查找“
$threads = $DB_site-> query("SELECT thread.iconid, icon.title AS icontitle, icon.iconpath,
threadid, thread.title AS title, open, lastpost, replycount, postusername, lastposter,
notes, views, userindex FROM thread LEFT JOIN icon ON ( icon.iconid = thread.iconid ) WHERE
forumid=$forumid AND visible=1 $datecut ORDER BY lastpost DESC LIMIT $limitlower,$perpage");
”(增加了话题置顶HACK的有两处,而且在第五行都略有不同),替换为“
if ( $threads = $DB_site-> query("SELECT thread.iconid, icon.title AS icontitle, icon.iconpath, thread.threadid, thread.title AS title, open, lastpost, replycount, postusername, lastposter, notes, views, userindex, SUM(LENGTH(post.pagetext)) AS sizes FROM thread LEFT JOIN icon ON (icon.iconid = thread.iconid) LEFT JOIN post ON (post.threadid = thread.threadid) WHERE forumid=$forumid AND thread.visible=1 $datecut GROUP BY post.threadid ORDER BY lastpost DESC LIMIT $limitlower,$perpage"));
”(增加了话题置顶HACK的,第一个在WHERE最后增加 AND open>1,第二个增加 AND open<2)。查找“
// Hack thread size
$posts=$DB_site->query_first("SELECT SUM(LENGTH(pagetext)) AS sizes FROM post WHERE threadid=".$thread[threadid]);
$size=$posts[sizes];
$size=iif($size>1024,number_format($size/1024,1)."K",$size)."B";
// End Hack thread size
”(增加了话题置顶HACK的有两处相同的),替换为“
// Hack thread size
$size=$thread[sizes];
$size=iif($size>1024,number_format($size/1024,1)."K",$size)."B";
// End Hack thread size
”。保存退出;完成!
二、第一次使用篇
⒈打开 forumdisplay.php ,查找“
if ($announcement = $DB_site-> query_first("SELECT announcementid, userid, startdate, title FROM announcement WHERE startdate <= $datenow AND enddate>=$datenow AND (forumid=$forumid OR forumid=-1) ORDER BY startdate DESC LIMIT 1")) {
$announceuser=$DB_site->query_first("SELECT username, usertitle FROM user WHERE userid = $announcement[userid]");
$announceusertitle = $announceuser[usertitle];
$announceposter = $announceuser[username];
”,替换为“
if ($announcement = $DB_site->query_first("SELECT announcementid, user.userid, startdate, title, LENGTH(pagetext) AS sizes, user.username AS username, user.usertitle AS usertitle FROM announcement LEFT JOIN user ON ( user.userid = announcement.userid) WHERE startdate<=$datenow AND enddate>=$datenow AND (forumid=$forumid OR forumid=-1) ORDER BY startdate DESC LIMIT 1")) {
// Hack thread size
$size=$announcement[sizes];
$size=iif($size>1024,number_format($size/1024,1)."K",$size)."B";
// End Hack thread size
$announceusertitle=$announcement[usertitle];
$announceposter=$announcement[username];
”,查找“
$threads=$DB_site->query("SELECT thread.iconid, icon.title AS icontitle, icon.iconpath, threadid, thread.title AS title, open, lastpost, replycount, postusername, lastposter, notes, views, userindex FROM thread LEFT JOIN icon ON (icon.iconid = thread.iconid) WHERE forumid=$forumid AND visible=1 $datecut ORDER BY lastpost DESC LIMIT $limitlower,$perpage");
”,替换为“
if ($threads=$DB_site->query("SELECT thread.iconid, icon.title AS icontitle, icon.iconpath, thread.threadid, thread.title AS title, open, lastpost, replycount, postusername, lastposter, notes, views, userindex, SUM(LENGTH(post.pagetext)) AS sizes FROM thread LEFT JOIN icon ON (icon.iconid = thread.iconid) LEFT JOIN post ON (post.threadid = thread.threadid) WHERE forumid=$forumid AND open>1 AND thread.visible=1 $datecut GROUP BY post.threadid ORDER BY lastpost DESC LIMIT $limitlower,$perpage"));
”(增加了话题置顶HACK的参考升级提示),查找“
while ($thread=$DB_site->fetch_array($threads)) {
”,在后面加入“
// Hack thread size
$size=$thread[sizes];
$size=iif($size>1024,number_format($size/1024,1)."K",$size)."B";
// End Hack thread size
”,保存退出;
⒉在《控制面板》打开 forumdisplay 模板,查找“
<td align=center><smallfont color="{tableheadtextcolor}"><B>回复</B></smallfont>
</td>
<td align=center><smallfont color="{tableheadtextcolor}"><B>人气</B></smallfont>
</td>
”,在后面加入“
<td align=center><smallfont color="{tableheadtextcolor}"><B>大小</B></smallfont>
</td>
”,查找“
<TD valign="middle" width="100%" colspan="7">
”,替换为“
<TD valign="middle" width="100%" colspan="8">
”保存。打开 forumdisplaybit 模板,查找“
<td align=center bgcolor="{firstaltcolor}">
<normalfont>$replies</normalfont>
</td>
<td align=center bgcolor="{secondaltcolor}">
<normalfont>$views</normalfont>
</td>
”,在后面加入“
<td align=center bgcolor="{secondaltcolor}">
<normalfont>$size</normalfont>
</td>
”,打开 forumdisplay_announcement 模板,查找“
<td align=center bgcolor="{firstaltcolor}">
<normalfont>-</normalfont>
</td>
<td align=center bgcolor="{secondaltcolor}">
<normalfont>-</normalfont>
</td>
”,在后面加入“
<td align=center bgcolor="{secondaltcolor}">
<normalfont>$size</normalfont>
</td>
”,保存。完成!
|