首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往
您找到你想要的搜索结果了吗?
是的
没有找到

MySQL索引管理优化详述

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列来找到那些可能包含没有使用到的列的索引。

02
领券