MySQL索引分类

2021-02-07  本文已影响0人  张利锋

存储结构:B-Tree索引、Hash索引、FULLTEXT全文索引、R-Tree索引
应用层次:普通索引、唯一索引、主键索引、复合索引
键值类型:主键索引、辅助索引(二级索引)

1. 普通索引。

create index <索引名> on tablename (字段名)
alert table tablename add index [索引名字](字段名)
create table tablename ([…], index [索引名字](字段名)) 建表时添加索引

drop index <索引名> 删除索引

show index from tablename;

2. 唯一索引.索引字段值必须唯一,但可以是空值

create unique index

3. 主键索引

primary key

4. 复合索引

在多个列上建立索引。开销更小,可以代替单一索引!

create index <索引名> on tablename (字段名1,字段名2……)

窄索引:列小于等于2
宽索引:列大于2
ps:尽量用窄索引

5. 全文索引

create fulltext index <索引名> on tablename (字段名)

select * from user where match(name) against ('aaa')
select * from user where match(name) against('a*' in boolean mode);
上一篇 下一篇

猜你喜欢

热点阅读