$order = isset($order) && in_array($order, array('credits','gender','username')) ? $order : '';代码: member.php=90
switch($order) { case 'credits': $orderadd = "ORDER BY credits DESC"; break; case 'gender': $orderadd = "ORDER BY gender DESC"; break; case 'username': $orderadd = "ORDER BY username DESC"; break; default: $orderadd = 'ORDER BY uid'; $order = 'uid'; break; }点评: 统计选项->会员列表无法根据注册日期排序。
$order = isset($order) && in_array($order, array('credits','gender','username', 'regdate')) ? $order : '';line90修改为
switch($order) { case 'credits': $orderadd = "ORDER BY credits DESC"; break; case 'gender': $orderadd = "ORDER BY gender DESC"; break; case 'username': $orderadd = "ORDER BY username DESC"; break; case 'regdate': $orderadd = " ORDER BY regdate DESC"; break; // ADD default: $orderadd = 'ORDER BY uid'; $order = 'uid'; break; }
$query = $db->query("SELECT * FROM {$tablepre}threadtypes WHERE typeid IN ($typeids) AND special='' ORDER BY displayorder");点评: 牛头不对马嘴,special字段明明是int类型却去搜索''空字符串,还好这个表不会大,不然坑死人不偿命
$db->query("UPDATE {$tablepre}threads SET attachment='0' WHERE tid IN ($tids)".($attachtids ? " AND tid NOT IN ($attachtids)" : NULL));代码: admin/attach.inc.php=176
$db->query("UPDATE {$tablepre}posts SET attachment='0' WHERE pid IN ($pids)".($attachpids ? " AND pid NOT IN ($attachpids)" : NULL));
---------------------------------------------------------------------------------------------
类型: 多余操作
$query = $db->query("SELECT tid FROM {$tablepre}attachments WHERE tid IN ($tids) GROUP BY tid ORDER BY pid DESC");点评: 可使用“SELECT DISTINCT”来替代“GROUP BY”,可“ORDER BY”是啥意思呢?相关操作对结果集顺序并未有要求,多余的排序操作将会耗费CPU能力与内存占用,结果将增加数据库负载。只不过一般一个主题不会有海量附件,所以性能下降不明显。
do{ $query = $db->query("SELECT f.name AS forumname, f.allowsmilies, f.allowhtml, f.allowbbcode, f.allowimgcode, t.tid, t.fid, t.authorid, t.author, t.subject, t.views, t.replies, t.dateline, p.message, p.useip, p.attachment, p.htmlon, p.smileyoff, p.bbcodeoff, tm.uid AS moduid, tm.username AS modusername, tm.dateline AS moddateline, tm.action AS modaction FROM {$tablepre}threads t LEFT JOIN {$tablepre}posts p ON p.tid=t.tid AND p.first='1' LEFT JOIN {$tablepre}threadsmod tm ON tm.tid=t.tid LEFT JOIN {$tablepre}forums f ON f.fid=t.fid WHERE t.displayorder='-1' $sql GROUP BY t.tid ORDER BY t.dateline DESC LIMIT $ppp OFFSET ".(($pagetmp - 1) * $ppp)); $pagetmp--; } while(!$query->rowCount() && $pagetmp);点评: "GROUP BY t.tid"是多余的,因为主表是threads tid是PK,上方line45还有一处类似。也许此段代码的大哥喜欢做菜。可厨艺不精,不知道什么时候该放什么调料,于是手边的调料瓶就都拿起来倒两下,只要味道不难吃这菜就算完成了。写代码也如此,估摸着写着写着忽然想起SQL还有“GROUP BY”的功能,随手拈来捣入SQL中搅和搅和,结果正确味道正好。遂顿悟,不会做菜的厨子不是个好程序猿 :D
$query = $db->query("SELECT author, COUNT(*) AS posts FROM {$tablepre}posts WHERE dateline>='$timestamp'-86400 AND invisible='0' AND authorid>'0' GROUP BY author ORDER BY posts DESC LIMIT 1");点评: “AND authorid>'0'” 条件可以删除掉。这个条件毫无意义,只会让数据库在抓取row时过滤条件多一个结果却没差别。
$query = $db->query("SELECT t.tid,t.fid,t.readperm,t.author,t.authorid,t.subject,t.dateline,t.lastpost,t.lastposter,t.views,t.replies,t.highlight,t.digest,t.typeid,t.sortid $sqlfrom WHERE t.readperm='0' $sql AND t.displayorder>='0' AND t.fid>'0' <-------- $attachadd ORDER BY t.$orderby DESC LIMIT $items OFFSET $startrow " );
extract($db->fetch_first("SELECT COUNT(*) AS threadcount, SUM(t.replies)+COUNT(*) AS replycount FROM {$tablepre}threads t, {$tablepre}forums f WHERE f.fid='$fid' AND t.fid=f.fid AND t.displayorder>='0'"));点评: 其实没用到forums表的数据,对forums表的连接完全是多余的
extract($db->fetch_first("SELECT COUNT(*) AS threadcount, SUM(replies)+COUNT(*) AS replycount FROM {$tablepre}threads WHERE fid='$fid' AND displayorder>='0'"));
$queryt = $db->query("SELECT uid FROM {$tablepre}members LIMIT $current, $pertask");点评: 查询时SQL不严格未使用ORDER BY,导致结果集、结果顺序不确定。此页面多个SQL均存在这个问题, 会导致分页结果不可预料,尤其是提取帖子(精华)分页时!
$queryt = $db->query("SELECT uid FROM {$tablepre}members ORDER BY uid LIMIT $current, $pertask");
$specialadd2 .= "AND (dp.stand='0' OR dp.stand IS NULL OR p.first='1')";代码: viewthread.php=378
$thread['replies'] = $sdb->result_first("SELECT COUNT(*) FROM {$tablepre}posts p LEFT JOIN {$tablepre}debateposts dp ON p.pid=dp.pid WHERE p.tid='$tid' AND (dp.stand='0' OR dp.stand IS NULL)");代码: include/task.func.php=134
$nextnewbietaskid = intval($db->result_first("SELECT t.taskid FROM {$tablepre}tasks t LEFT JOIN {$tablepre}mytasks mt ON mt.taskid=t.taskid AND mt.uid='$discuz_uid' WHERE mt.taskid IS NULL AND t.available='2' AND t.newbietask='1' ORDER BY t.newbietask DESC LIMIT 1"));
点评: 会数据库的应该知道NULL值不会走索引,除非建立ISNULL索引,作NULL查询将会扫全表导致性能暴跌! DZ数据库建表风格是都采用NOT NULL约束,PHP代码风格也是不做NULL的判断。在字段已经明确NOT NULL约束条件下还采用(dp.stand='0' OR dp.stand IS NULL)这样条件,对mt.taskid不使用mt.taskid>0判断,如果不是临时工干的那就基本上是存心考古的……
FIX: include/task.func.php=134不能简单删除 ISNULL判断,否则将导致新手任务无法结束,把 mt.taskid IS NULL 改成 mt.taskid = 0 即可
$uids = 0;代码: admin/prune.inc.php~220
$forums = '0';代码: admin/prune.inc.php~230
$uids = '-1';代码: viewthreads.php~220
$attachpids = -1;代码: topicadmin.php~102 前台删除帖子
$pids = 0;代码: topicadmin.php~109 前台删除帖子
$pids .= ','.$post['pid'];代码: admin/threads.inc.php~622
$tids = 0;代码: admin/forums.inc.php~1289
$query = $db->query("SELECT * FROM {$tablepre}threadtypes WHERE typeid IN ($typeids) AND special='' ORDER BY displayorder");代码: modcp/moderate.inc.php~286
WHERE pid IN (0,".implode(',', $pidarray).")");代码: admin/moderate.inc.php=727
$db->query("UPDATE {$tablepre}posts SET invisible='0' WHERE pid IN (0,".implode(',', $pidarray).")");代码: include/misc.func.php~289
$db->query("UPDATE $tablepre$table SET $viewscol=$viewscol+'$views' WHERE $idcol IN (0$ids)" );代码:
$str = $comma = ''; foreach (..) { $str .= $comma. 'something'; $comma = ','; }
$forumstickycount = $stickycount = $stickytids = 0;点评: 对tid搜索包含0, 版块精华SQL类似如下,将会导致扫全索引. 并且影响到即使没有全局置顶主题也会做同样查询,非常坑爹
SELECT t.* FROM cdb_threads t WHERE t.tid IN (0,110) AND t.displayorder IN (2, 3, 4) ORDER BY displayorder DESC, lastpost DESC LIMIT 1 OFFSET 0
if(($start_limit && $start_limit > $stickycount) || !$stickycount || $filterbool) {
之前加上过滤
if ($stickytids) { $tarr = array(); $stickytids = explode(',', $stickytids); foreach ($stickytids as $s_id) { $s_id = intval($s_id) && $s_id > 0 && $tarr[] = $s_id; } $stickytids = implode(',', $tarr); unset($tarr); } else { $stickytids = ''; }
$querysticky = $sdb->query("SELECT t.* FROM {$tablepre}threads t WHERE t.tid IN ($stickytids) AND t.displayorder IN (2, 3, 4) ORDER BY displayorder DESC, $orderby $ascdesc LIMIT $start_limit, ".($stickycount - $start_limit < $tpp ? $stickycount - $start_limit : $tpp));修改成:
if ($stickytids) { $querysticky = $sdb->query("SELECT t.* FROM {$tablepre}threads t WHERE t.tid IN ($stickytids) AND t.displayorder IN (2, 3, 4) ORDER BY displayorder DESC, $orderby $ascdesc LIMIT ".($stickycount - $start_limit < $tpp ? $stickycount - $start_limit : $tpp). ' OFFSET '. $start_limit); } else { $querysticky = false; }
---------------------------------------------------------------------------------------------
类型: 多余连接
坑爹指数: ★★★
代码: include/requres.func.php~450
case 'weekposts': $week = gmdate('w', $timestamp) - 1; $week = $week != -1 ? $week : 6; $historytime = mktime(0, 0, 0, date('m', $timestamp), date('d', $timestamp) - $week, date('Y', $timestamp)); $sql = "SELECT DISTINCT (p.author) AS username,p.authorid AS uid,COUNT(p.pid) AS postnum FROM {$tablepre}posts p LEFT JOIN {$tablepre}memberfields mf ON mf.uid = p.authorid WHERE p.dateline>=$historytime GROUP BY p.authorid, p.author ORDER BY postnum DESC";
点评: 输出字段并未使用memberfields表中字段,貌似MySQL在这种情况下并不会因此而放弃连接操作,对此表的连接徒耗资源。
---------------------------------------------------------------------------------------------
类型: 逻辑错误
坑爹指数: ★
代码: admin/recyclebin.inc.php~145
$threadcount = $db->result_first("SELECT count(*) FROM {$tablepre}threads t LEFT JOIN {$tablepre}threadsmod tm ON tm.tid=t.tid WHERE t.displayorder='-1' $sql");
点评: 此SQL未对threadsmod作如下do while()循环中类似的GROUP BY处理,如果一个回收站中主题被反复删除恢复那么就会出现“符合条件的回收站主题数”不为0但下方无列表显示这种情况
FIX1: 添加一条GROUP BY语句过滤threadsmod的结果。但限于MySQL不标准的GROUP BY语法,其结果(顺序)也许非正确
$threadcount = $db->result_first("SELECT count(*) FROM {$tablepre}threads t LEFT JOIN {$tablepre}threadsmod tm ON tm.tid=t.tid GROUP BY tm.tid ORDER BY tm.dateline DESC, t.dateline DESC WHERE t.displayorder='-1' $sql");
版权曾经拥有,欢迎网上分享
转载请保留链接 http://my.oschina.net/u/126398/blog/39255