首页 emlog用户过多导致发布文章太慢的解决办法

emlog用户过多导致发布文章太慢的解决办法

jaeheng 2020-2-10 0 55
标签: emlog

emlog用户过多,会导致站点统计缓存的时候太慢,具体表现为发布文章,更新数据,评论之类的写操作时很慢。原因是站点统计的函数存在大数据量循环语句中进行mysql连接查询的问题,查询效率低下。通过修改mc_sta函数可以修复:


/**
	 * 站点统计缓存
	 */
	private function mc_sta() {
		$sta_cache = array();
		$data = $this->db->once_fetch_array("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "blog WHERE type='blog' AND hide='n' AND checked='y' ");
		$lognum = $data['total'];

		$data = $this->db->once_fetch_array("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "blog WHERE type='blog' AND hide='y'");
		$draftnum = $data['total'];		

		$data = $this->db->once_fetch_array("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "blog WHERE type='blog' AND hide='n' AND checked='n' ");
		$checknum = $data['total'];			

  		$data = $this->db->once_fetch_array("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "comment WHERE hide='n' ");
		$comnum = $data['total'];	

  		$data = $this->db->once_fetch_array("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "comment WHERE hide='y' ");
		$hidecom = $data['total'];

		$data = $this->db->once_fetch_array("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "twitter ");
		$twnum = $data['total'];

		$sta_cache = array(
			'lognum' => $lognum,
			'draftnum' => $draftnum,
			'comnum' => $comnum,
			'comnum_all' => $comnum + $hidecom,
			'twnum' => $twnum,
			'hidecomnum' => $hidecom,
            'checknum' => $checknum,
		);

		$logNumQuery = $this->db->query("SELECT author,COUNT(*) AS total FROM " . DB_PREFIX . "blog WHERE hide='n' and type='blog' group by author");
		$draftNumQuery = $this->db->query("SELECT author,COUNT(*) AS total FROM " . DB_PREFIX . "blog WHERE hide='y' and type='blog' group by author");
		$commentNumQuery = $this->db->query("SELECT author,COUNT(*) AS total FROM " . DB_PREFIX . "comment AS a, " . DB_PREFIX . "blog AS b WHERE a.gid = b.gid group by b.author");
		$hidecommentNumQuery = $this->db->query("SELECT author,COUNT(*) AS total FROM " . DB_PREFIX . "comment AS a, " . DB_PREFIX . "blog AS b WHERE a.gid = b.gid and a.hide='y' group by b.author");
		$twnumQuery = $this->db->query("SELECT author,COUNT(*) AS total FROM " . DB_PREFIX . "twitter group by author");

		$dataHash = array(
			'lognum' => array(),
			'draftnum' => array(),
			'commentnum' => array(),
			'hidecommentnum' => array(),
			'twnum' => array(),
		);

		while ($row = $this->db->fetch_array($logNumQuery)) {
			$dataHash['lognum'][$row['author']] = $row['total'];
		}

		while ($row = $this->db->fetch_array($draftNumQuery)) {
			$dataHash['draftnum'][$row['author']] = $row['total'];
		}
		while ($row = $this->db->fetch_array($commentNumQuery)) {
			$dataHash['commentnum'][$row['author']] = $row['total'];
		}
		while ($row = $this->db->fetch_array($hidecommentNumQuery)) {
			$dataHash['hidecommentnum'][$row['author']] = $row['total'];
		}
		while ($row = $this->db->fetch_array($twnumQuery)) {
			$dataHash['twnum'][$row['author']] = $row['total'];
		}

		$query = $this->db->query("SELECT uid FROM " . DB_PREFIX . "user");
		while ($row = $this->db->fetch_array($query)) {
			$sta_cache[$row['uid']] = array(
				'lognum' => isset($dataHash['lognum'][$row['uid']]) ? $dataHash['lognum'][$row['uid']] : 0,
				'draftnum' => isset($dataHash['draftnum'][$row['uid']]) ? $dataHash['draftnum'][$row['uid']] : 0,
				'commentnum' => isset($dataHash['commentnum'][$row['uid']]) ? $dataHash['commentnum'][$row['uid']] : 0,
				'hidecommentnum' => isset($dataHash['hidecommentnum'][$row['uid']]) ? $dataHash['hidecommentnum'][$row['uid']] : 0,
				'twnum' => isset($dataHash['twnum'][$row['uid']]) ? $dataHash['twnum'][$row['uid']] : 0
			);
		}

		$cacheData = serialize($sta_cache);

		$this->cacheWrite($cacheData, 'sta');
	}

作者: jaeheng 本文发布于2020-2-10 02:14:38
jaeheng

前端工程师,emlog模版beginning开发者

免责声明:本文仅代表作者个人观点,与本网站无关。其原创性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容、文字的真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。

发表评论