• MYSQL索引优化法则


    目录

      一首诗送给各位:

      全值匹配我最爱,最左前缀要遵守;
      带头大哥不能死,中间兄弟不能断;
      索引列上少计算,范围之后全失效;
      Like百分写最右,覆盖索引不写星;
      不等空值还有or,索引失效要少用;
      VAR引号不可丢,SQL高级也不难!

      举个栗子:
      假设index(a,b,c)

      where语句 索引是否被用到 原因
      where a=3 使用到a 全值匹配
      where a=3 and b=5 使用到a,b 全值匹配
      where a=3 and b=5 and c=4 使用到a,b,c 全值匹配
      where b=3 or where b=3 and c=4 or where c=4 NULL 因为按照创建索引的顺序第一个索引列a没有被用到,导致后面的索引失效。
      where a=3 and c=5 使用到a b没有被用到,导致c失效
      where a=3 and b>4 and c=5 使用到a b为范围查询索引失效导致C也失效
      where a=3 and b like 'kk%' and c =4 使用到a,b,c 这里的b是可以用到的因为百分号在最右结合最左前缀原则,虽然%相当于范围查询但是在最右,最左边是定值。
      where a=3 and b like '%kk' and c=4 使用到a b中间断开失效导致c也失效
      where a=3 and b like '%kk%' and c=4 使用到a b断开
      where a=3 and b like 'k%kk%' and c=4 使用到a,b,c 同上上上
    • 相关阅读:
      01_计算机基础
      09_哈希表
      08_查找算法
      Swagger使用
      Thymeleaf代码实例
      Spring boot代码实例
      Spring mvc代码实例
      Hibernate代码实例
      Mysql JDBC代码实例
      Mybatis代码实例
    • 原文地址:https://www.cnblogs.com/xhj928675426/p/14459124.html
    Copyright © 2020-2023  润新知