zoukankan      html  css  js  c++  java
  • 如何理解MySQL 索引最左前缀原则

    在创建一个n列的索引时,需要遵循“最左前缀”原则。

    创建表:create table abc(a varchar(32) not null, b varchar(32), c date, d varchar(32) );

    创建普通索引:create index in_abc_acb on abc(a, c, b);

    1、select * from abc where  d='d' and b='b';  不会用到索引,必须要用到左边第一个字段;

    2、select * from abc where a like '1%' and c=sysdate;  a 会用到索引,c 则不会,遇到范围(>、<、between、like)查询就停止匹配,而 where a = '%1',不会用到索引;

    3、select * from abc where trim(a) = 'a' ; 不会用到索引 ,字段前加了函数(表达式)索引会被抑制。where a = 'a' and c + 1 > sysdate,c不会用到索引,而where a = 'a' and c > sysdate - 2,会用到索引;

    4、select * from abc where b like 'b%' and c = sysdate and a='a' ;acb的索引都可以用到,因为索引是从左到右匹配,in 和 = 可以乱序;

  • 相关阅读:
    等价表达式
    读入字符串
    n以内质数占的比例
    图论——最小生成树_prim
    搜索
    图论——最小生成树
    线段树模板
    WC总结
    三练斜率优化
    斜率优化技巧——换个角度思考
  • 原文地址:https://www.cnblogs.com/haiyangwu/p/10463125.html
Copyright © 2011-2022 走看看