HTAP数据库 PostgreSQL 场景与性能测试之 15 - (OLTP) 物联网 - 查询一个时序区间的数据

简介:

标签

PostgreSQL , HTAP , OLTP , OLAP , 场景与性能测试


背景

PostgreSQL是一个历史悠久的数据库,历史可以追溯到1973年,最早由2014计算机图灵奖得主,关系数据库的鼻祖Michael_Stonebraker 操刀设计,PostgreSQL具备与Oracle类似的功能、性能、架构以及稳定性。

pic

PostgreSQL社区的贡献者众多,来自全球各个行业,历经数年,PostgreSQL 每年发布一个大版本,以持久的生命力和稳定性著称。

2017年10月,PostgreSQL 推出10 版本,携带诸多惊天特性,目标是胜任OLAP和OLTP的HTAP混合场景的需求:

《最受开发者欢迎的HTAP数据库PostgreSQL 10特性》

1、多核并行增强

2、fdw 聚合下推

3、逻辑订阅

4、分区

5、金融级多副本

6、json、jsonb全文检索

7、还有插件化形式存在的特性,如 向量计算、JIT、SQL图计算、SQL流计算、分布式并行计算、时序处理、基因测序、化学分析、图像分析 等。

pic

在各种应用场景中都可以看到PostgreSQL的应用:

pic

PostgreSQL近年来的发展非常迅猛,从知名数据库评测网站dbranking的数据库评分趋势,可以看到PostgreSQL向上发展的趋势:

pic

从每年PostgreSQL中国召开的社区会议,也能看到同样的趋势,参与的公司越来越多,分享的公司越来越多,分享的主题越来越丰富,横跨了 传统企业、互联网、医疗、金融、国企、物流、电商、社交、车联网、共享XX、云、游戏、公共交通、航空、铁路、军工、培训、咨询服务等 行业。

接下来的一系列文章,将给大家介绍PostgreSQL的各种应用场景以及对应的性能指标。

环境

环境部署方法参考:

《PostgreSQL 10 + PostGIS + Sharding(pg_pathman) + MySQL(fdw外部表) on ECS 部署指南(适合新用户)》

阿里云 ECS:56核,224G,1.5TB*2 SSD云盘。

操作系统:CentOS 7.4 x64

数据库版本:PostgreSQL 10

PS:ECS的CPU和IO性能相比物理机会打一定的折扣,可以按下降1倍性能来估算。跑物理主机可以按这里测试的性能乘以2来估算。

场景 - 物联网 - 查询一个时序区间的数据 (OLTP)

1、背景

在物联网、互联网、业务系统中都有时序数据,随着时间推移产生的数据。在时间维度或序列字段上呈现自增特性。

区间查询是一种按范围查询的业务需求。

PostgreSQL针对时序类型的数据,除了有传统的b-tree索引,还有一种块级索引BRIN,非常适合这种相关性很好的时序数据。这种索引在Oracle Exadata一体机上也有。而使用PostgreSQL可以免费享用这种高端特性。

2、设计

1亿条时序自增记录,按任意区间查询并输出 5万条记录。

3、准备测试表

create table t_range(  
  id int,  
  ts timestamp default clock_timestamp()  
);  

4、准备测试函数(可选)

5、准备测试数据

insert into t_range(id) select generate_series(1,100000000);  

6、准备测试脚本

1、使用传统的b-tree索引

btree索引占用2142MB空间。

create index idx_t_range_id on t_range using btree (id);  
  
postgres=# \di+ idx_t_range_id  
                              List of relations  
 Schema |      Name      | Type  |  Owner   |  Table  |  Size   | Description  
--------+----------------+-------+----------+---------+---------+-------------  
 public | idx_t_range_id | index | postgres | t_range | 2142 MB |  
(1 row)  

单次查询效率:

postgres=# explain (analyze,verbose,timing,costs,buffers) select * from t_range where id between 1 and 50000;  
                                                                QUERY PLAN  
