MYSQL查詢速度特別慢,是不是我的表結構有問題,請大家看一下。

來源:互聯網
上載者:User
表一,是小說表:
CREATE TABLE IF NOT EXISTS `book` (
`bid` int(6) unsigned NOT NULL AUTO_INCREMENT,
`siteid` smallint(2) NOT NULL DEFAULT '0', 多網站ID
`catid` smallint(6) NOT NULL, 欄目ID
`title` char(80) NOT NULL, 標題
`words` char(20) NOT NULL DEFAULT '0', 字數統計
`content` text NOT NULL, 簡介
`inputtime` int(11) NOT NULL, 錄入時間
`updatetime` int(11) NOT NULL, 更新時間
`username` varchar(40) NOT NULL, 使用者名稱
`author` varchar(100) NOT NULL, 作者
`letter` char(5) NOT NULL, 首字母
`thumb` varchar(200) NOT NULL, 縮圖
`url` varchar(200) NOT NULL, 靜態地址
`keywords` varchar(200) NOT NULL,關鍵字
`passed` tinyint(1) NOT NULL DEFAULT '0',連載情況
`status` tinyint(2) NOT NULL DEFAULT '99',審核
`posid` tinyint(1) NOT NULL DEFAULT '0',推薦ID
PRIMARY KEY (`bid`),
KEY `siteid` (`siteid`,`passed`,`catid`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

表二,章節表
CREATE TABLE IF NOT EXISTS `book_capital` (
`cid` int(8) unsigned NOT NULL AUTO_INCREMENT,
`bid` int(6) unsigned NOT NULL, 書ID
`listorder` int(8) NOT NULL DEFAULT '0',排序
`title` varchar(180) NOT NULL,章區段標頭
`content` text NOT NULL,章節內容
`updatetime` int(11) NOT NULL,更新時間
`url` varchar(200) NOT NULL,章節地址
PRIMARY KEY (`cid`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=38065 ;


現在不知道是什麼問題,查詢速度特別慢,請高手看看是不是表建的不對。



回複討論(解決方案)

sql 指令的問題

public function lists($data) {
$siteid = $data['siteid'] ? intval($data['siteid']) : get_siteid();
$catid = intval($data['catid']);
$passed = $data['passed']==''?"3":$data['passed'];
$posid = intval($data['posid']);
$catidin = $data['catidin'];
//echo $data['letter'];
//echo ord($data['letter']);
if(ord($data['letter'])>60){
$letter=$data['letter'];
}
//$letter = $data['letter']==0?"":$data['letter'];
if(isset($data['where'])) {$sql = " and ".$data['where'];}
$thumb = intval($data['thumb']) ? " AND thumb != ''" : '';
if ($siteid) $where .= " `siteid`='".$siteid."'";
if ($passed==0 or $passed==1) $where .= " and `passed`='".$passed."'";
if ($posid) $where .= " and `posid`='".$posid."'";
if ($catidin) $where .= " and catid in('".$catidin."')";
if ($catid) $where .= " and `catid`='".$catid."'";
if ($letter) $where .= " and `letter`='".$letter."'";
$where .= ' AND `status`=\'99\''.$sql.$thumb;
$order = $data['order'];
return $this->db->select($where, '*', $data['limit'], $order);
}


這是查詢的標籤代碼,這裡有問題嗎。

是否有問題,需要看到完整的查詢指令才知道

第一個是熱點內容查詢,是和點擊表關聯的查詢指令:
select a.bid,a.title,a.author,a.content,a.updatetime,a.catid,a.url, a.posid, b.bid,b.hits ,b.hits from book a,book_hits b where a.bid=b.bid and a.posid=2 order by hits desc LIMIT 1

點擊表:
CREATE TABLE IF NOT EXISTS `book_hits` (
`catid` int(6) NOT NULL,
`bid` int(6) unsigned NOT NULL,
`hits` int(6) NOT NULL DEFAULT '0',
`day` int(6) DEFAULT '0',
`week` int(6) DEFAULT '0',
`month` int(6) DEFAULT '0',
`updatetime` int(11) NOT NULL,
PRIMARY KEY (`bid`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;


第二個是單表查詢的:select * from book where `siteid`='1' and `posid`='1' and `catid`='22' AND `status`='99' AND thumb != '' order by bid desc limit 0,10

整個頁面就這兩種查詢。開啟首頁差不多要20秒才行。

是不是?量大?

在查詢器裡在你的查詢語句前面加explain
例如
EXPLAIN select * from user where user='999'
具體相關解釋看這個
http://www.blogjava.net/persister/archive/2008/10/27/236813.html

desc select * from …分析下查詢 慢的地方,合理加上索引

根據查詢的需求,表加上索引

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.