HTAP数据库 PostgreSQL 场景与性能测试之 45 - (OLTP) 数据量与性能的线性关系(10亿+无衰减), 暨单表多大需要分区

简介:

标签

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来估算。

场景 - 数据量与性能的线性关系(10亿+无衰减), 暨单表多大需要分区 (OLTP)

1、背景

这个测试回答用户一个问题,PostgreSQL在单表可以管理多大的数据量性能不会衰减。

单表多大需要分区。

单表记录数在不同的级别(如千万、亿、十亿、百亿),查询,更新,写入吞吐 分别是什么样的性能。

2、设计

单表记录数:千万、亿、十亿。

分别测试写入吞吐、查询tps,更新TPS。

3、准备测试表

create unlogged table test(id int primary key, info text, crt_time timestamp);    
    
create unlogged table test1(id int primary key, info text, crt_time timestamp);    
    
create unlogged table test3(id int primary key, info text, crt_time timestamp);    

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

5、准备测试数据

postgres=# insert into test select generate_series(1,10000000), 'test', now();    
INSERT 0 10000000    
Time: 17197.822 ms (00:17.198)    
    
postgres=# insert into test1 select generate_series(1,100000000), 'test', now();    
INSERT 0 100000000    
Time: 187844.576 ms (03:07.845)    
    
for ((i=1;i<=100;i++)) ; do psql -c "insert into test3 select generate_series(($i-1)*10000000+1, $i*10000000),'test', now();" & done    
写入10亿 耗时 615秒    

6、准备测试脚本

1、查询测试

-- 1000万    
vi test.sql    
    
\set id random(1,10000000)    
select * from test where id=:id;    
    
-- 1亿    
vi test1.sql    
    
\set id random(1,100000000)    
select * from test1 where id=:id;    
    
    
-- 10亿    
vi test3.sql    
    
\set id random(1,1000000000)    
select * from test3 where id=:id;    

2、更新测试

-- 1000万    
vi test.sql    
    
\set id random(1,10000000)    
update test set crt_time=now() where id=:id;    
    
-- 1亿    
vi test1.sql    
    
\set id random(1,100000000)    
update test1 set crt_time=now() where id=:id;    
    
    
-- 10亿    
-- 第三个CASE,虽然记录数10亿,但是频繁被更新的数据假设在1亿内。测试时被访问的数据依旧在10亿的范围。    
vi test3.sql    
    
\set id random(1,100000000)    
\set id1 random(1,1000000000)    
with tmp as (select * from test3 where id=:id1)    
update test3 set crt_time=now() where id=:id ;    

7、测试

测试脚本

CONNECTS=48     
TIMES=120      
export PGHOST=$PGDATA      
export PGPORT=1921      
export PGUSER=postgres      
export PGPASSWORD=postgres      
export PGDATABASE=postgres      
      
pgbench -M prepared -n -r -P 5 -f ./test.sql -c $CONNECTS -j $CONNECTS -T $TIMES      
pgbench -M prepared -n -r -P 5 -f ./test1.sql -c $CONNECTS -j $CONNECTS -T $TIMES      
pgbench -M prepared -n -r -P 5 -f ./test3.sql -c $CONNECTS -j $CONNECTS -T $TIMES      

8、测试结果

1、查询测试TPS

pgbench -M prepared -n -r -P 1 -f ./test.sql -c 48 -j 48 -T 120    
    
transaction type: ./test.sql    
scaling factor: 1    
query mode: prepared    
number of clients: 48    
number of threads: 48    
duration: 120 s    
number of transactions actually processed: 80378275    
latency average = 0.072 ms    
latency stddev = 0.012 ms    
tps = 669810.772760 (including connections establishing)    
tps = 669876.004400 (excluding connections establishing)    
script statistics:    
 - statement latencies in milliseconds:    
         0.001  \set id random(1,10000000)    
         0.071  select * from test where id=:id;    
    
pgbench -M prepared -n -r -P 1 -f ./test1.sql -c 48 -j 48 -T 120    
    
transaction type: ./test1.sql    
scaling factor: 1    
query mode: prepared    
number of clients: 48    
number of threads: 48    
duration: 120 s    
number of transactions actually processed: 76078076    
latency average = 0.076 ms    
latency stddev = 0.010 ms    
tps = 633977.716555 (including connections establishing)    
tps = 634041.588175 (excluding connections establishing)    
script statistics:    
 - statement latencies in milliseconds:    
         0.001  \set id random(1,100000000)    
         0.074  select * from test1 where id=:id;    
    
pgbench -M prepared -n -r -P 1 -f ./test3.sql -c 48 -j 48 -T 120    
    
transaction type: ./test3.sql    
scaling factor: 1    
query mode: prepared    
number of clients: 48    
number of threads: 48    
duration: 120 s    
number of transactions actually processed: 72746181    
latency average = 0.079 ms    
latency stddev = 0.019 ms    
tps = 606203.459638 (including connections establishing)    
tps = 606259.356671 (excluding connections establishing)    
script statistics:    
 - statement latencies in milliseconds:    
         0.001  \set id random(1,1000000000)    
         0.078  select * from test3 where id=:id;    

