Rumah >pembangunan bahagian belakang >tutorial php >MYSQL查询速度特别慢,是不是我的表结构有问题,请大家看一下。
表一,是小说表:
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 …分析下查询 慢的地方,合理加上索引
根据查询的需求,表加上索引