问题描述:由于用户量访问量大或恶意用户频繁刷新列表,导致系统资源占用巨大或mysql出现慢查询,拖慢服务器运行。
故障原因:
经排查是source/class/table/table_forum_thread.php里的fetch_all_search函数生成的这个查询导致的
- public function fetch_all_search($conditions, $tableid = 0, $start = 0, $limit = 0, $order = '', $sort = 'DESC', $forceindex='') {
- $ordersql = '';
- if(!empty($order)) {
- $ordersql = " ORDER BY $order $sort ";
- }
- $data = array();
- $tlkey = !empty($conditions['inforum']) && !is_array($conditions['inforum']) ? $conditions['inforum'] : '';
- $firstpage = false;
- $defult = count($conditions) < 5 ? true : false;
- if(count($conditions) < 5) {
- foreach(array_keys($conditions) as $key) {
- if(!in_array($key, array('inforum', 'sticky', 'displayorder', 'intids'))) {
- $defult = false;
- break;
- }
- }
- }
- if(!defined('IN_MOBILE') && $defult && $conditions['sticky'] == 4 && $start == 0 && $limit && strtolower(preg_replace("/\s?/ies", '', $order)) == 'displayorderdesc,lastpostdesc' && empty($sort)) {
- foreach($conditions['displayorder'] as $id) {
- if($id < 2) {
- $firstpage = true;
- if($id < 0) {
- $firstpage = false;
- break;
- }
- }
- }
- if($firstpage && !empty($tlkey) && ($ttl = getglobal('setting/memory/forum_thread_forumdisplay')) !== null && ($data = $this->fetch_cache($tlkey, 'forumdisplay_')) !== false) {
- $delusers = $this->fetch_cache('deleteuids', '');
- if(!empty($delusers)) {
- foreach($data as $tid => $value) {
- if(isset($delusers[$value['authorid']])) {
- $data = array();
- }
- }
- }
- if($data) {
- return $data;
- }
- }
- }
- $data = DB::fetch_all("SELECT * FROM ".DB::table($this->get_table_name($tableid))." $forceindex".$this->search_condition($conditions)." $ordersql ".DB::limit($start, $limit));
- if($firstpage && !empty($tlkey) && ($ttl = getglobal('setting/memory/forum_thread_forumdisplay')) !== null) {
- $this->store_cache($tlkey, $data, $ttl, 'forumdisplay_');
- }
- return $data;
- }
复制代码
解决办法:改了一下最后那里的sql,判断如果是那个无条件的慢查询就不执行了 |