mysql> select * from tb_user where phone >='17799990020';+----+--------+-------------+---------------------+------------+------+--------+--------+---------------------+| id | name | phone | email | profession | age | gender | status | createtime |+----+--------+-------------+---------------------+------------+------+--------+--------+---------------------+|21| 典韦 |17799990020| ycaunanjian@163.com | 城市规划 |52|1|2|2000-04-1200:00:00||22| 廉颇 |17799990021| lianpo321@126.com | 土木工程 |19|1|3|2002-07-1800:00:00||23| 后羿 |17799990022| altycj2000@139.com | 城市园林 |20|1|0|2002-03-1000:00:00||24| 姜子牙 |17799990023|37483844@qq.com | 工程造价 |29|1|4|2003-05-2600:00:00|+----+--------+-------------+---------------------+------------+------+--------+--------+---------------------+4 rows in set(0.00 sec)
mysql>
2.3、执行计划 phone >=‘17799990020’
mysql> explain select * from tb_user where phone >='17799990020';+----+-------------+---------+------------+-------+----------------+----------------+---------+------+------+----------+-----------------------+| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |+----+-------------+---------+------------+-------+----------------+----------------+---------+------+------+----------+-----------------------+|1| SIMPLE | tb_user |NULL| range | idx_user_phone | idx_user_phone |46|NULL|4|100.00| Using index condition |+----+-------------+---------+------------+-------+----------------+----------------+---------+------+------+----------+-----------------------+1 row in set,1warning(0.00 sec)
mysql>
此时走索引
2.4、执行计划 phone >=‘17799990000’
mysql> explain select * from tb_user where phone >='17799990000';+----+-------------+---------+------------+------+----------------+------+---------+------+------+----------+-------------+| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |+----+-------------+---------+------------+------+----------------+------+---------+------+------+----------+-------------+|1| SIMPLE | tb_user |NULL| ALL | idx_user_phone |NULL|NULL|NULL|24|100.00| Using where |+----+-------------+---------+------------+------+----------------+------+---------+------+------+----------+-------------+1 row in set,1warning(0.00 sec)
此时没有走索引,走的全表扫描
2.5、执行计划 phone >=‘17799990010’
mysql> explain select * from tb_user where phone >='17799990010';+----+-------------+---------+------------+------+----------------+------+---------+------+------+----------+-------------+| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |+----+-------------+---------+------------+------+----------------+------+---------+------+------+----------+-------------+|1| SIMPLE | tb_user |NULL| ALL | idx_user_phone |NULL|NULL|NULL|24|58.33| Using where |+----+-------------+---------+------------+------+----------------+------+---------+------+------+----------+-------------+1 row in set,1warning(0.00 sec)
mysql>
此时没有走索引,走的是全表扫描,因为表中的数据大部分满足phone >='17799990010'
2.6、执行计划 phone >=‘17799990013’
mysql> explain select * from tb_user where phone >='17799990013';+----+-------------+---------+------------+-------+----------------+----------------+---------+------+------+----------+-----------------------+| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |+----+-------------+---------+------------+-------+----------------+----------------+---------+------+------+----------+-----------------------+|1| SIMPLE | tb_user |NULL| range | idx_user_phone | idx_user_phone |46|NULL|11|100.00| Using index condition |+----+-------------+---------+------------+-------+----------------+----------------+---------+------+------+----------+-----------------------+1 row in set,1warning(0.00 sec)
mysql>
此时命中索引,因为表中的数据很少一部分满足phone >=‘17799990013’所以走索引更快
2.7、执行计划 profession is null
mysql> explain select * from tb_user where profession is null;+----+-------------+---------+------------+------+----------------------+----------------------+---------+-------+------+----------+-----------------------+| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |+----+-------------+---------+------------+------+----------------------+----------------------+---------+-------+------+----------+-----------------------+|1| SIMPLE | tb_user |NULL| ref | idx_user_pro_age_sta | idx_user_pro_age_sta |47|const|1|100.00| Using index condition |+----+-------------+---------+------------+------+----------------------+----------------------+---------+-------+------+----------+-----------------------+1 row in set,1warning(0.00 sec)
mysql>
此时命中索引,因为表中的数据都不是null,所以走索引更快
2.8、执行计划 profession is not null
mysql> explain select * from tb_user where profession is not null;+----+-------------+---------+------------+------+----------------------+------+---------+------+------+----------+-------------+| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |+----+-------------+---------+------------+------+----------------------+------+---------+------+------+----------+-------------+|1| SIMPLE | tb_user |NULL| ALL | idx_user_pro_age_sta |NULL|NULL|NULL|24|100.00| Using where |+----+-------------+---------+------------+------+----------------------+------+---------+------+------+----------+-------------+1 row in set,1warning(0.00 sec)
mysql>
mysql> explain select * from tb_user where profession is null;+----+-------------+---------+------------+------+----------------------+------+---------+------+------+----------+-------------+| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |+----+-------------+---------+------------+------+----------------------+------+---------+------+------+----------+-------------+|1| SIMPLE | tb_user |NULL| ALL | idx_user_pro_age_sta |NULL|NULL|NULL|24|100.00| Using where |+----+-------------+---------+------------+------+----------------------+------+---------+------+------+----------+-------------+1 row in set,1warning(0.00 sec)
mysql>
2.11、执行计划 profession is not null
mysql> explain select * from tb_user where profession is not null;+----+-------------+---------+------------+-------+----------------------+----------------------+---------+------+------+----------+-----------------------+| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |+----+-------------+---------+------------+-------+----------------------+----------------------+---------+------+------+----------+-----------------------+|1| SIMPLE | tb_user |NULL| range | idx_user_pro_age_sta | idx_user_pro_age_sta |47|NULL|1|100.00| Using index condition |+----+-------------+---------+------------+-------+----------------------+----------------------+---------+------+------+----------+-----------------------+1 row in set,1warning(0.00 sec)
mysql>