关于mysql where 时间字段 大于 小于是否使用索引。 复合索引

对时间字段 trans_date 添加普通索引。

使用where trans_date = ‘’  是肯定会使用索引的。

但是使用<  , > 比较符时就不一定了。

select count(1) from A; // 40000

EXPLAIN select * from A where trans_date = '20190428'; 

2. 使用> ,  查询语句没有使用索引

select count(1) from t_trans_log_info where trans_date > '20190428'; //11200

EXPLAIN select * from t_trans_log_info where trans_date > '20190410';

3. 

select count(1) from t_trans_log_info where trans_date > '20190528'; //1120

EXPLAIN select * from t_trans_log_info where trans_date > '20190528';

这时又使用了索引,说明时间字段 使用大于 时是否使用索引 是和结果的数量有关的,当数量较少(网上查到是有一个比例)时时使用索引的。 

 

 

二。 

复合索引: 索引并不强制出现在条件第一位,因为mysql会优化查询。但是复合索引的首列必须出现在查询条件中才可能使用该索引。

可以理解索引就是个字符串,从中间去找,效率极低。需要注意部分操作可能是索引失效,如不等于,函数计算,类型转换,空,非空判断等。

同时重复性较高的列不适合建立索引。

posted on 2019-07-15 10:42  远方的人  阅读(29356)  评论(0编辑  收藏  举报

导航