MySQL(14)命中索引简略

  • 类型不一致

    select * from big where name = 123;  -- 未命中
    select * from big where email = 123; -- 未命中
    
    特殊的主键:
     select * from big where id = "123"; -- 命中
    
  • 使用不等于

    select * from big where name != "武沛齐";    -- 未命中
    select * from big where email != "wupeiqi@live.com";  -- 未命中
    
    特殊的主键:
     select * from big where id != 123; -- 命中
    
  • or,当or条件中有未建立索引的列才失效。

    select * from big where id = 123 or password="xx";   -- 未命中
    select * from big where name = "wupeiqi" or password="xx"; -- 未命中
    特别的:
     select * from big where id = 10 or password="xx" and name="xx"; -- 命中
    
  • 排序,当根据索引排序时候,选择的映射如果不是索引,则不走索引。

    select * from big order by name asc;     -- 未命中
    select * from big order by name desc;    -- 未命中
    
    特别的主键:
     select * from big order by id desc;  -- 命中
    
  • like,模糊匹配时。

    select * from big where name like "%u-12-19999"; -- 未命中
    select * from big where name like "_u-12-19999"; -- 未命中
    select * from big where name like "wu-%-10";  -- 未命中
    
    特别的:
     select * from big where name like "wu-1111-%"; -- 命中
     select * from big where name like "wuw-%";  -- 命中
    
  • 使用函数

    select * from big where reverse(name) = "wupeiqi";  -- 未命中
    
    特别的:
     select * from big where name = reverse("wupeiqi");  -- 命中
    
  • 最左前缀,如果是联合索引,要遵循最左前缀原则。

    如果联合索引为:(name,password)
        name and password       -- 命中
        name                  -- 命中
        password                -- 未命中
        name or password        -- 未命中
    
posted @ 2021-11-18 15:20  下个ID见  阅读(142)  评论(0)    收藏  举报