数据库优化之索引优化

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值