空间索引+时间分区
- create index idx_20170101
- on tbl using gist (pos)
- where crt_time between 2017-01-01 and 2017-01-02 ;
- ...
- create index idx_20170102
- on tbl using gist (pos)
- where crt_time between 2017-01-02 and 2017-01-03 ;
- ...
通过使用前述分区索引,可以在输入时间范围后快速定位目标数据,执行空间搜索。
- select * from tbl
- where crt_time between 2017-01-01 and 2017-01-02 -- Time
- and (pos <-> ?) < ? -- Distance to a point to be searched for
- and ? -- Other conditions
- order by pos <-> ? -- Sort by distance
- limit ?; -- Number of results to be returned
可以使用更多的索引分区,比如用作搜索条件和商店类型的维度(对象属性)(假设它是可枚举的或在范围相对较小的情况下)。
- create index idx_20170101_mod0 on tbl using gist (pos) where crt_time between 2017-01-01 and 2017-01-02 and dtype=0;
- ...
- create index idx_20170101_mod1 on tbl using gist (pos) where crt_time between 2017-01-01 and 2017-01-02 and dtype=1;
- ...
通过使用前面的分区索引,在输入时间范围或特定条件以执行空间搜索后,可以快速定位目标数据。
- select * from tbl
- where crt_time between 2017-01-01 and 2017-01-02 -- Time
- and (pos <-> ?) < ? -- Distance to a point to be searched for
- and dtype=0 -- Object condition
- and ? -- Other conditions
- order by pos <-> ? -- Sort by distance
- limit ?; -- Number of results to be returned
请注意,前面的SQL查询可以实现最佳性能优化。
索引组织形式(或索引结构)可以由逻辑分区重新构造,可以用上述类似的索引创建方法覆盖所有条件。
CTID相交阵列连接扫描
如前所述,BitmapAnd和BitmapOr合并扫描是在多个索引或GIN索引中自动执行的。事实上,这种扫描也可以在SQL中显式执行。
每个条件渗透对应的CTID。
使用Intersect或Union生成满足总体需求的CTID。(Intersect对应于“and”条件;union对应于“or”条件。)
生成一个ctid数组。
示例

图片来源:unsplash.com/@markusspiske
1. 创建对象提要数据表
- postgres=# create table tbl (id int, info text, crt_time timestamp, pos point, c1 int , c2 int, c3 int );
- CREATE TABLE
2. 将5000万条测试数据写入表中
- postgres=# insert into tbl select generate_series(1,50000000), md5(random()::text), clock_timestamp(), point(180-random()*180, 90-random()*90), random()*10000, random()*5000, random()*1000;
- INSERT 0 50000000
(编辑:晋中站长网)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|