1.索引优化之选择合适的列进行索引:
1).在where从句、order by从句、group by从句、on从句中出现的列
2).索引字段越小越好
3).离散度大的列放在联合索引的前面,如:
SELECT * from payment where customer_id=584 and staff_id=2;
是index(customer_id,staff_id)好?还是index(staff_id,customer_id)好?如果 customer_id的离散度(数据内容更多)更高就应该使用index(customer_id,staff_id);
2.重复索引和冗余索引:
重复索引和冗余索引都会影响数据库增删改查的效率
//重复索引:id既设置成了primary key又设置成了unique
create table test( id int not null primary key, name varchar(5) not null default '', title varchar(8) not null default '', unique(id) )engine=innodb;
//冗余索引:id设置成了primary key,然后又在联合索引中被包含
create table test( id int not null primary key, name varchar(5) not null default '', title varchar(8) not null default '', key(id,name) )engine=innodb;
查询索引信息可以使用:
show create table test;
或者使用查询工具:pt-duplicate-key-checker
原文链接:https://blog.csdn.net/living_ren/article/details/79417619
本站声明:网站内容来源于网络,如有侵权,请联系我们,我们将及时处理。
还没有人抢沙发呢~