MySQL索引

一、索引建立

1.普通索引

create index on table_name(column_name);

2.唯一索引

create unique index on table_name(column_name);

二、索引删除

drop index index_name on table_name;

三、查询表索引

show index from table_name;

四、查询索引碎片

show table status like `table_name`;

data_free字段大于0时,表示有碎片产生。

五、碎片清理

optimize table table_name;

六、什么情况下不建或少建索引

1.表记录太少。

2.经常插入、删除、修改的表。

3.数据重复且分布量差不多的列。

4.经常跟主字段一块查询但主字段索引值较多的表。

注意:

1.建立索引会占用磁盘大量空间,要考虑磁盘是否充足。

2.建立索引的时候会对表加锁,因此应在空闲时进行。

你可能感兴趣的:(MySQL索引)