过多的索引不但会影响写入效率也会影响查询效率。
重复索引
重复索引是指相同的列以相同的顺序建立的同类型的索引,如下表中primary key和ID列上的索引就是重复索引。
create tables test(
id int not null primary key,
name varchar(50) not null,
title varchar(50) not nll.
unique(id)
)engine = innodb; //主键已经是唯一索引了
冗余索引是指多个索引的前缀列相同或是在联合索引中包含了主键的索引,下面例子中key(name,id)就是一个冗余索引
create table test(
id int not null primary key/
name varchar(10) not null.
title varchar(50) not null,
key(name,id)
)engine=innodb;
对于innodb来说每一个索引后面实际上都会包含主键。在建立一个联合索引人为的把索引包含进去,这时就是一个冗余索引。
工具 pt-duplicate-key-checker
用法 pt-duplicate-key-checker -uroot -p****** -h127.0.0.1
可以查看重复索引,冗余索引,并且给出了一个优化的建议,应用更加方便有效。
删除不再使用的索引
目前mysql中还没有记录索引的使用情况,但是在perconmysql和mariadb中可以通过INDEX_STATISTICE表来查看哪些索引未使用,但在mysql中目前只能通过慢查日志配合pt_index_usage工具来进行索引使用情况的分析。
pt-index-usage \ -uroot -p****** \ mysql-slow.log
小蚂蚁学习mysql性能优化(6)--SQL以及索引优化--索引的优化要点
原文:http://my.oschina.net/woshixiaomayi/blog/513349