空间索引+时刻分区
- 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
(编辑:湖南网)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|