标签
PostgreSQL , limit , order by , 优化器 , 选择性 , 相关性 , 数据存储顺序 , 目标数据存储顺序
背景
当我们在执行一个这样的SQL时,假如有这样几个索引(c1,c2) (id),数据库到底该用哪个索引呢?
explain select * from tbl where c1=200 and c2=200 order by id limit 10;...
问题描述
bug 触发条件如下:
优化器先选择了 where 条件中字段的索引,该索引过滤性较好;
SQL 中必须有 order by limit 从而引导优化器尝试使用 order by 字段上的索引进行优化,最终因代价问题没有成功。
复现case
表结构
create table t1(
id int auto_increment primary key,
a int, b ...