暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

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

digoal 2017-11-07
272

作者

digoal

日期

2017-11-07

标签

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

PostgreSQL 许愿链接

您的愿望将传达给PG kernel hacker、数据库厂商等, 帮助提高数据库产品质量和功能, 说不定下一个PG版本就有您提出的功能点. 针对非常好的提议,奖励限量版PG文化衫、纪念品、贴纸、PG热门书籍等,奖品丰富,快来许愿。开不开森.

9.9元购买3个月阿里云RDS PostgreSQL实例

PostgreSQL 解决方案集合

德哥 / digoal's github - 公益是一辈子的事.

digoal's wechat

文章转载自digoal,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论