-- 注意:id列是主键索引 user_id是普通索引-- ALL走全表扫描:explainselect*from user_operation_log a where a.ip ='10.131.11';explainselect id from user_operation_log a where a.ip ='10.131.11';-- 即便查询字段是索引字段,但是条件是非索引字段,还是走全表扫描-- const:根据主键/普通唯一索引列等值匹配查询(is null除外),这种查询是很快的,查询速率认为是常数级别的,定义为constexplainselect*from user_operation_log a where id =100;-- ref:根据普通索引等值匹配,或is null。(前面说的普通唯一索引列查询时 is null也是这种场景)。这种方式需要先根据普通索引匹配到多个主键,然后根据主键进行回表。explainselect*from user_operation_log a where id isnull;explainselect*from user_operation_log a where user_id ='100';-- 有查询非索引字段,就需要回表了,无法索引覆盖,注意字符串不加单引号,就会索引失效的;一个索引一棵树(除了联合索引),所以需要根据普通索引等值配置拿到主键,再回表根据主键拿到字段值-- range:根据主键索引或普通索引(包含唯一索引)进行范围查找explainselect*from user_operation_log a where id >100and id <200;-- index:索引覆盖,你查询的列刚好是索引列,即使查询条件是联合索引的非最左索引列,查询的条件是联合索引中的列,也可能会走索引覆盖;#如果我们select的字段/数据,可以通过索引树直接找到数据,不需要再次去聚集索引的叶子节点找数据,这时我们就叫他覆盖索引explainselect id,user_id from user_operation_log a where user_id ='4000100';#走的是ref形式,但是是using index的,也是索引覆盖-- index merge:除此之外,还会有index merge(索引合并),针对一些and、or的操作,单纯的回表可能速度会慢一些,如果先将使用到的索引先进行求 交集、并集之后在进行回表,会更加高效explainselect*from user_operation_log a where id =200or user_id ='100';