HTAP数据库 PostgreSQL 场景与性能测试之 25 - (OLTP) IN , EXISTS 查询

本文涉及的产品
云原生数据库 PolarDB 分布式版,标准版 2核8GB
云数据库 RDS SQL Server,基础系列 2核4GB
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
简介:

标签

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

场景 - IN , EXISTS 查询 (OLTP)

1、背景

in 查询,多用在多个输入值的匹配场景。

实际上PostgreSQL支持很多种多个输入值匹配的语法。

1、in (...)

2、in (table or subquery or srf)

3、= any (array)

4、exists (select 1 from (values (),(),...) as t(id) where x.?=t.id)

5、=? or =? or =? or .....

他们的执行计划分别如下,(in (values....) or = any (array)最佳) :

postgres=# explain select * from a where id in (1,2,3,4,5);  
                           QUERY PLAN                              
-----------------------------------------------------------------  
 Index Scan using a_pkey on a  (cost=0.43..9.46 rows=5 width=45)  
   Index Cond: (id = ANY ('{1,2,3,4,5}'::integer[]))  
(2 rows)  
  
postgres=# explain select * from a where id = any (array[1,2,3,4,5]);  
                           QUERY PLAN                              
-----------------------------------------------------------------  
 Index Scan using a_pkey on a  (cost=0.43..9.46 rows=5 width=45)  
   Index Cond: (id = ANY ('{1,2,3,4,5}'::integer[]))  
(2 rows)  
  
postgres=# explain select * from a where id = any (array(select generate_series(1,10)));  
                            QUERY PLAN                               
-------------------------------------------------------------------  
 Index Scan using a_pkey on a  (cost=5.45..22.74 rows=10 width=45)  
   Index Cond: (id = ANY ($0))  
   InitPlan 1 (returns $0)  
     ->  ProjectSet  (cost=0.00..5.02 rows=1000 width=4)  
           ->  Result  (cost=0.00..0.01 rows=1 width=0)  
(5 rows)  
  
postgres=# explain select * from a where id = any (array(select id from (values (1),(2),(3),(4),(5)) t (id)));  
                             QUERY PLAN                                
---------------------------------------------------------------------  
 Index Scan using a_pkey on a  (cost=0.50..17.79 rows=10 width=45)  
   Index Cond: (id = ANY ($0))  
   InitPlan 1 (returns $0)  
     ->  Values Scan on "*VALUES*"  (cost=0.00..0.06 rows=5 width=4)  
(4 rows)  
  
postgres=# explain select * from a where id in (select id from (values (1),(2),(3),(4),(5)) t (id));  
                               QUERY PLAN                                  
-------------------------------------------------------------------------  
 Nested Loop  (cost=0.51..14.39 rows=5 width=45)  
   ->  HashAggregate  (cost=0.07..0.12 rows=5 width=4)  
         Group Key: "*VALUES*".column1  
         ->  Values Scan on "*VALUES*"  (cost=0.00..0.06 rows=5 width=4)  
   ->  Index Scan using a_pkey on a  (cost=0.43..2.85 rows=1 width=45)  
         Index Cond: (id = "*VALUES*".column1)  
(6 rows)  
  
postgres=# explain select * from a where exists (select 1 from (values (1),(2),(3),(4),(5)) t (id) where t.id=a.id);  
                               QUERY PLAN                                  
-------------------------------------------------------------------------  
 Nested Loop  (cost=0.51..14.39 rows=5 width=45)  
   ->  HashAggregate  (cost=0.07..0.12 rows=5 width=4)  
         Group Key: "*VALUES*".column1  
         ->  Values Scan on "*VALUES*"  (cost=0.00..0.06 rows=5 width=4)  
   ->  Index Scan using a_pkey on a  (cost=0.43..2.85 rows=1 width=45)  
         Index Cond: (id = "*VALUES*".column1)  
(6 rows)  
  
postgres=# explain select * from a where id=1 or id=2 or id=3 or id=4 or id =5;  
                                 QUERY PLAN                                   
----------------------------------------------------------------------------  
 Bitmap Heap Scan on a  (cost=8.22..14.32 rows=5 width=45)  
   Recheck Cond: ((id = 1) OR (id = 2) OR (id = 3) OR (id = 4) OR (id = 5))  
   ->  BitmapOr  (cost=8.22..8.22 rows=5 width=0)  
         ->  Bitmap Index Scan on a_pkey  (cost=0.00..1.64 rows=1 width=0)  
               Index Cond: (id = 1)  
         ->  Bitmap Index Scan on a_pkey  (cost=0.00..1.64 rows=1 width=0)  
               Index Cond: (id = 2)  
         ->  Bitmap Index Scan on a_pkey  (cost=0.00..1.64 rows=1 width=0)  
               Index Cond: (id = 3)  
         ->  Bitmap Index Scan on a_pkey  (cost=0.00..1.64 rows=1 width=0)  
               Index Cond: (id = 4)  
         ->  Bitmap Index Scan on a_pkey  (cost=0.00..1.64 rows=1 width=0)  
               Index Cond: (id = 5)  
