加入收藏 | 设为首页 | 会员中心 | 我要投稿 湖南网 (https://www.hunanwang.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程 > 正文

海量、多维数据让人抓狂?高效搜刮要领看这里

发布时间:2019-09-17 14:38:57 所属栏目:编程 来源:读芯术
导读:人与天下万物的互动会发生大量的时空数据。那么,当我们必要随时挪用已往的数据时,改怎么办?尤其是面临各类海量、多维度的数据库,假如没有高效的搜刮要领,我们只能望洋兴叹、一筹莫展。 别担忧,本文将器具体的代码,手把手来教授高效搜刮法的特技! 对

空间索引+时刻分区

  1. create index idx_20170101  
  2. on tbl using gist (pos)  
  3. where crt_time between 2017-01-01 and 2017-01-02 ;  
  4. ...  
  5. create index idx_20170102  
  6. on tbl using gist (pos)  
  7. where crt_time between 2017-01-02 and 2017-01-03 ;  
  8. ... 

通过行使前述分区索引,可以在输入时刻范畴后快速定位方针数据,执行空间搜刮。

  1. select * from tbl  
  2.  where crt_time between 2017-01-01 and 2017-01-02 -- Time  
  3.  and (pos <-> ?) < ? -- Distance to a point to be searched for  
  4.  and ? -- Other conditions  
  5.  order by pos <-> ? -- Sort by distance  
  6.  limit ?; -- Number of results to be returned 

可以行使更多的索引分区,好比用作搜刮前提和市肆范例的维度(工具属性)(假设它是可列举的或在范畴相对较小的环境下)。

  1. create index idx_20170101_mod0 on tbl using gist (pos) where crt_time between 2017-01-01 and 2017-01-02 and dtype=0;  
  2. ...  
  3. create index idx_20170101_mod1 on tbl using gist (pos) where crt_time between 2017-01-01 and 2017-01-02 and dtype=1;  
  4. ... 

通过行使前面的分区索引,在输入时刻范畴或特定前提以执行空间搜刮后,可以快速定位方针数据。

  1. select * from tbl  
  2.  where crt_time between 2017-01-01 and 2017-01-02 -- Time  
  3.  and (pos <-> ?) < ? -- Distance to a point to be searched for  
  4.  and dtype=0 -- Object condition  
  5.  and ? -- Other conditions  
  6.  order by pos <-> ? -- Sort by distance  
  7.  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. 建设工具概要数据表

  1. postgres=# create table tbl (id int, info text, crt_time timestamp, pos point, c1 int , c2 int, c3 int );  
  2. CREATE TABLE 

2. 将5000万条测试数据写入表中

  1. 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;  
  2. INSERT 0 50000000 

(编辑:湖南网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

热点阅读