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 |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_5 |1| email | A |23|5|NULL| YES | BTREE ||| YES |NULL|+---------+------------+----------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+5 rows in set(0.00 sec)
mysql>
4、创建 name 和 phone 索引
mysql> create index idx_user_name on tb_user(name);
Query OK,0 rows affected(0.03 sec)
Records:0 Duplicates:0 Warnings:0
mysql> create unique index idx_user_phone on tb_user(phone);
Query OK,0 rows affected(0.03 sec)
Records:0 Duplicates:0 Warnings:0
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_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_5 |1| email | A |23|5|NULL| YES | BTREE ||| YES |NULL|| tb_user |1| idx_user_name |1| name | A |24|NULL|NULL|| BTREE ||| YES |NULL|+---------+------------+----------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+7 rows in set(0.00 sec)
mysql>
5、查询 phone=‘17799990010’ and name=‘韩信’
mysql> select id,phone,name from tb_user where phone='17799990010'and name='韩信';+----+-------------+------+| id | phone | name |+----+-------------+------+|11|17799990010| 韩信 |+----+-------------+------+1 row in set(0.00 sec)
mysql>
6、执行计划 phone=‘17799990010’ and name=‘韩信’
mysql> explain select id,phone,name from tb_user where phone='17799990010'and name='韩信';+----+-------------+---------+------------+-------+------------------------------+----------------+---------+-------+------+----------+-------+| 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_name | idx_user_phone |46|const|1|100.00|NULL|+----+-------------+---------+------------+-------+------------------------------+----------------+---------+-------+------+----------+-------+1 row in set,1warning(0.00 sec)
mysql>
多条件联合查询时,MySQL优化器会评估哪个字段的索引效率更高,会选择该索引完成本次查询。
7、创建联合唯一索引 idx_user_phone_name
mysql> create unique index idx_user_phone_name on tb_user(phone,name);
Query OK,0 rows affected(0.02 sec)
Records:0 Duplicates:0 Warnings:0
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 |0| idx_user_phone_name |1| phone | A |24|NULL|NULL|| BTREE ||| YES |NULL|| tb_user |0| idx_user_phone_name |2| 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_5 |1| email | A |23|5|NULL| YES | BTREE ||| YES |NULL|| tb_user |1| idx_user_name |1| name | A |24|NULL|NULL|| BTREE ||| YES |NULL|+---------+------------+----------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+9 rows in set(0.02 sec)
mysql>
8、再次执行计划 phone=‘17799990010’ and name=‘韩信’
mysql> explain select id,phone,name from tb_user where phone='17799990010'and name='韩信';+----+-------------+---------+------------+-------+--------------------------------------------------+----------------+---------+-------+------+----------+-------+| 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_name,idx_user_name | idx_user_phone |46|const|1|100.00|NULL|+----+-------------+---------+------------+-------+--------------------------------------------------+----------------+---------+-------+------+----------+-------+1 row in set,1warning(0.00 sec)
mysql>