(13 rows)  

2、设计

1亿记录,查询匹配多个输入值的性能。分别输入1,10,100,1000,10000,100000,1000000个值作为匹配条件。

1、in (...)

2、in (table or subquery or srf)

3、= any (array)

4、exists (select 1 from (values (),(),...) as t(id) where x.?=t.id)

5、=? or =? or =? or .....

3、准备测试表

create table t_in_test (id int primary key, info text, crt_time timestamp);  

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

5、准备测试数据

insert into t_in_test select generate_series(1,100000000), md5(random()::text), clock_timestamp();  

6、准备测试脚本

1、in (...)

1,10,100,1000,10000,100000,1000000 个输入值的测试性能

do language plpgsql $$  
declare  
  arr text;  
  ts timestamp := clock_timestamp();  
  mx int8;  
begin  
  for i in 0..6 loop  
    mx := (1*(10^i))::int8;  
    select string_agg((random()*100000)::int::text, ',') into arr from generate_series(1, mx);  
    ts := clock_timestamp();  
    execute 'select * from t_in_test where id in ('||arr||')';  
    raise notice '%: %', mx, clock_timestamp()-ts;  
  end loop;  
end;  
$$ ;  

2、in (table or subquery or srf)

1,10,100,1000,10000,100000,1000000 个输入值的测试性能

do language plpgsql $$  
declare  
  arr text;  
  ts timestamp := clock_timestamp();  
  mx int8;  
begin  
  for i in 0..6 loop  
    mx := (1*(10^i))::int8;  
    ts := clock_timestamp();  
    perform * from t_in_test where id in ( select (random()*100000)::int from generate_series(1, mx) );  
    raise notice '%: %', mx, clock_timestamp()-ts;  
  end loop;  
end;  
$$ ;  

3、= any (array)

1,10,100,1000,10000,100000,1000000 个输入值的测试性能

do language plpgsql $$  
declare  
  arr int[];  
  ts timestamp := clock_timestamp();  
  mx int8;  
begin  
  for i in 0..6 loop  
    mx := (1*(10^i))::int8;  
    select array_agg((random()*100000)::int) into arr from generate_series(1, mx);  
    ts := clock_timestamp();  
    perform * from t_in_test where id = any ( arr );  
    raise notice '%: %', mx, clock_timestamp()-ts;  
  end loop;  
end;  
$$ ;  

4、exists (select 1 from (values (),(),...) as t(id) where x.?=t.id)

1,10,100,1000,10000,100000,1000000 个输入值的测试性能

do language plpgsql $$  
declare  
  ts timestamp := clock_timestamp();  
  mx int8;  
begin  
  for i in 0..6 loop  
    mx := (1*(10^i))::int8;  
    ts := clock_timestamp();  
    perform * from t_in_test where exists ( select 1 from ( select (random()*100000)::int id from generate_series(1,mx) ) t where t_in_test.id=t.id );  
    raise notice '%: %', mx, clock_timestamp()-ts;  
  end loop;  
end;  
$$ ;  

5、压测

匹配1 ~ 100个输入值,求聚合。高并发。

vi test.sql  
  
\set x random(1,100)  
select count(*) from t_in_test where id = any(array(select (random()*100000000)::int from generate_series(1,:x)));  

压测

CONNECTS=56    
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    

7、测试

1、in (...)

1,10,100,1000,10000,100000,1000000 个输入值的测试性能

do language plpgsql $$  
declare  
  arr text;  
  ts timestamp := clock_timestamp();  
  mx int8;  
begin  
  for i in 0..6 loop  
    mx := (1*(10^i))::int8;  
    select string_agg((random()*100000)::int::text, ',') into arr from generate_series(1, mx);  
    ts := clock_timestamp();  
    execute 'select * from t_in_test where id in ('||arr||')';  
    raise notice '%: %', mx, clock_timestamp()-ts;  
  end loop;  
end;  
$$ ;  
NOTICE:  1: 00:00:00.000256  
NOTICE:  10: 00:00:00.000173  
NOTICE:  100: 00:00:00.000772  
NOTICE:  1000: 00:00:00.004445  
NOTICE:  10000: 00:00:00.024073  
NOTICE:  100000: 00:00:00.195439  
NOTICE:  1000000: 00:00:01.638982  
DO  

2、in (table or subquery or srf)

1,10,100,1000,10000,100000,1000000 个输入值的测试性能

do language plpgsql $$  
declare  
  arr text;  
  ts timestamp := clock_timestamp();  
  mx int8;  