2、更新测试TPS

pgbench -M prepared -n -r -P 1 -f ./test.sql -c 48 -j 48 -T 120    
transaction type: ./test1.sql    
scaling factor: 1    
query mode: prepared    
number of clients: 48    
number of threads: 48    
duration: 120 s    
number of transactions actually processed: 27703709    
latency average = 0.208 ms    
latency stddev = 0.126 ms    
tps = 230828.616797 (including connections establishing)    
tps = 230853.344303 (excluding connections establishing)    
script statistics:    
 - statement latencies in milliseconds:    
         0.002  \set id random(1,100000000)    
         0.207  update test1 set crt_time=now() where id=:id;    
    
    
pgbench -M prepared -n -r -P 1 -f ./test1.sql -c 48 -j 48 -T 120    
    
    
transaction type: ./test1.sql    
scaling factor: 1    
query mode: prepared    
number of clients: 48    
number of threads: 48    
duration: 120 s    
number of transactions actually processed: 29387603    
latency average = 0.196 ms    
latency stddev = 0.110 ms    
tps = 244891.957430 (including connections establishing)    
tps = 244917.399306 (excluding connections establishing)    
script statistics:    
 - statement latencies in milliseconds:    
         0.001  \set id random(1,100000000)    
         0.195  update test1 set crt_time=now() where id=:id;    
    
pgbench -M prepared -n -r -P 1 -f ./test3.sql -c 48 -j 48 -T 120    
    
transaction type: ./test3.sql    
scaling factor: 1    
query mode: prepared    
number of clients: 48    
number of threads: 48    
duration: 120 s    
number of transactions actually processed: 28026501    
latency average = 0.205 ms    
latency stddev = 0.110 ms    
tps = 233533.801692 (including connections establishing)    
tps = 233554.689137 (excluding connections establishing)    
script statistics:    
 - statement latencies in milliseconds:    
         0.002  \set id random(1,100000000)    
         0.001  \set id1 random(1,1000000000)    
         0.203  with tmp as (select * from test3 where id=:id1)    

索引深度的差别:

postgres=# explain (analyze,verbose,timing,costs,buffers) select * from test where id=1;
                                                       QUERY PLAN                                                       
------------------------------------------------------------------------------------------------------------------------
 Index Scan using test_pkey on public.test  (cost=0.43..2.85 rows=1 width=44) (actual time=0.074..0.075 rows=1 loops=1)
   Output: id, info, crt_time
   Index Cond: (test.id = 1)
   Buffers: shared read=4
 Planning time: 0.215 ms
 Execution time: 0.102 ms
(6 rows)

postgres=# explain (analyze,verbose,timing,costs,buffers) select * from test1 where id=1;
                                                        QUERY PLAN                                                        
--------------------------------------------------------------------------------------------------------------------------
 Index Scan using test1_pkey on public.test1  (cost=0.57..2.98 rows=1 width=44) (actual time=0.094..0.094 rows=1 loops=1)
   Output: id, info, crt_time
   Index Cond: (test1.id = 1)
   Buffers: shared read=5
 Planning time: 0.217 ms
 Execution time: 0.119 ms
(6 rows)

postgres=# explain (analyze,verbose,timing,costs,buffers) select * from test3 where id=1;
                                                        QUERY PLAN                                                        
--------------------------------------------------------------------------------------------------------------------------
 Index Scan using test3_pkey on public.test3  (cost=0.57..2.99 rows=1 width=44) (actual time=0.054..0.055 rows=1 loops=1)
   Output: id, info, crt_time
   Index Cond: (test3.id = 1)
   Buffers: shared hit=5
 Planning time: 0.413 ms
 Execution time: 0.080 ms
(6 rows)

性能小结

数据量 写入吞吐 查询tps 更新tps
1000万 58万行/s 67万 23.1万
1亿 53.2万行/s 63.4万 24.5万
10亿 162.6万行/s 60.6万 23.4万

表分区建议

单表多大需要分区?

1、非常频繁更新的表(考虑到autovacuum的速度)

2亿

指表中频繁被更新的记录数在2亿以内,表本身的记录数可以更多。

2、更新、删除不频繁或毫无的表(考虑到设计rewrite的DDL,建索引,逻辑备份等的速度)

20亿(还需要考虑单行大小,直接影响DDL rewrite table的开销)

参考

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

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

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

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

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

