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

复合查询SQL优化问题 - ( 范围检索 , IN检索加时间倒序) #32

Open
cosionbob opened this issue Jul 2, 2018 · 2 comments

Comments

@cosionbob
Copy link

如德哥写的**PostgreSQL 一复合查询SQL优化例子 - (多个exists , 范围检索 , IN检索 , 模糊检索 组合)**这篇文章如果条件改成create table test(id int, c1 text, c2 date, c3 text);
select * from test
where
c1 in ('1','2','3')
and c2 between current_date-1 and current_date
oder by c2 desc limit 10;
这样的查询语句创建btree联合索引create index idx_c2_c1_test on test(c2,c1);
使用这个索引只会读取时间范围,而c1的过滤却要回表过滤,而Oracle是会在索引上全部过滤掉的,索引性能pg不如Oracle快。
使用gin索引好像速度也不是很快。
感谢能给出优化方法或思路,万分感谢。

@digoal
Copy link
Owner

digoal commented Jun 13, 2023

PG高版本支持了增量排序. 你可以再试一试.

@digoal
Copy link
Owner

digoal commented Jun 13, 2023

create table test(id int, c1 text, c2 date, c3 text, c4 float);

create index idx on test (c1,c2,c4 desc)

select * from test
where
c1 in ('1','2','3')
and c2 between current_date-1 and current_date
order by c4 desc limit 10;

c1, c2 都是离散值.
c1,c2在输入条件c1 in ('1','2','3') and c2 between current_date-1 and current_date中的所有可能值可以组成一个有限的排列组合.
idx索引的每个c1,c2组合下的所有index tuplec4 desc上是有序的, 所以使用idx索引, 按每个c1,c2组合跳跃查询, 并持续使用merge sort(c4), 即可最高效的返回数据.
每个c1,c2组合跳跃查询可以使用并行进行. 例如每个worker扫描一组c1,c2条件.

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