zoukankan      html  css  js  c++  java
  • 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在前都是用了联合索引

  • 相关阅读:
    ArcGIS Server 服务迁移、恢复
    发布(高程数据)服务,Service Editor界面无LERC格式选项
    汇总10.4版本ArcGIS Server与ArcMap 安装+SDE+注册数据源(非破解)
    ArcGIS Server 缓存服务增加新比例尺缓存
    ArcMap 标注、注记、图形文本
    ArcGIS Server 缓存服务切图范围
    Oracle 安装 INS-30131错误。
    JAVA经典算法40题(供面试所用)
    Java 之HashMap.values()方法误用
    Java中如何把两个数组合并为一个
  • 原文地址:https://www.cnblogs.com/iLoveMyD/p/2545006.html
Copyright © 2011-2022 走看看