V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
1023k
V2EX  ›  数据库

在 mysql 中,查询使用了覆盖索引,为什么查询时还会用到 using where 过滤呢

  •  
  •   1023k · 2016-06-29 12:56:11 +08:00 · 872 次点击
    这是一个创建于 2862 天前的主题,其中的信息可能已经有所发展或是发生改变。

    如题。

    #索引
    alter table test add index `idx`(`discount_amount`,`limit_amount`);
    

    使用了覆盖索引,但还会出现 using where

    mysql> explain select discount_amount from test where discount_amount=10 and limit_amount>10;
    +----+-------------+-------+-------+------+---------+--------+--------------------------+
    | id | select_type | table | type  | key  | key_len | rows   | Extra                    |
    +----+-------------+-------+-------+------+---------+--------+--------------------------+
    |  1 | SIMPLE      | test  | range | idx  | 4       | 660750 | Using where; Using index |
    +----+-------------+-------+-------+------+---------+--------+--------------------------+
    

    现在有点懵了,使用到 using where 的查询执行流程是怎样的?

    gclover
        1
    gclover  
       2016-06-29 13:53:28 +08:00
    优化器会在索引存在的情况下,通过符合 RANGE 范围的条数和总数的比例来选择是使用索引还是进行全表遍历
    https://segmentfault.com/q/1010000004197413
    1023k
        2
    1023k  
    OP
       2016-06-29 15:20:12 +08:00
    @gclover 看了链接的内容,不知道我理解的对不对,出现这种情况是不是意味着:符合 discount_amount=10 的索引都返回到 mysql server 过滤 limit_amount > 10 的索引值,所以就会出现 using where 。就跟把 index_condition_pushdown 关闭了,执行 explain select * from test where discount_amount=10 and limit_amount>10 出现 using where 的原理一样?
    1023k
        3
    1023k  
    OP
       2016-06-29 15:25:24 +08:00
    @1023k select discount_amount from test where discount_amount=10 and limit_amount>10;这个举例错了
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2705 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 10:52 · PVG 18:52 · LAX 03:52 · JFK 06:52
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.