SQL单表优化
YuWei Wu 9/26/2022 MySql
先看走不走索引 再看回不回数据表
# 创建数据库表
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,
)
insert into book values(1,'java',1,1,2);
insert into book values(2,'tc',2,1,2);
insert into book values(3,'wx',3,2,1);
insert into book values(4,'math',4,2,3);
# 需求
查询authorid = 1 且 typeid为2或3的bid
# 逐步优化
select bid from book where typeid in (2,3) and authorid = 1 order by typeid desc;
优化一:
alter table book add index idx_bta(bid,typeid,authorid);
分析:这种优化没有考虑SQL实际解析的顺序,应该调整索引的顺序
优化二:
alter table book add index idx_tab(typeid,authorid,bid);
分析:比如in(1,2)里面有多列会造成索引失效
优化三:
select bid from book where authorid = 1 and typeid in (2,3) order by typeid desc;
alter table book add index idx_atb(authorid,typeid,bid);
# 总结
- 最佳左前缀,保持索引的定义和使用的顺序一致
- 索引需要逐步优化,删除之前的索引,防止索引冲突
- 将包含in的放到放在where条件的最后面