Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

范围查询和in查询优化 #31

Open
cosionbob opened this issue Jun 20, 2018 · 1 comment
Open

范围查询和in查询优化 #31

cosionbob opened this issue Jun 20, 2018 · 1 comment

Comments

@cosionbob
Copy link

创建个新表create table a(id int,t_date timestamp,name varchar);
添加索引create index idx_date on a using btree(t_date desc);
create index idx_date_name on a using btree(t_date desc,name);
查询语句如:select * from a where t_date >='time1' and t_date <'time2' and name in ('a','b','c') order by t_date desc limit 10;
这样的查询执行计划会走单时间索引,选择出时间范围的数据在过滤name 数组,而不会像Oracle一样直接走date和name的联合索引直接查询?
希望得到德哥的解释,谢谢。

@digoal
Copy link
Owner

digoal commented Jun 13, 2023

这个查询取决于a,b,c的过滤性, 如果过滤性好, 可能就直接使用name的单一索引.
如果过滤性不好, 可以使用partial index: create index on a (t_date desc) where (name in ('a','b','c'));
但是使用idx_date_name还不如使用including语法, 把name放到idx_date的叶子结点中.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants