美文网首页
explain索引

explain索引

作者: packet | 来源:发表于2019-03-06 16:36 被阅读0次

主键索引:B+树的叶子节点存储,非叶子节点存储主键。
辅助索引:B+树的叶子节点存储索引值和主键,非叶子节点存储索引值。



尽可能使用联合索引,index(a,b,,c)覆盖index(a), index(a,b)

alter table t add index idx_a_b_c(a, b, c)
Select * from t where a = ? and b > ?
Select * from t where a > ? and b = ?
Select * from t where a = ? order by b
Select * from t where a = ? and b > ? and b < ? order by c

使用区分度高的列作为索引
not null 的列
数据类型简答的列,比如int比string好

索引的列不能参与计算
like %abc, not in (...), != 不能使用索引
如果查询中有某个列的索引范围,则其右边所有的列都无法使用索引优化。

select * from t where a = 1 or a = 2 // no
select * from t where id = 1 or id = 2 // yes

select * from t where a = 1 or id = 1 ; // no
select * from t where a = 1 and id = 1  // yes

select * from t where id + 1 = 2   // no
select * from t where id = 2 -1 // yes

limiy m,n 当m很大时,还会过滤很多数据。

最左前缀匹配
索引可以简单如一个列(a),也可以复杂如多个列(a, b, c, d),即联合索引。
如果是联合索引,那么key也由多个列组成,同时,索引只能用于查找key是否存在(相等),遇到范围查询(>、<、between、like左匹配)等就不能进一步匹配了,后续退化为线性查找。因此,列的排列顺序决定了可命中索引的列数。

如有索引(a, b, c, d),查询条件a = 1 and b = 2 and c > 3 and d = 4,则会在每个节点依次命中a、b、c,无法命中d。也就是最左前缀匹配原则。

类型的的是a = 1 and b = 2 and c = 3 order by d 的extra: using index condition,而a = 1 and b = 2 and c > 3 order by d 的extra: using filesort

select * from t where a > 100 order by a; // using index condition
select * from t where a > 100 order by b // using filesort

相关文章

  • Mysql索引优化分析:为啥SQL慢?为啥建的索引常失效

    文章主要介绍mysql性能下降的原因,索引的简介,索引常见的原则,explain命令的使用,以及explain输出...

  • 索引 - Explain

    本文的示例代码参考index_explain 目录 startup explain select_type聚簇索引...

  • explain索引

    主键索引:B+树的叶子节点存储,非叶子节点存储主键。辅助索引:B+树的叶子节点存储索引值和主键,非叶子节点存储索引...

  • explain详解

    explain详解 查询sql查询的执行计划,查看sql查询的索引策略 explain使用 explain字段 i...

  • MySQL索引和explain

    一,MySQL中的索引 二,explain

  • mongodb学习2

    MongoDB 索引 和 explain 的使用 索引基础唯一索引索引的一些参数使用 explainexplain...

  • MySQL Explain命令用法

    引用自 MySQL 性能优化神器 Explain 使用分析 一、介绍 explain显示了mysql如何使用索引...

  • LIMIT优化

    原sql explain type结果为all 方案1:覆盖索引 explain type结果为index 方案2...

  • Mysql 索引优化

    联合索引和单个索引选择 对比,值越大越好 强制使用某个索引 使用explain分析索引 1、id:SQL执行的顺序...

  • MySQL索引、事务、锁、MVCC简述

    目录 MySQL索引、事务、锁、MVCC简述一、索引1.1 执行计划 Explain1.2 索引结构1.2.1 H...

网友评论

      本文标题:explain索引

      本文链接:https://www.haomeiwen.com/subject/ahlxpqtx.html