begin  
  for i in 0..6 loop  
    mx := (1*(10^i))::int8;  
    ts := clock_timestamp();  
    perform * from t_in_test where id in ( select (random()*100000)::int from generate_series(1, mx) );  
    raise notice '%: %', mx, clock_timestamp()-ts;  
  end loop;  
end;  
$$ ;  
NOTICE:  1: 00:00:00.00044  
NOTICE:  10: 00:00:00.000244  
NOTICE:  100: 00:00:00.000788  
NOTICE:  1000: 00:00:00.004455  
NOTICE:  10000: 00:00:00.028793  
NOTICE:  100000: 00:00:00.187841  
NOTICE:  1000000: 00:00:00.583744  
DO  

3、= any (array)

1,10,100,1000,10000,100000,1000000 个输入值的测试性能

do language plpgsql $$  
declare  
  arr int[];  
  ts timestamp := clock_timestamp();  
  mx int8;  
begin  
  for i in 0..6 loop  
    mx := (1*(10^i))::int8;  
    select array_agg((random()*100000)::int) into arr from generate_series(1, mx);  
    ts := clock_timestamp();  
    perform * from t_in_test where id = any ( arr );  
    raise notice '%: %', mx, clock_timestamp()-ts;  
  end loop;  
end;  
$$ ;  
NOTICE:  1: 00:00:00.000216  
NOTICE:  10: 00:00:00.000151  
NOTICE:  100: 00:00:00.000654  
NOTICE:  1000: 00:00:00.00399  
NOTICE:  10000: 00:00:00.021216  
NOTICE:  100000: 00:00:00.106335  
NOTICE:  1000000: 00:00:00.386113  
DO  

4、exists (select 1 from (values (),(),...) as t(id) where x.?=t.id)

1,10,100,1000,10000,100000,1000000 个输入值的测试性能

do language plpgsql $$  
declare  
  ts timestamp := clock_timestamp();  
  mx int8;  
begin  
  for i in 0..6 loop  
    mx := (1*(10^i))::int8;  
    ts := clock_timestamp();  
    perform * from t_in_test where exists ( select 1 from ( select (random()*100000)::int id from generate_series(1,mx) ) t where t_in_test.id=t.id );  
    raise notice '%: %', mx, clock_timestamp()-ts;  
  end loop;  
end;  
$$ ;  
NOTICE:  1: 00:00:00.000458
NOTICE:  10: 00:00:00.000224
NOTICE:  100: 00:00:00.000687
NOTICE:  1000: 00:00:00.003916
NOTICE:  10000: 00:00:00.02734
NOTICE:  100000: 00:00:00.187671
NOTICE:  1000000: 00:00:00.570389
DO

5、匹配1 ~ 100个输入值,求聚合。高并发。

transaction type: ./test.sql  
scaling factor: 1  
query mode: prepared  
number of clients: 56  
number of threads: 56  
duration: 300 s  
number of transactions actually processed: 13913566  
latency average = 1.207 ms  
latency stddev = 0.840 ms  
tps = 46378.142149 (including connections establishing)  
tps = 46384.723274 (excluding connections establishing)  
script statistics:  
 - statement latencies in milliseconds:  
         0.002  \set x random(1,100)  
         1.207  select count(*) from t_in_test where id = any(array(select (random()*100000000)::int from generate_series(1,:x)));  

TPS: 46384

5、匹配1 ~ 100个输入值,求聚合。高并发。

平均响应时间: 1.207 毫秒

5、匹配1 ~ 100个输入值,求聚合。高并发。

1到100万个输入值的响应时间

1亿条记录,匹配100万个输入值( = any (array) ),只需要386毫秒。

NOTICE:  1: 00:00:00.000216  
NOTICE:  10: 00:00:00.000151  
NOTICE:  100: 00:00:00.000654  
NOTICE:  1000: 00:00:00.00399  
NOTICE:  10000: 00:00:00.021216  
NOTICE:  100000: 00:00:00.106335  
NOTICE:  1000000: 00:00:00.386113  

参考

