MySQL索引管理优化详述

2018-04-21 23:48:45 14584

1、整合DDL语句
      在将索引添加到MySQL表的过程中,一个很重要的问题就是DDL语句时阻塞性,把多条alter语句整合成一条SQL语句时一种简单的优化改进。
例如:
alter table test add index(username);
 alter table test drop index name,add index name(last_name,first_name);
 alter table test add column laset_visit date null;
改成:
alter table test 
 add index(username),
 drop index name,
 add index name(last_name,first_name),
 add column laset_visit date null;
      该优化能够大幅度提升管理任务的性能。
2、去除重复索引
      重复的索引有两个主要的影响:第一,所有DML语句都会运行的更慢,因为需要更多工作来保持数据和索引的一致性;第二,数据库的磁盘占用量会更大,这将导致备份和恢复的时间增加。
例如:
create table test
 (id int unsinged not null,
 first_name varchar(30) not null,
 last_name varchar(30) not null,
 joined date not null,
 primary key(id),
 index (id)
 );
      这个DDL中id列上的索引是重复的索引,需要将其移除。
      当一个给定索引的最左边部分被包含在其他索引中时也会产生重复索引。
create table test
 (id int unsinged not null,
 first_name varchar(30) not null,
 last_name varchar(30) not null,
 joined date not null,
 primary key(id),
 index name1 (last_name),
 index name2 (last_name,first_name)
 );
 name1这个索引是多余的,因为此索引所在的列已经被包含在索引name2的最左边部分里面了。
3、删除不用的索引
      除了重复索引没有被使用到之外,还有其他索引可能也没有被用到,这些索引和重复索引一样会影响性能。
4、监控无效的索引
      当定义多列索引时,一定要注意确定所指定的每一列是否真的有效,可以通过分析指定表上的所有SQL语句的key_len列来找到那些可能包含没有使用到的列的索引。

提交成功!非常感谢您的反馈,我们会继续努力做到更好!

这条文档是否有帮助解决问题?

非常抱歉未能帮助到您。为了给您提供更好的服务,我们很需要您进一步的反馈信息:

在文档使用中是否遇到以下问题: