mysql> show index from tb_user;+---------+------------+----------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment | Visible | Expression |+---------+------------+----------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+| tb_user |0| PRIMARY |1| id | A |24|NULL|NULL|| BTREE ||| YES |NULL|| tb_user |0| idx_user_phone |1| phone | A |24|NULL|NULL|| BTREE ||| YES |NULL|| tb_user |1| idx_user_name |1| name | A |24|NULL|NULL|| BTREE ||| YES |NULL|| tb_user |1| idx_user_pro_age_sta |1| profession | A |16|NULL|NULL| YES | BTREE ||| YES |NULL|| tb_user |1| idx_user_pro_age_sta |2| age | A |22|NULL|NULL| YES | BTREE ||| YES |NULL|| tb_user |1| idx_user_pro_age_sta |3| status | A |24|NULL|NULL| YES | BTREE ||| YES |NULL|| tb_user |1| idx_email |1| email | A |24|NULL|NULL| YES | BTREE ||| YES |NULL|+---------+------------+----------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+7 rows in set(0.00 sec)
mysql>
1.3、查询 phone=‘17799990015’
mysql> select * from tb_user where phone='17799990015';+----+------+-------------+-------------------+------------+------+--------+--------+---------------------+| id | name | phone | email | profession | age | gender | status | createtime |+----+------+-------------+-------------------+------------+------+--------+--------+---------------------+|16| 妲己 |17799990015|2783238293@qq.com | 软件工程 |31|2|0|2001-01-3000:00:00|+----+------+-------------+-------------------+------------+------+--------+--------+---------------------+1 row in set(0.00 sec)
mysql>
1.4、执行计划 phone=‘17799990015’
mysql> explain select * from tb_user where phone='17799990015';+----+-------------+---------+------------+-------+----------------+----------------+---------+-------+------+----------+-------+| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |+----+-------------+---------+------------+-------+----------------+----------------+---------+-------+------+----------+-------+|1| SIMPLE | tb_user |NULL|const| idx_user_phone | idx_user_phone |46|const|1|100.00|NULL|+----+-------------+---------+------------+-------+----------------+----------------+---------+-------+------+----------+-------+1 row in set,1warning(0.00 sec)
mysql>
1.5、查询 substring(phone,10,2) =‘15’
mysql> select * from tb_user where substring(phone,10,2)='15';+----+------+-------------+-------------------+------------+------+--------+--------+---------------------+| id | name | phone | email | profession | age | gender | status | createtime |+----+------+-------------+-------------------+------------+------+--------+--------+---------------------+|16| 妲己 |17799990015|2783238293@qq.com | 软件工程 |31|2|0|2001-01-3000:00:00|+----+------+-------------+-------------------+------------+------+--------+--------+---------------------+1 row in set(0.00 sec)
mysql>
1.6、执行计划 substring(phone,10,2) =‘15’
mysql> explain select * from tb_user where substring(phone,10,2)='15';+----+-------------+---------+------------+------+---------------+------+---------+------+------+----------+-------------+| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |+----+-------------+---------+------------+------+---------------+------+---------+------+------+----------+-------------+|1| SIMPLE | tb_user |NULL| ALL |NULL|NULL|NULL|NULL|24|100.00| Using where |+----+-------------+---------+------------+------+---------------+------+---------+------+------+----------+-------------+1 row in set,1warning(0.00 sec)
mysql>
phone索引失效
2、字符串不加引号
字符串类型字段使用时,不加引号,索引将失效。
2.1、查询 phone=17799990015
mysql> select * from tb_user where phone=17799990015;+----+------+-------------+-------------------+------------+------+--------+--------+---------------------+| id | name | phone | email | profession | age | gender | status | createtime |+----+------+-------------+-------------------+------------+------+--------+--------+---------------------+|16| 妲己 |17799990015|2783238293@qq.com | 软件工程 |31|2|0|2001-01-3000:00:00|+----+------+-------------+-------------------+------------+------+--------+--------+---------------------+1 row in set(0.00 sec)
mysql>
2.2、执行计划 phone=17799990015
mysql> explain select * from tb_user where phone=17799990015;+----+-------------+---------+------------+------+----------------+------+---------+------+------+----------+-------------+| 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|10.00| Using where |+----+-------------+---------+------------+------+----------------+------+---------+------+------+----------+-------------+1 row in set,3warnings(0.00 sec)
mysql>
字符串没有加单引号,所以phon索引失效
2.3、执行计划 profession=‘软件工程’ and age=31 and status=0
mysql> explain select * from tb_user where profession='软件工程'and age=31and status=0;+----+-------------+---------+------------+------+----------------------+----------------------+---------+-------------+------+----------+-----------------------+| 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 |49|const,const|1|10.00| Using index condition |+----+-------------+---------+------------+------+----------------------+----------------------+---------+-------------+------+----------+-----------------------+1 row in set,2warnings(0.00 sec)
mysql>
mysql> select * from tb_user where profession like '软件%';+----+--------+-------------+--------------------+------------+------+--------+--------+---------------------+| id | name | phone | email | profession | age | gender | status | createtime |+----+--------+-------------+--------------------+------------+------+--------+--------+---------------------+|5| 花木兰 |17799990004|19980729@sina.com | 软件工程 |23|2|1|2001-04-2200:00:00||1| 吕布 |17799990000| lvbu666@163.com | 软件工程 |23|1|6|2001-02-0200:00:00||16| 妲己 |17799990015|2783238293@qq.com | 软件工程 |31|2|0|2001-01-3000:00:00||15| 貂蝉 |17799990014|84958948374@qq.com | 软件工程 |40|2|3|2001-02-1200:00:00|+----+--------+-------------+--------------------+------------+------+--------+--------+---------------------+4 rows in set(0.00 sec)
mysql>
3.2、执行计划 profession like ‘软件%’
mysql> explain select * from tb_user where profession like '软件%';+----+-------------+---------+------------+-------+----------------------+----------------------+---------+------+------+----------+-----------------------+| 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|4|100.00| Using index condition |+----+-------------+---------+------------+-------+----------------------+----------------------+---------+------+------+----------+-----------------------+1 row in set,1warning(0.00 sec)
mysql>
profession的key_len=47 profession命中索引
3.3、执行计划 profession like ‘%软件’
mysql> explain select * from tb_user where profession like '%软件';+----+-------------+---------+------------+------+---------------+------+---------+------+------+----------+-------------+| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |+----+-------------+---------+------------+------+---------------+------+---------+------+------+----------+-------------+|1| SIMPLE | tb_user |NULL| ALL |NULL|NULL|NULL|NULL|24|11.11| Using where |+----+-------------+---------+------------+------+---------------+------+---------+------+------+----------+-------------+1 row in set,1warning(0.00 sec)
mysql>
头部模糊匹配,profession索引失效。
3.4、执行计划 profession like ‘%软件%’
mysql> explain select * from tb_user where profession like '%软件%';+----+-------------+---------+------------+------+---------------+------+---------+------+------+----------+-------------+| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |+----+-------------+---------+------------+------+---------------+------+---------+------+------+----------+-------------+|1| SIMPLE | tb_user |NULL| ALL |NULL|NULL|NULL|NULL|24|11.11| Using where |+----+-------------+---------+------------+------+---------------+------+---------+------+------+----------+-------------+1 row in set,1warning(0.00 sec)
mysql>