-------------------------------------------------------------------------------------------------------------------------------------------  
 Index Scan using idx_t_range_id on public.t_range  (cost=0.57..1527.31 rows=53167 width=12) (actual time=0.013..9.938 rows=50000 loops=1)  
   Output: id, ts  
   Index Cond: ((t_range.id >= 1) AND (t_range.id <= 50000))  
   Buffers: shared hit=411  
 Planning time: 0.060 ms  
 Execution time: 14.320 ms  
(6 rows)  
vi test.sql  
  
\set id random(1,90000000)  
\set mx :id+50000  
select * from t_range where id between :id and :mx;  

2、使用BRIN块级索引

BRIN索引仅占用256KB空间。

drop index idx_t_range_id;  
create index idx_t_range_id on t_range using brin (id) with (pages_per_range=64);  
postgres=# \di+ idx_t_range_id  
                              List of relations  
 Schema |      Name      | Type  |  Owner   |  Table  |  Size  | Description  
--------+----------------+-------+----------+---------+--------+-------------  
 public | idx_t_range_id | index | postgres | t_range | 256 kB |  
(1 row)  

单次查询效率:

postgres=# explain (analyze,verbose,timing,costs,buffers) select * from t_range where id between 1 and 50000;  
                                                          QUERY PLAN  
-------------------------------------------------------------------------------------------------------------------------------  
 Bitmap Heap Scan on public.t_range  (cost=43.31..52572.18 rows=38593 width=12) (actual time=1.497..9.807 rows=50000 loops=1)  
   Output: id, ts  
   Recheck Cond: ((t_range.id >= 1) AND (t_range.id <= 50000))  
   Rows Removed by Index Recheck: 9200  
   Heap Blocks: lossy=320  
   Buffers: shared hit=355  
   ->  Bitmap Index Scan on idx_t_range_id  (cost=0.00..33.66 rows=47360 width=0) (actual time=1.489..1.489 rows=3200 loops=1)  
         Index Cond: ((t_range.id >= 1) AND (t_range.id <= 50000))  
         Buffers: shared hit=35  
 Planning time: 0.036 ms  
 Execution time: 14.162 ms  
(11 rows)  

压测

vi test.sql  
  
\set id random(1,90000000)  
\set mx :id+50000  
select * from t_range where id between :id and :mx;  

7、测试

压测

CONNECTS=16  
TIMES=300  
export PGHOST=$PGDATA  
export PGPORT=1999  
export PGUSER=postgres  
export PGPASSWORD=postgres  
export PGDATABASE=postgres  
  
pgbench -M prepared -n -r -f ./test.sql -P 5 -c $CONNECTS -j $CONNECTS -T $TIMES  

8、测试结果

1、b-tree索引

transaction type: ./test.sql  
scaling factor: 1  
query mode: prepared  
number of clients: 16  
number of threads: 16  
duration: 300 s  
number of transactions actually processed: 188165  
latency average = 25.509 ms  
latency stddev = 4.625 ms  
tps = 627.166703 (including connections establishing)  
tps = 627.187145 (excluding connections establishing)  
script statistics:  
 - statement latencies in milliseconds:  
         0.002  \set id random(1,90000000)  
         0.000  \set mx :id+50000  
        25.507  select * from t_range where id between :id and :mx;  

2、brin索引

transaction type: ./test.sql  
scaling factor: 1  
query mode: prepared  
number of clients: 16  
number of threads: 16  
duration: 300 s  
number of transactions actually processed: 189889  
latency average = 25.278 ms  
latency stddev = 4.570 ms  
tps = 632.907768 (including connections establishing)  
tps = 632.927776 (excluding connections establishing)  
script statistics:  
 - statement latencies in milliseconds:  
         0.002  \set id random(1,90000000)  
         0.000  \set mx :id+50000  
        25.276  select * from t_range where id between :id and :mx;  

TPS

1、b-tree索引

627  
  
相当于每秒返回3135万行记录。  

2、brin索引

632  
  
相当于每秒返回3160万行记录。  

平均响应时间

