1.在测试一个按照时间的范围查询时,尽管增加了索引,发现使用不到索引,可以使用这个来强制使用索引
测试过程为,创建下面的表,以及创建了联合索引
create table delay_delete_users( id int auto_increment,email_id int not null default 0 comment 'email表id',email varchar(50) default '' comment 邮箱前缀企业id默认域timestamp comment 删除时间tinyint 0未处理,1已清空primary key (id),1)"> email_entid(email,entid),1)"> delete_time(delete_time,clear) )engine innodb;
插入测试数据,进行explain查询
insert into `delay_delete_users` (email,entid,default_domain,delete_time)value(shihan2',23684,appdev.sinanet.com2019-12-10 15:49:16); 12019-12-12 15:49:161);
explain select * from delay_delete_users where delete_time<'2019-12-12' and clear=0; 索引没有使用到,还是进行的全表扫描,看那个扫描行数rows
+----+-------------+--------------------+------------+------+---------------+------+---------+------+------+----------+-------------+ | id | select_type | table | partitions | type | possible_keys key | key_len | ref | rows | filtered | Extra | | 1 | SIMPLE | delay_delete_users NULL | ALL | delete_time NULL NULL | 7 14.29 | Using where |
explain select * from delay_delete_users force index(delete_time) where delete_time<'2019-12-12' and clear=0;使用到了索引
--+-------------+--------------------+------------+-------+---------------+-------------+---------+------+------+----------+-----------------------+ | type key | Extra | range | delete_time 4 3 index condition |
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。