mysql type字段写不进去_关于 Mysql 字段值分布很少的字段要不要加索引的问题.

我看到很多 mysql 索引的文章,都提到了,说如果某个字段的值分布范围很少(大量重复值),是不需要建立索引的.

但是我实际测试情况:

user_type_id字段有索引:

create table user_score_have_index

(

id int auto_increment

primary key,

score int default '0' not null,

user_type_id tinyint(2) default '0' not null

);

create index user_score_have_index_score_index

on user_score_have_index (score);

create index user_score_have_index_user_type_id_index

on user_score_have_index (user_type_id);

user_type_id字段无索引:

create table user_score_no_index

(

id int auto_increment

primary key,

score int default '0' not null,

user_type_id tinyint(2) default '0' not null

);

两张表数据一模一样, user_type_id 只有 1,2,3,4,5 五种值.

执行 explain 效果:

有索引:

1db7c624b179d34fef35fe82299cde32.png

无索引:

7966a016d9531c56b35c644abb139806.png

然后执行时间:

有索引用时 1s

无索引用时 6s

可以看到查询速度显著提高.

那么为什么还有这种说法呢?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值