|

- 帖子
- 48
- 积分
- 50
- 威望
- 1
- 金币
- 1 个
- 体力
- 54
|
缓存你的数据查询语句1个+减少8个查询 【声明】这是我辛苦买来的,版权规作者所有
缓存你的数据查询语句1个+减少8个查询
只支持DZ6.0不支持UC版本第一个:
common.inc.php
中
找到:
if(empty($tid)) {
$query = $db->query("SELECT f.fid, f.*, ff.* $accessadd1 $modadd1, f.fid AS fid
FROM {$tablepre}forums f
LEFT JOIN {$tablepre}forumfields ff ON ff.fid=f.fid $accessadd2 $modadd2
WHERE f.fid='$fid'");
$forum = $db->fetch_array($query);
} else {
改成:
if(empty($tid)) {
$sql = "SELECT f.fid, f.*, ff.* $accessadd1 $modadd1, f.fid AS fid
FROM {$tablepre}forums f
LEFT JOIN {$tablepre}forumfields ff ON ff.fid=f.fid $accessadd2 $modadd2
WHERE f.fid='$fid'";
$f_c_file = DISCUZ_ROOT.'./forumdata/cache/sql_'.md5($sql).'.php';
if((file_exists($f_c_file) && $timestamp-filemtime($f_c_file)>15*60) || !file_exists($f_c_file)){
$forum = $db->fetch_array($db->query($sql));
require_once DISCUZ_ROOT.'./include/cache.func.php';
$cachedata = '$f_cachetime='.$timestamp.";\r\n";
$cachedata .= '$forum='.var_export($forum, TRUE).";\r\n";
writetocache(md5($sql), '', $cachedata, $prefix = 'sql_');
}
require_once $f_c_file;
} else {
注意:
上面的语句设定缓存时间为15分钟,您可以改成您需要的值
======================================================================================================
第二个:
DZ发贴减少两个查询
post.php
减少一:
找到:
if(!$adminid && $newbiespan && (!$lastpost || $timestamp - $lastpost < $newbiespan * 3600)) {
$query = $db->query("SELECT regdate FROM {$tablepre}members WHERE uid='$discuz_uid'");
if($timestamp - ($db->result($query, 0)) < $newbiespan * 3600) {
showmessage('post_newbie_span');
}
}
改成:
if(!$adminid && $newbiespan && (!$lastpost || $timestamp - $lastpost < $newbiespan * 3600)) {
//$query = $db->query("SELECT regdate FROM {$tablepre}members WHERE uid='$discuz_uid'");
//if($timestamp - ($db->result($query, 0)) < $newbiespan * 3600) {
if($timestamp - $regdate < $newbiespan * 3600) {
showmessage('post_newbie_span');
}
}
需要在common.inc.php把:
$membertablefields = 'm.uid AS discuz_uid, m.username AS discuz_user, m.password AS discuz_pw, m.secques AS discuz_secques,
m.adminid, m.groupid, m.groupexpiry, m.extgroupids, m.email, m.timeoffset, m.tpp, m.ppp, m.posts, m.digestposts,
m.oltime, m.pageviews, m.credits, m.extcredits1, m.extcredits2, m.extcredits3, m.extcredits4, m.extcredits5,
m.extcredits6, m.extcredits7, m.extcredits8, m.timeformat, m.dateformat, m.pmsound, m.sigstatus, m.invisible,
m.lastvisit, m.lastactivity, m.lastpost, m.newpm, m.accessmasks, m.xspacestatus, m.editormode, m.customshow';
的查询语句增加一个m.regdate
如下:
$membertablefields = 'm.uid AS discuz_uid, m.username AS discuz_user, m.password AS discuz_pw, m.secques AS discuz_secques,
m.adminid, m.groupid, m.groupexpiry, m.extgroupids, m.regdate, m.email, m.timeoffset, m.tpp, m.ppp, m.posts, m.digestposts,
m.oltime, m.pageviews, m.credits, m.extcredits1, m.extcredits2, m.extcredits3, m.extcredits4, m.extcredits5,
m.extcredits6, m.extcredits7, m.extcredits8, m.timeformat, m.dateformat, m.pmsound, m.sigstatus, m.invisible,
m.lastvisit, m.lastactivity, m.lastpost, m.newpm, m.accessmasks, m.xspacestatus, m.editormode, m.customshow';
减少二:
把:
if($forum['type'] == 'sub') {
$query = $db->query("SELECT name, fid FROM {$tablepre}forums WHERE fid='$forum[fup]'");
$fup = $db->fetch_array($query);
$navigation = "» [url=http://www.discuz.net/%22forumdisplay.php?fid=$fup[fid]\]$fup[name][/url] $navigation";
$navtitle = $navtitle.strip_tags($fup['name']).' - ';
}
改成:
*///减少一个查询
if($forum['type'] == 'sub') {
$fup['fid'] = $forum['fup'];
@require_once DISCUZ_ROOT.'./forumdata/cache/cache_forums.php';//很重要
$fup['name'] = $_DCACHE['forums'][$forum['fup']]['name'];
if (empty($fup['name'])){
$query = $db->query("SELECT name, fid FROM {$tablepre}forums WHERE fid='$forum[fup]'");
$fup = $db->fetch_array($query);
}
$navigation = "» [url=http://www.discuz.net/%22forumdisplay.php?fid=$fup[fid]\]$fup[name][/url] $navigation";
$navtitle = $navtitle.strip_tags($fup['name']).' - ';
}
///结束
=====================================================================================
第三个:
viewthread.php
浏览子版块减少一个查询/如果没有子板块没什么影响
把:
if($forum['type'] == 'sub') {
$query = $db->query("SELECT fid, name FROM {$tablepre}forums WHERE fid='$forum[fup]'");
$fup = $db->fetch_array($query);
$navigation = '» '.$fup[name].' '.$navigation;
$navtitle = $navtitle.' - '.strip_tags($fup['name']);
}
改成:
if($forum['type'] == 'sub') {
$fup['fid'] = $forum['fup'];
$fup['name'] = $_DCACHE['forums'][$forum['fup']]['name'];
if (empty($fup['name'])){
$query = $db->query("SELECT fid, name FROM {$tablepre}forums WHERE fid='$forum[fup]'");
$fup = $db->fetch_array($query);
}
$navigation = '» '.$fup[name].' '.$navigation;
$navtitle = $navtitle.' - '.strip_tags($fup['name']);
}
注明:下面的查询影响不大,您可以不做修改,但如果你很在意数据库查询,可以按照下面的修改!
====================================================================
第四个:
pm.php减少一个查询
把:
if(!$adminid && $newbiespan && (!$lastpost || $timestamp - $lastpost < $newbiespan * 3600)) {
$query = $db->query("SELECT regdate FROM {$tablepre}members WHERE uid='$discuz_uid'");
if($timestamp - ($db->result($query, 0)) < $newbiespan * 3600) {
showmessage('pm_newbie_span', NULL, 'HALTED');
}
}
改成:
if(!$adminid && $newbiespan && (!$lastpost || $timestamp - $lastpost < $newbiespan * 3600)) {
//$query = $db->query("SELECT regdate FROM {$tablepre}members WHERE uid='$discuz_uid'");
//if($timestamp - ($db->result($query, 0)) < $newbiespan * 3600) {
if($timestamp - $regdate < $newbiespan * 3600) {//common.inc.php需加m.regdate
showmessage('pm_newbie_span', NULL, 'HALTED');
}
}
如果使用发帖减少查询,common.inc.php不修改,否则按照post.php的修改
=====================================================================
第五个/第六个:
misc.php/topicadmin.php各减少一个查询
把:
$query = $db->query("SELECT name, fid FROM {$tablepre}forums WHERE fid='$forum[fup]'");
$fup = $db->fetch_array($query);
改成:
$fup['fid'] = $forum['fup'];
@require_once DISCUZ_ROOT.'./forumdata/cache/cache_forumdisplay.php';//很重要
$fup['name'] = $_DCACHE['forums'][$forum['fup']]['name'];
unset($_DCACHE['forums']);
if (empty($fup['name'])){
$query = $db->query("SELECT name, fid FROM {$tablepre}forums WHERE fid='$forum[fup]'");
$fup = $db->fetch_array($query);
}
================================================================
第七个/第八个
register.php
logging.php
找:
$query = $db->query("SELECT styleid, name FROM {$tablepre}styles WHERE available='1'");
while($styleinfo = $db->fetch_array($query)) {
$styleselect .= ''.$styleinfo['name'].''."\n";
}
改成:
if(is_array($stylejump) && stylejumpstatus) {
foreach($stylejump as $key => $var){
$styleselect .= "$var\n";
}
}
=========================================================
第九个:
memcp.php
把:
$query = $db->query("SELECT styleid, name FROM {$tablepre}styles WHERE available='1'");
while($style = $db->fetch_array($query)) {
$styleselect .= "<OPTION value='\"$style[styleid]\"' ?.
">$style[name]\n";
}
改成:
if(is_array($stylejump)) {
foreach($stylejump as $key => $var) {
$styleselect .= "<OPTION value='\"$key\"' ?.
">$var\n";
}
} |
|