索引类型介绍

BTREE:B+树索引

HASH:HASH索引

FULLTEXT:全文索引RTREE:R树索引

索引管理

索引建立在表的列上(字段)的。

在where后面的列建立索引才会加快查询速度。

pages<---索引(属性)<----查数据。

索引分类:

主键索引

普通索引*

唯一索引

添加索引:

alter table test add index index_name(name);
#创建索引
create index index_name on test(name);
#创建索引
desc table;
#查看索引
show index from table;
#查看索引
alter table test drop key index_name;
#删除索引
alter table student add unique key uni_xxx(xxx);
#添加主键索引(略)
#添加唯一性索引
select count(*) from city;
#查看表中数据行数
select count(distinct(name)) from city;
#查看去重数据行数

前缀索引和联合索引

前缀索引

根据字段的前N个字符建立索引

alter table test add index idx_name(name(10));

避免对大列建索引

如果有,就使用前缀索引

联合索引

多个字段建立一个索引

原则:把最常用来做为条件查询的列放在最前面

create table people (id int,name varchar(20),age tinyint,money int ,gender
enum('m','f'));
#创建people表
alter table people add index idx_gam(gender,age,money);
#创建联合索引