1023k
V2EX  ›  数据库

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

  •  
  •   1023k · Jun 29, 2016 · 1883 views
    This topic created in 3630 days ago, the information mentioned may be changed or developed.

    如题。

    #索引
    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  
       Jun 29, 2016
    优化器会在索引存在的情况下,通过符合 RANGE 范围的条数和总数的比例来选择是使用索引还是进行全表遍历
    https://segmentfault.com/q/1010000004197413
    1023k
        2
    1023k  
    OP
       Jun 29, 2016
    @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
       Jun 29, 2016
    @1023k select discount_amount from test where discount_amount=10 and limit_amount>10;这个举例错了
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2758 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 11:37 · PVG 19:37 · LAX 04:37 · JFK 07:37
    ♥ Do have faith in what you're doing.