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

2016-06-29 12:56:11 +08:00
 1023k

如题。

#索引
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 的查询执行流程是怎样的?

873 次点击
所在节点    数据库
3 条回复
gclover
2016-06-29 13:53:28 +08:00
优化器会在索引存在的情况下,通过符合 RANGE 范围的条数和总数的比例来选择是使用索引还是进行全表遍历
https://segmentfault.com/q/1010000004197413
1023k
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
2016-06-29 15:25:24 +08:00
@1023k select discount_amount from test where discount_amount=10 and limit_amount>10;这个举例错了

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/289084

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX