HybridDB for PostgreSQL 轨迹相似(伴随分析)

本文涉及的产品
云原生数据库 PolarDB MySQL 版,Serverless 5000PCU 100GB
云原生数据库 PolarDB 分布式版,标准版 2核8GB
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
简介:

标签

PostgreSQL , Greenplum , 伴随分析 , 轨迹相似


背景

《阿里云 PostgreSQL 产品生态;案例、开发实践、管理实践、学习资料、学习视频 - 珍藏级》

以上有个CASE是讲:如何找出在同一辆车内的人,实际上就是通过车辆、人物的轨迹点数据,进行多轮的求交集,将结果收敛到一个较小的范围。

Greenplum伴随分析测试

1、创建测试表

create table tbl_pos (uid int8, phonenum text, ts timestamp, pos geometry, ghash text);    

2、写入测试数据3亿条左右

insert into tbl_pos  
select uid, md5(uid::text), ts, pos, st_geohash(pos,8) from  
(  
  select   
    (random()*10000000)::int8 as uid,   
    ts,   
    st_setsrid(st_makepoint(120.2+0.5-random(), 30.3+0.5-random()), 4326) as pos   
  from   
  generate_series('2018-01-01'::timestamp, '2019-01-01'::timestamp, interval '0.1 sec')   
t (ts)  
) t;  

3、重复生成数据到100亿

postgres=> insert into tbl_pos select * from tbl_pos;  
INSERT 0 315360001  
postgres=> \timing  
Timing is on.  
postgres=> insert into tbl_pos select * from tbl_pos;  
INSERT 0 630720002  
Time: 8992.130 ms (00:08.992)  
postgres=> insert into tbl_pos select * from tbl_pos;  
INSERT 0 1261440004  
Time: 19517.465 ms (00:19.517)  
postgres=> insert into tbl_pos select * from tbl_pos;  
INSERT 0 2522880008  
Time: 37244.890 ms (00:37.245)  
postgres=> insert into tbl_pos select * from tbl_pos;  
INSERT 0 5045760016  
Time: 73391.309 ms (01:13.391)  

4、新增索引

create index idx_tbl_pos_1 on tbl_pos(ghash);  
create index idx_tbl_pos_2 on tbl_pos using gist(pos);  
create index idx_tbl_pos_3 on tbl_pos (ts);  

5、收集统计信息

postgres=> vacuum analyze tbl_pos;  

6、伴随分析

postgres=> select count(*) from tbl_pos where ghash >= 'wtmm1k' and ghash < 'wtmm1'||chr(ascii('k')+1);  
 count    
--------  
 609632  
(1 row)  
  
Time: 60.275 ms  
  
postgres=> select count(*) from tbl_pos where ts between '2018-01-01 00:06:25.2' and '2018-01-01 01:06:25.2' and ghash >= 'wtmmmm' and ghash < 'wtmmm'||chr(ascii('m')+1);  
 count   
-------  
 69984  
(1 row)  
  
Time: 70.161 ms  
  
  
postgres=> explain select count(*) from tbl_pos where ts between '2018-01-01 00:06:25.2' and '2018-01-01 01:06:25.2' and ghash >= 'wtmmmm' and ghash < 'wtmmm'||chr(ascii('m')+1);  
                                                                                                     QUERY PLAN                                                                                                       
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------  
 Aggregate  (cost=10681940.85..10681940.86 rows=1 width=8)  
   ->  Gather Motion 1024:1  (slice1; segments: 1024)  (cost=10681930.57..10681940.83 rows=1 width=8)  
         ->  Aggregate  (cost=10681930.57..10681930.58 rows=1 width=8)  
               ->  Bitmap Heap Scan on tbl_pos  (cost=10520118.62..10681889.69 rows=16 width=0)  
                     Recheck Cond: ts >= '2018-01-01 00:06:25.2'::timestamp without time zone AND ts <= '2018-01-01 01:06:25.2'::timestamp without time zone AND ghash >= 'wtmmmm'::text AND ghash < 'wtmmmn'::text  
                     ->  BitmapAnd  (cost=10520118.62..10520118.62 rows=2 width=0)  
                           ->  Bitmap Index Scan on idx_tbl_pos_3  (cost=0.00..1751.33 rows=161 width=0)  
                                 Index Cond: ts >= '2018-01-01 00:06:25.2'::timestamp without time zone AND ts <= '2018-01-01 01:06:25.2'::timestamp without time zone  
                           ->  Bitmap Index Scan on idx_tbl_pos_1  (cost=0.00..10518358.87 rows=97466 width=0)  
                                 Index Cond: ghash >= 'wtmmmm'::text AND ghash < 'wtmmmn'::text  
 Settings:  effective_cache_size=8GB; enable_seqscan=off; gp_statistics_use_fkeys=on; optimizer=off; work_mem=64MB  
 Optimizer status: legacy query optimizer  
(12 rows)  

在某5天出现在某个范围的人的交集:

select uid from   
(select distinct uid from tbl_pos where ts between '2018-01-01 00:06:25.2' and '2018-01-01 01:06:25.2' and ghash >= 'wtmmmm' and ghash < 'wtmmm'||chr(ascii('m')+1)) t1  
join  
(select distinct uid from tbl_pos where ts between '2018-02-01 00:06:25.2' and '2018-02-01 01:06:25.2' and ghash >= 'wtmmmm' and ghash < 'wtmmm'||chr(ascii('m')+1)) t2  
using (uid)  
join  
(select distinct uid from tbl_pos where ts between '2018-03-01 00:06:25.2' and '2018-03-01 01:06:25.2' and ghash >= 'wtmmmm' and ghash < 'wtmmm'||chr(ascii('m')+1)) t3  
using (uid)  
join  
(select distinct uid from tbl_pos where ts between '2018-04-01 00:06:25.2' and '2018-04-01 01:06:25.2' and ghash >= 'wtmmmm' and ghash < 'wtmmm'||chr(ascii('m')+1)) t4  
using (uid)  
join  
(select distinct uid from tbl_pos where ts between '2018-05-01 00:06:25.2' and '2018-05-01 01:06:25.2' and ghash >= 'wtmmmm' and ghash < 'wtmmm'||chr(ascii('m')+1)) t5  
using (uid);  
    
  
 uid   
-----  
(0 rows)  
  
Time: 207.750 ms  

小结

1、PostgreSQL bitmap scan支持将多个索引合并,在本例中体现在时间、空间、(其他)条件。

《PostgreSQL 数据库多列复合索引的字段顺序选择原理》

《PostgreSQL bitmapAnd, bitmapOr, bitmap index scan, bitmap heap scan》

2、除了使用这种暴力求交集的方法,还有别的方法来计算伴随么?相似轨迹分析可能也是一个不错的选择

参考

《PostgreSQL + PostGIS 时态分析》

《阿里云 PostgreSQL 产品生态;案例、开发实践、管理实践、学习资料、学习视频 - 珍藏级》

相关实践学习
使用PolarDB和ECS搭建门户网站
本场景主要介绍基于PolarDB和ECS实现搭建门户网站。
阿里云数据库产品家族及特性
阿里云智能数据库产品团队一直致力于不断健全产品体系,提升产品性能,打磨产品功能,从而帮助客户实现更加极致的弹性能力、具备更强的扩展能力、并利用云设施进一步降低企业成本。以云原生+分布式为核心技术抓手,打造以自研的在线事务型(OLTP)数据库Polar DB和在线分析型(OLAP)数据库Analytic DB为代表的新一代企业级云原生数据库产品体系, 结合NoSQL数据库、数据库生态工具、云原生智能化数据库管控平台,为阿里巴巴经济体以及各个行业的企业客户和开发者提供从公共云到混合云再到私有云的完整解决方案,提供基于云基础设施进行数据从处理、到存储、再到计算与分析的一体化解决方案。本节课带你了解阿里云数据库产品家族及特性。
相关文章
|
4月前
|
关系型数据库 MySQL Serverless
高顿教育:大数据抽数分析业务引入polardb mysql serverless
高顿教育通过使用polardb serverless形态进行数据汇总,然后统一进行数据同步到数仓,业务有明显高低峰期,灵活的弹性伸缩能力,大大降低了客户使用成本。
|
5月前
|
关系型数据库 BI 分布式数据库
PolarDB NL2BI解决方案,让你不懂SQL也能进行数据查询分析并生成BI报表
无需创建和开通资源,在预置环境中免费体验PolarDB MySQL及其NL2BI解决方案
PolarDB NL2BI解决方案,让你不懂SQL也能进行数据查询分析并生成BI报表
|
2月前
|
存储 关系型数据库 MySQL
TiDB与MySQL、PostgreSQL等数据库的比较分析
【2月更文挑战第25天】本文将对TiDB、MySQL和PostgreSQL等数据库进行详细的比较分析,探讨它们各自的优势和劣势。TiDB作为一款分布式关系型数据库,在扩展性、并发性能等方面表现突出;MySQL以其易用性和成熟性受到广泛应用;PostgreSQL则在数据完整性、扩展性等方面具有优势。通过对比这些数据库的特点和适用场景,帮助企业更好地选择适合自己业务需求的数据库系统。
|
3月前
|
关系型数据库 分布式数据库 PolarDB
电子书阅读分享《PolarDB开发者大会:PolarDB在线数据实时分析加速》
电子书阅读分享《PolarDB开发者大会:PolarDB在线数据实时分析加速》
85 3
|
3月前
|
关系型数据库 分布式数据库 PolarDB
电子书阅读分享《PolarDB开发者大会:PolarDB在线数据实时分析加速》
电子书阅读分享《PolarDB开发者大会:PolarDB在线数据实时分析加速》
76 1
|
3月前
|
关系型数据库 分布式数据库 PolarDB
电子书阅读分享《PolarDB开发者大会:PolarDB在线数据实时分析加速》
电子书阅读分享《PolarDB开发者大会:PolarDB在线数据实时分析加速》
89 1
|
4月前
|
存储 关系型数据库 分布式数据库
阿里云PolarDB解决乐麦多源数据存储性能问题
乐麦通过使用PolarDB数据库,使整个系统之间的数据查询分析更加高效
390 3
|
5月前
|
SQL 关系型数据库 MySQL
PostgreSQL【异常 01】java.io.IOException:Tried to send an out-of-range integer as a 2-byte value 分析+解决
PostgreSQL【异常 01】java.io.IOException:Tried to send an out-of-range integer as a 2-byte value 分析+解决
176 1
|
6月前
|
缓存 关系型数据库 Serverless
数据库内核那些事,PolarDB HTAP Serverless,打造经济易用的实时分析系统
下本从IMCI Serverless核心优势角度的介绍各优化工作内容。
数据库内核那些事,PolarDB HTAP Serverless,打造经济易用的实时分析系统
|
7月前
|
SQL 关系型数据库 MySQL
《PostgreSQL与MySQL:详细对比与分析》
《PostgreSQL与MySQL:详细对比与分析》
259 0

相关产品

  • 云原生数据库 PolarDB