一、Btree 两类结构
Clustered:聚簇(extent区)索引
extent区:连续的64个数据页(page),默认1M
Secondary:辅助索引
普通单列索引
普通联合索引
1. 最左原则
# 1. abc, 重复值最少的列 作为最左列
# 2. 查询条件中必须包含最左列条件(前后顺序不影响)
alter table t1 add index idx(name,gender);
create index 索引名称 on 表名(字段名称1, 字段名称2, ...);
# 示例
create index firstIndex on student(id, name, address);
遇到 where xxx order by xxx 或 where xxx groupby xxx时,建议创建联合索引。
唯一联合索引
create unique index 索引名称 on 表名(字段名称1, 字段名称2, ...);
# 示例
create unique index secondIndex on student(id, name, address);
二、优化
1. 强制使用索引
SELECT * FROM table1 USE INDEX (col1_index,col2_index)
WHERE col1=1 AND col2=2 AND col3=3;
2. 修改优化器算法(优选hints)
三、index_condition_pushdown (ICP) 索引下推