《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数据库、数据库生态工具、云原生智能化数据库管控平台,为阿里巴巴经济体以及各个行业的企业客户和开发者提供从公共云到混合云再到私有云的完整解决方案,提供基于云基础设施进行数据从处理、到存储、再到计算与分析的一体化解决方案。本节课带你了解阿里云数据库产品家族及特性。
目录
相关文章
|
7月前
|
关系型数据库 分布式数据库 数据库
一库多能:阿里云PolarDB三大引擎、四种输出形态,覆盖企业数据库全场景
PolarDB是阿里云自研的新一代云原生数据库,提供极致弹性、高性能和海量存储。它包含三个版本:PolarDB-M(兼容MySQL)、PolarDB-PG(兼容PostgreSQL及Oracle语法)和PolarDB-X(分布式数据库)。支持公有云、专有云、DBStack及轻量版等多种形态,满足不同场景需求。2021年,PolarDB-PG与PolarDB-X开源,内核与商业版一致,推动国产数据库生态发展,同时兼容主流国产操作系统与芯片,获得权威安全认证。
|
5月前
|
存储 监控 关系型数据库
B-tree不是万能药:PostgreSQL索引失效的7种高频场景与破解方案
在PostgreSQL优化实践中,B-tree索引虽承担了80%以上的查询加速任务,但因多种原因可能导致索引失效,引发性能骤降。本文深入剖析7种高频失效场景,包括隐式类型转换、函数包裹列、前导通配符等,并通过实战案例揭示问题本质,提供生产验证的解决方案。同时,总结索引使用决策矩阵与关键原则,助你让索引真正发挥作用。
328 0
|
5月前
|
SQL 存储 关系型数据库
PostgreSQL窗口函数避坑指南:如何让复杂分析查询提速300%?
本文基于真实企业级案例,深入剖析PostgreSQL窗口函数的执行原理与性能陷阱,提供8大优化策略。通过定制索引、分区裁剪、内存调优及并行处理等手段,将分钟级查询压缩至秒级响应。结合CTE分阶段计算与物化视图技术,解决海量数据分析中的瓶颈问题。某金融客户实践表明,风险分析查询从47秒降至0.8秒,效率提升5800%。文章附带代码均在PostgreSQL 15中验证,助您高效优化SQL性能。
277 0
|
6月前
|
边缘计算 安全 5G
高精度时钟同步测试仪:构建全场景时间同步生态
在数字化转型中,时间同步至关重要。西安同步电子科技的 SYN5106 高精度时钟测试仪,具备±20ns 时差测量精度与 GPS/北斗双模授时能力,广泛应用于电力、通信、金融和科研领域。它解决变电站时间偏差、5G 基站同步误差及高频交易延迟等问题,助力智能电网、5G 网络和科研实验。产品便携可靠,支持多协议,满足国家安全要求,为各行业提供精准时间同步解决方案。未来将探索量子通信与深空探测等领域,持续推动技术创新。
|
2月前
|
缓存 Java 应用服务中间件
Spring Boot配置优化:Tomcat+数据库+缓存+日志,全场景教程
本文详解Spring Boot十大核心配置优化技巧,涵盖Tomcat连接池、数据库连接池、Jackson时区、日志管理、缓存策略、异步线程池等关键配置,结合代码示例与通俗解释,助你轻松掌握高并发场景下的性能调优方法,适用于实际项目落地。
443 4
|
2月前
|
人工智能 边缘计算 搜索推荐
AI产品测试学习路径全解析:从业务场景到代码实践
本文深入解析AI测试的核心技能与学习路径,涵盖业务理解、模型指标计算与性能测试三大阶段,助力掌握分类、推荐系统、计算机视觉等多场景测试方法,提升AI产品质量保障能力。
|
存储 Oracle 关系型数据库
Oracle数据库的应用场景有哪些?
【10月更文挑战第15天】Oracle数据库的应用场景有哪些?
1004 64
|
5月前
|
安全 关系型数据库 数据库
瀚高股份与 Anolis OS 完成适配,龙蜥获数据库场景高性能与稳定性认证
Anolis OS 能够为用户提供更加高效、安全的数据处理与管理体验。
|
6月前
|
编解码 5G 定位技术
时间频率综合测试仪优势所在及场景使用介绍
时间频率综合测试仪是保障系统精准运行的关键设备。以西安同步电子科技有限公司的SYN5104型为例,它集时间标准源、时差测量和频率测试于一体,功能涵盖时间准确度、频率分析、PPS/B码/E1/PTP/NTP测试等,精度达30ns。其便携设计适用于研发、标定、现场检测,支持电力系统校准、通信同步测试及科研校准等场景,助力高精度时频同步与产品质量提升。文章版权归西安同步电子科技有限公司所有,严禁侵权。
|
9月前
|
SQL 关系型数据库 OLAP
云原生数据仓库AnalyticDB PostgreSQL同一个SQL可以实现向量索引、全文索引GIN、普通索引BTREE混合查询,简化业务实现逻辑、提升查询性能
本文档介绍了如何在AnalyticDB for PostgreSQL中创建表、向量索引及混合检索的实现步骤。主要内容包括:创建`articles`表并设置向量存储格式,创建ANN向量索引,为表增加`username`和`time`列,建立BTREE索引和GIN全文检索索引,并展示了查询结果。参考文档提供了详细的SQL语句和配置说明。
239 2

相关产品

  • 云原生数据库 PolarDB
  • 云数据库 RDS PostgreSQL 版
  • 推荐镜像

    更多