mysql where过滤条件中and连接的两个条件的顺序不必和建立的联合索引的字段顺序一致...

aa表

mysql> select * from aa;
+------+--------+--------+
| id   | name   | other  |
+------+--------+--------+
|    1 | asdf   | dfdfd  |
|    2 | sdf    | fdfd   |
|    3 | dfasdf | dfdasd |
|    4 | fasdf  | fdasd  |
|    5 | asdf   | dasd   |
|    6 | bsdf   | hell0  |
+------+--------+--------+

联合索引

mysql> show index from aa;
+-------+------------+-------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
| Table | Non_unique | Key_name    | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment |
+-------+------------+-------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
| aa    |          1 | aa_id_name  |            1 | id          | A         |           2 |     NULL | NULL   | YES  | BTREE      |         |
| aa    |          1 | aa_id_name  |            2 | name        | A         |           2 |     NULL | NULL   | YES  | BTREE      |         |
+-------+------------+-------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+

执行计划

mysql> explain select * from aa where id = 5 and name = 'asdf';
+----+-------------+-------+------+------------------------+------------+---------+-------------+------+-------------+
| id | select_type | table | type | possible_keys          | key        | key_len | ref         | rows | Extra       |
+----+-------------+-------+------+------------------------+------------+---------+-------------+------+-------------+
|  1 | SIMPLE      | aa    | ref  | aa_id_name,idx_id_name | aa_id_name | 24      | const,const |    1 | Using where |
+----+-------------+-------+------+------------------------+------------+---------+-------------+------+-------------+
1 row in set (0.02 sec)

mysql> explain select * from aa where name = 'asdf' and id = 5;
+----+-------------+-------+------+------------------------+------------+---------+-------------+------+-------------+
| id | select_type | table | type | possible_keys          | key        | key_len | ref         | rows | Extra       |
+----+-------------+-------+------+------------------------+------------+---------+-------------+------+-------------+
|  1 | SIMPLE      | aa    | ref  | aa_id_name,idx_id_name | aa_id_name | 24      | const,const |    1 | Using where |
+----+-------------+-------+------+------------------------+------------+---------+-------------+------+-------------+
1 row in set (0.00 sec)

总结:不管where过滤条件中id在前还是name在前都是用了联合索引

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值