相关实践学习
使用PolarDB和ECS搭建门户网站
本场景主要介绍如何基于PolarDB和ECS实现搭建门户网站。
阿里云数据库产品家族及特性
阿里云智能数据库产品团队一直致力于不断健全产品体系,提升产品性能,打磨产品功能,从而帮助客户实现更加极致的弹性能力、具备更强的扩展能力、并利用云设施进一步降低企业成本。以云原生+分布式为核心技术抓手,打造以自研的在线事务型(OLTP)数据库Polar DB和在线分析型(OLAP)数据库Analytic DB为代表的新一代企业级云原生数据库产品体系, 结合NoSQL数据库、数据库生态工具、云原生智能化数据库管控平台,为阿里巴巴经济体以及各个行业的企业客户和开发者提供从公共云到混合云再到私有云的完整解决方案,提供基于云基础设施进行数据从处理、到存储、再到计算与分析的一体化解决方案。本节课带你了解阿里云数据库产品家族及特性。
目录
相关文章
|
3月前
|
关系型数据库 MySQL 数据库
阿里云数据库RDS费用价格:MySQL、SQL Server、PostgreSQL和MariaDB引擎收费标准
阿里云RDS数据库支持MySQL、SQL Server、PostgreSQL、MariaDB,多种引擎优惠上线!MySQL倚天版88元/年,SQL Server 2核4G仅299元/年,PostgreSQL 227元/年起。高可用、可弹性伸缩,安全稳定。详情见官网活动页。
803 152
|
3月前
|
关系型数据库 MySQL 分布式数据库
阿里云PolarDB云原生数据库收费价格:MySQL和PostgreSQL详细介绍
阿里云PolarDB兼容MySQL、PostgreSQL及Oracle语法,支持集中式与分布式架构。标准版2核4G年费1116元起,企业版最高性能达4核16G,支持HTAP与多级高可用,广泛应用于金融、政务、互联网等领域,TCO成本降低50%。
|
3月前
|
关系型数据库 分布式数据库 数据库
阿里云数据库收费价格:MySQL、PostgreSQL、SQL Server和MariaDB引擎费用整理
阿里云数据库提供多种类型,包括关系型与NoSQL,主流如PolarDB、RDS MySQL/PostgreSQL、Redis等。价格低至21元/月起,支持按需付费与优惠套餐,适用于各类应用场景。
|
7月前
|
边缘计算 安全 5G
高精度时钟同步测试仪:构建全场景时间同步生态
在数字化转型中,时间同步至关重要。西安同步电子科技的 SYN5106 高精度时钟测试仪,具备±20ns 时差测量精度与 GPS/北斗双模授时能力,广泛应用于电力、通信、金融和科研领域。它解决变电站时间偏差、5G 基站同步误差及高频交易延迟等问题,助力智能电网、5G 网络和科研实验。产品便携可靠,支持多协议,满足国家安全要求,为各行业提供精准时间同步解决方案。未来将探索量子通信与深空探测等领域,持续推动技术创新。
|
6月前
|
SQL 关系型数据库 MySQL
Go语言数据库编程:使用 `database/sql` 与 MySQL/PostgreSQL
Go语言通过`database/sql`标准库提供统一数据库操作接口,支持MySQL、PostgreSQL等多种数据库。本文介绍了驱动安装、连接数据库、基本增删改查操作、预处理语句、事务处理及错误管理等内容,涵盖实际开发中常用的技巧与注意事项,适合快速掌握Go语言数据库编程基础。
506 62
|
3月前
|
人工智能 边缘计算 搜索推荐
AI产品测试学习路径全解析:从业务场景到代码实践
本文深入解析AI测试的核心技能与学习路径,涵盖业务理解、模型指标计算与性能测试三大阶段,助力掌握分类、推荐系统、计算机视觉等多场景测试方法,提升AI产品质量保障能力。
|
3月前
|
关系型数据库 MySQL 数据库
阿里云数据库RDS支持MySQL、SQL Server、PostgreSQL和MariaDB引擎
阿里云数据库RDS支持MySQL、SQL Server、PostgreSQL和MariaDB引擎,提供高性价比、稳定安全的云数据库服务,适用于多种行业与业务场景。
|
4月前
|
存储 关系型数据库 数据库
【赵渝强老师】PostgreSQL数据库的WAL日志与数据写入的过程
PostgreSQL中的WAL(预写日志)是保证数据完整性的关键技术。在数据修改前,系统会先将日志写入WAL,确保宕机时可通过日志恢复数据。它减少了磁盘I/O,提升了性能,并支持手动切换日志文件。WAL文件默认存储在pg_wal目录下,采用16进制命名规则。此外,PostgreSQL提供pg_waldump工具解析日志内容。
421 0
|
6月前
|
存储 关系型数据库 分布式数据库
【赵渝强老师】基于PostgreSQL的分布式数据库:Citus
Citus 是基于 PostgreSQL 的开源分布式数据库,采用 shared nothing 架构,具备良好的扩展性。它以插件形式集成,部署简单,适用于处理大规模数据和高并发场景。本文介绍了 Citus 的基础概念、安装配置步骤及其在单机环境下的集群搭建方法。
540 2
|
存储 关系型数据库 数据库
【赵渝强老师】PostgreSQL的数据库
PostgreSQL的逻辑存储结构涵盖数据库集群、数据库、表、索引、视图等对象,每个对象有唯一的oid标识。数据库集群包含多个数据库,每个数据库又包含多个模式,模式内含表、函数等。通过特定SQL命令可查看和管理这些数据库对象。
215 4

相关产品

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