1、b-tree索引

25.509 毫秒  

2、brin索引

25.278 毫秒  

参考

《PostgreSQL、Greenplum 应用案例宝典《如来神掌》 - 目录》

《数据库选型之 - 大象十八摸 - 致 架构师、开发者》

《PostgreSQL 使用 pgbench 测试 sysbench 相关case》

《数据库界的华山论剑 tpc.org》

https://www.postgresql.org/docs/10/static/pgbench.html

相关实践学习
阿里云AIoT物联网开发实战
本课程将由物联网专家带你熟悉阿里云AIoT物联网领域全套云产品,7天轻松搭建基于Arduino的端到端物联网场景应用。 开始学习前,请先开通下方两个云产品,让学习更流畅: IoT物联网平台:https://iot.console.aliyun.com/ LinkWAN物联网络管理平台:https://linkwan.console.aliyun.com/service-open
目录
相关文章
|
人工智能 安全 机器人
无代码革命:10分钟打造企业专属数据库查询AI机器人
随着数字化转型加速,企业对高效智能交互解决方案的需求日益增长。阿里云AppFlow推出的AI助手产品,借助创新网页集成技术,助力企业打造专业数据库查询助手。本文详细介绍通过三步流程将AI助手转化为数据库交互工具的核心优势与操作指南,包括全场景适配、智能渲染引擎及零代码配置等三大技术突破。同时提供Web集成与企业微信集成方案,帮助企业实现便捷部署与安全管理,提升内外部用户体验。
1362 12
无代码革命:10分钟打造企业专属数据库查询AI机器人
|
Cloud Native 关系型数据库 分布式数据库
|
并行计算 关系型数据库 MySQL
如何用 esProc 将数据库表转储提速查询
当数据库查询因数据量大或繁忙变慢时,可借助 esProc 将数据导出为文件进行计算,大幅提升性能。以 MySQL 的 3000 万行订单数据为例,两个典型查询分别耗时 17.69s 和 63.22s。使用 esProc 转储为二进制行存文件 (btx) 或列存文件 (ctx),结合游标过滤与并行计算,性能显著提升。例如,ctx 并行计算将原查询时间缩短至 0.566s,TopN 运算提速达 30 倍。esProc 的简洁语法和高效文件格式,特别适合历史数据的复杂分析场景。
|
存储 关系型数据库 测试技术
拯救海量数据:PostgreSQL分区表性能优化实战手册(附压测对比)
本文深入解析PostgreSQL分区表的核心原理与优化策略,涵盖性能痛点、实战案例及压测对比。首先阐述分区表作为继承表+路由规则的逻辑封装,分析分区裁剪失效、全局索引膨胀和VACUUM堆积三大性能杀手,并通过电商订单表崩溃事件说明旧分区维护的重要性。接着提出四维设计法优化分区策略,包括时间范围分区黄金法则与自动化维护体系。同时对比局部索引与全局索引性能,展示后者在特定场景下的优势。进一步探讨并行查询优化、冷热数据分层存储及故障复盘,解决分区锁竞争问题。
|
关系型数据库 分布式数据库 数据库
|
关系型数据库 分布式数据库 PolarDB
《阿里云产品手册2022-2023 版》——PolarDB for PostgreSQL
《阿里云产品手册2022-2023 版》——PolarDB for PostgreSQL
801 0
|
存储 缓存 关系型数据库
|
存储 SQL 并行计算
PolarDB for PostgreSQL 开源必读手册-开源PolarDB for PostgreSQL架构介绍(中)
PolarDB for PostgreSQL 开源必读手册-开源PolarDB for PostgreSQL架构介绍
1044 0
|
存储 算法 安全
PolarDB for PostgreSQL 开源必读手册-开源PolarDB for PostgreSQL架构介绍(下)
PolarDB for PostgreSQL 开源必读手册-开源PolarDB for PostgreSQL架构介绍
876 0

相关产品

  • 云原生数据库 PolarDB
  • 云数据库 RDS PostgreSQL 版