性别字段能不能建立索引
之前面试被问到一个问题
什么字段适合建索引,什么字段不适合建索引。
性别字段可以建索引吗?
我回答得不是很好。
性别字段这种重复性很强的字段,不要建立索引。为什么不能呢?
下面建立表测试下。如:我建了个tb_test表,往里面插入了七百多万数据,先给性别建立索引,查询开启 sql运行时间记录。
创建名为tb_index_test 的索引
alter table tb_test add index tb_index_test(genenre)
# 开启时间记录
set profiling = 1;
查询genenre=1 的数据;
select * from tb_test where genenre=1;
删除索引
# 查看tb_test表的索引
show index from tb_test;
# 删除索引
drop index tb_test_index on tb_test;
在次查询genenre=1 的数据
select * from tb_test where genenre=1;
查看以上几个命令的执行时间
show profiles;
结果如下
添加索引后查询 比原来为添加索引还慢
添加索引 的查询耗时为 64.5秒
没有索引的查询耗时为 7.8秒
结论是 重复性较强的字段,不适合添加索引
mark222