1.索引
MySQL索引的建立对于MySQL的高效运行是很重要的,索引可以大大提高MySQL的检索速度。
索引分单列索引和组合索引。单列索引,即一个索引只包含单个列,一个表可以有多个单列索引,但这不是组合索引。组合索引,即一个索引包含多个列。
创建索引时,你需要确保该索引是应用在 SQL 查询语句的条件(一般作为 WHERE 子句的条件)。
实际上,索引也是一张表,该表保存了主键与索引字段,并指向实体表的记录。
缺点:虽然索引大大提高了查询速度,同时却会降低更新表的速度,如对表进行INSERT、UPDATE和DELETE。因为更新表时,MySQL不仅要保存数据,还要保存一下索引文件。
建立索引会占用磁盘空间的索引文件。
2.创建索引
create index indexName on mytable(username(length));
create index a on user (id(2));
创建user表id列的a索引
3.删除索引
drop index [indexName] on mytable;
drop index a on user;
删除user表的a索引
4.唯一索引
create unique index indexName on mytable(username(length));
create unique index a on user (id(2));
创建user表id列的a唯一索引
修改表结构
alter table mytable add unique [indexName] (username(length))
alter table user add unique a (id(2));
5.全文索引
alter table tbl_name add fulltext index_name (column_list):该语句指定了索引为 fulltext ,用于全文索引。
alter table user add fulltext a (id(2));
6.显示索引
show index from table_name;
show index from user;
查看user表的索引