Mysql 组合索引 最左侧原理

网上那么多关于组合索引详解 最左侧原理 : 个人感觉99%都是讲不清楚的废话,看完 都不知道在讲个jb
因此在此写一下!
原理:你使用的sql能否用到组合索引?
结论:你建立的组合索引(a,b,c,d) 无论怎么使用只要和a没有组合(最左面的那个),那么都没有使用上索引!!!!!
下面是验证:EXPLAIN 观察 type 列 
相关连接:[https://www.jianshu.com/p/ea3fc71fdc45(https://www.jianshu.com/p/ea3fc71fdc45)
  select table_name,
  DATA_LENGTH as tablesData,
  INDEX_LENGTH as indexData
  from information_schema.tables
  where table_schema='test'
  ORDER BY  indexData desc;

  -- 索引 ON `index` (a, b, c);
  EXPLAIN
  SELECT *
  FROM `index` WHERE a='a';-- type=ref ref=const rows =1 true

  EXPLAIN
  SELECT *
  FROM `index` WHERE b='b'; -- type=ALL rows=3 false

  EXPLAIN
  SELECT *
  FROM `index` WHERE c='c'; -- type=ALL rows=3 false

  EXPLAIN
    SELECT *
    FROM `index` WHERE a='a' AND b='b'; -- type=ref ref=const rows =1 true

  EXPLAIN
      SELECT *
      FROM `index` WHERE b='b'  AND a='a';-- type=ref ref=const rows =1 true

  EXPLAIN
      SELECT *
      FROM `index` WHERE a='a'  AND c='c';-- type=ref ref=const rows =1 true

  EXPLAIN
      SELECT *
      FROM `index` WHERE c='c'  AND a='a';-- type=ref ref=const rows =1 true

  EXPLAIN
      SELECT *
      FROM `index` WHERE c='c'  AND b='b';-- type=ref ref=const rows =1 false

  EXPLAIN
      SELECT *
      FROM `index` WHERE b='b'  AND c='c';-- type=ref ref=const rows =1 false










  EXPLAIN
      SELECT *
      FROM `index` WHERE a='a'  AND b='b' AND c='c';-- type=ref ref=const rows =1 true

  EXPLAIN
      SELECT *
      FROM `index` WHERE a='a'  AND c='c' AND b='b';-- type=ref ref=const rows =1 true

  EXPLAIN
      SELECT *
      FROM `index` WHERE b='b'  AND c='c' AND a='a';-- type=ref ref=const rows =1 true

  EXPLAIN
      SELECT *
      FROM `index` WHERE b='b'  AND a='a' AND c='c';-- type=ref ref=const rows =1 true

  EXPLAIN
      SELECT *
      FROM `index` WHERE c='c'  AND b='b' AND a='a';-- type=ref ref=const rows =1 true

  EXPLAIN
      SELECT *
      FROM `index` WHERE c='c'  AND a='a' AND b='b';-- type=ref ref=const rows =1 true



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值