[MySQL] use of force index mandatory use of the index

When tested in accordance with the scope of a query time, despite the increase in the index, we found that use less than the index, you can use this to force the use of the index

The testing process is to create the following table, and the creation of a joint index

create table delay_delete_users(
id int auto_increment, 
email_id int not null default 0 comment 'email表id',
email varchar(50) not null default '' comment '邮箱前缀',
entid int not null default 0 comment '企业id',
default_domain  varchar(50) not null  default  '' Comment ' Default Domain ' , 
delete_time timestamp Comment ' deletion time ' , 
Clear tinyint  Not  null  default  0 Comment ' 0 untreated been emptied 1 ' ,
 Primary  Key (ID),
 Key email_entid (In Email, entid),
 Key delete_time (delete_time, Clear) 
) Engine InnoDB;

 

Insert test data, explain the query

insert into `delay_delete_users` (email,entid,default_domain,delete_time)value('shihan2',23684,'appdev.sinanet.com','2019-12-10 15:49:16');
insert into `delay_delete_users` (email,entid,default_domain,delete_time,clear)value('shihan2',23684,'appdev.sinanet.com','2019-12-10 15:49:16',1);
insert into `delay_delete_users` (email,entid,default_domain,delete_time,clear)value('shihan2',23684,'appdev.sinanet.com','2019-12-12 15:49:16',1);

explain select * from delay_delete_users where delete_time < '2019-12-12' and clear = 0; not used to index, or for the full table scan, see that the number of rows of scanning lines

+----+-------------+--------------------+------------+------+---------------+------+---------+------+------+----------+-------------+
| 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    | 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;

+----+-------------+--------------------+------------+-------+---------------+-------------+---------+------+------+----------+-----------------------+
| id | select_type | table              | partitions | type  | possible_keys | key         | key_len | ref  | rows | filtered | Extra                 |
+----+-------------+--------------------+------------+-------+---------------+-------------+---------+------+------+----------+-----------------------+
|  1 | SIMPLE      | delay_delete_users | NULL       | range | delete_time   | delete_time | 4       | NULL |    3 |    14.29 | Using index condition |

 

Guess you like

Origin www.cnblogs.com/taoshihan/p/12031269.html