加入收藏 | 设为首页 | 会员中心 | 我要投稿 源码网 (https://www.900php.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > MySql教程 > 正文

海量、多维数据让人抓狂?高效搜索方法看这里

发布时间:2019-09-16 22:47:06 所属栏目:MySql教程 来源:读芯术
导读:副标题#e# 人与世界万物的互动会产生大量的时空数据。那么,当我们需要随时调用过去的数据时,改怎么办?尤其是面对各种海量、多维度的数据库,如果没有高效的搜索方法,我们只能望洋兴叹、束手无策。 别担心,本文将用详细的代码,手把手来传授高效搜索法的

空间索引+时间分区

  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 

(编辑:源码网)

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

热点阅读