zoukankan      html  css  js  c++  java
  • mysql 组合索引中对范围的查询

      

    建立表:

    CREATE TABLE `ygzt_test` (
    `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
    `a` int(11) NOT NULL,
    `b` int(11) NOT NULL,
    `c` int(11) NOT NULL,
    `d` int(11) NOT NULL,
    PRIMARY KEY (`id`),
    KEY `a` (`a`,`b`,`c`,`d`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci COMMENT='测试';

    一、实验一,无order by

    首先加联合索引a,b,c,d

    explain select * from ygzt_test where a=1 and b=2 and c=3 and d=4

    修改sql:

    explain select * from ygzt_test where a>1 and b=2 and c=3 and d=4

    type已经由ref降为index

    修改索引b,c,d,a

    explain select * from ygzt_test where a>1 and b=2 and c=3 and d=4

    done

    二、实验二,order by

    建立索引a,b

    explain select * from ygzt_test where a>0 order by b

    可以看到,a>0使用了索引,order by b 未使用

    修改索引为b,a

    explain select * from ygzt_test where a>0   order by b

     

    where 与 order by 都无索引

    想起此前order by+select *的问题

    这个问题单独拿出来实践下:

    修改索引为b

    explain select * from ygzt_test order by b

    缺点:未使用索引

    explain select b from ygzt_test order by b

     缺点:只能返回b

    explain select b,a from ygzt_test  order by b

    缺点:未使用索引

    explain select * from ygzt_test FORCE INDEX (b)  order by b

    缺点:type也就是index级别

    索引b,a

    explain select b,a from ygzt_test  order by b

    缺点:与强制索引同,只能达到index级别,还加大了索引建立的复杂度

    结论:使用强制索引

    结论:

    1.单列索引中——<,<=,=,>,>=,between,like(右边模糊)适用索引

    2.索引中有范围的,有序性失效,解决方案以实际为准

  • 相关阅读:
    TPS限流
    JDK并发基础与部分源码解读
    tomcat6-servlet规范对接 与 ClassLoader隔离
    tomcat6-输入输出buffer设计
    tomcat6-endpoint设计
    springMVC请求路径 与实际资源路径关系
    mysql 常用的数据类型
    认识IPv4分组
    CSMA/CD协议(载波侦听多路访问/碰撞检测) 最小帧长理解
    简单的vector--- 2
  • 原文地址:https://www.cnblogs.com/silyvin/p/9251297.html
Copyright © 2011-2022 走看看