1、单表优化
首先创建一个表
create table book(
bid int(4) primary key,
name varchar(20) not null,
authorid int(4) not null,
publicid int(4) not null,
typeid int(4) not null
);
然后插入4条数据(随便插入)
问题:查询authorid=1 且 typeid=2或3 的bid
select bid from book where authorid=1 and (typeid=2 or typeid=3) order by typeid desc;
在未添加索引的情况下
现在添加索引,mysql语句的
编写过程 having:过滤
select distinct from join on where group by having order by limit
解析过程
from on join where group by having select distinct order by limit
根据解析过程可知,select后面的字段最后解析,所以建立索引的时候bid应该放在最后
alter table book