zoukankan      html  css  js  c++  java
  • mysql是如何利用索引的

    1、explain执行计划中的 key_len 的计算规则:

    • 一般key_len等于索引列字节长度,如int为4个字节,bigint为8个字节。
    • 字符串需要考虑字符集因素。如char(30) utf8的key_len至少是90个字节。
    • 类型为null,key_len需要加多1个字节。
    • 类型为可变类型如varchar,key_len需要加多2个字节。

    2、哪些条件能用到索引  

    •  index key 
      • eg1,注意「<」为上边界,「>」为下边界

          idx_c1_c2_c3(c1,c2,c3)

          where c1>=1 and c2>2 and c3=3;

          first key (c1,c2)

          c1为>=,加入下边界界定,继续匹配下一个。

          c2为>,加入下边界界定,停止匹配。

          where c1<=1 and c2=2 and c3<3;

          last key(c1,c2,c3)

          c1为<=,加入上边界界定,继续匹配下一个。

          c2为 = ,加入上边界界定,继续匹配下一个

          c3为<,加入上边界界定,停止匹配。

    • index filter

          idx_c1_c2_c3(c1,c2,c3)

          where c1>=1 and c2<=2 and c3=3;

          index key -> c1

          index filter -> c2 c3

       c2确定上边界,但是上边界中c1并非(<=,=),下边界中c1是>=且无c2,因此index key只有c1,而c2,c3都在索引中,因此可以被当做index filter。

    • table filter

      扫全表,引擎层会将行数据返回server 层,由server层进行table filter。

    3、between 和 like

    • between
      • where c1 between 1 and 2 等价于 where c1 >= 1 and c1<=2 
    • like
      • where c like "a%" 等价于 where c >= "a" and c < b       

    以上参考:https://mp.weixin.qq.com/s/mDJv_FH2TP0c7T-9nbm1oQ

  • 相关阅读:
    Remove Element
    Binary Tree Inorder Traversal
    Symmetric Tree
    Roman to Integer
    Search Insert Position
    Reverse Integer
    Pascal's Triangle
    Merge Sorted Array
    Same Tree
    Visual Studio Code 做PHP开发
  • 原文地址:https://www.cnblogs.com/smallzhen/p/14974942.html
Copyright © 2011-2022 走看看