Greenplum 点查询的优化(分布键)

简介:

标签

PostgreSQL , Greenplum , 分布式查询 , 分布式执行计划 , 点查


背景

Greenplum是分布式数据库,在建表时,可以指定随机分布、或按指定字段或多个字段进行分布。

因此在做点查时,Greenplum可以根据WHERE条件来判断是需要在所有节点查询,还是只需要到若干节点查询。

假设tbl按id分布,那么下面几种情况应该到哪个节点查询呢:

-- 到单个节点查询  
select * from tbl where id=?  
  
-- 到若干个节点查询  
select * from tbl where id in (?,?,?)  
  
-- 到所有节点查询  
select * from tbl where id >= ? and id < ?;  
  
-- 到所有节点查询  
select * from tbl where col1 ...;  

到多个节点查询和到一个节点查询,性能是不一样的。因此我们就有了优化的方法。

点查的优化与分布式QUERY

测试

1、构建测试表和数据

create table tbl2 (id int, c1 int) DISTRIBUTED BY (id);  
  
insert into tbl2 select id,id from generate_series(1,10000000) t(id);  

2、创建索引

create index idx_tbl2_1 on tbl2(id);  
create index idx_tbl2_2 on tbl2(c1);  

3、按分布键,点查,只需要查询一个节点。

postgres=# explain analyze select * from tbl2 where id=1;  
                                                QUERY PLAN                                                   
-----------------------------------------------------------------------------------------------------------  
 Gather Motion 1:1  (slice1; segments: 1)  (cost=0.00..200.28 rows=1 width=8)  
   Rows out:  1 rows at destination with 2.421 ms to first row, 2.422 ms to end, start offset by 0.212 ms.  
   ->  Index Scan using idx_tbl2_1 on tbl2  (cost=0.00..200.28 rows=1 width=8)  
         Index Cond: id = 1  
         Rows out:  1 rows with 0.017 ms to first row, 0.019 ms to end, start offset by 2.576 ms.  
 Slice statistics:  
   (slice0)    Executor memory: 226K bytes.  
   (slice1)    Executor memory: 172K bytes (seg42).  
 Statement statistics:  
   Memory used: 128000K bytes  
 Settings:  enable_bitmapscan=off; enable_seqscan=off; optimizer=off  
 Optimizer status: legacy query optimizer  
 Total runtime: 2.732 ms  
(13 rows)  

4、按非分布键查询,需要查询所有节点。

postgres=# explain analyze select * from tbl2 where c1=1;  
                                              QUERY PLAN                                                 
-------------------------------------------------------------------------------------------------------  
 Gather Motion 48:1  (slice1; segments: 48)  (cost=0.00..200.28 rows=1 width=8)  
   Rows out:  1 rows at destination with 58 ms to first row, 74 ms to end, start offset by 0.341 ms.  
   ->  Index Scan using idx_tbl2_2 on tbl2  (cost=0.00..200.28 rows=1 width=8)  
         Index Cond: c1 = 1  
         Rows out:  1 rows (seg42) with 0.294 ms to first row, 0.297 ms to end, start offset by 58 ms.  
 Slice statistics:  
   (slice0)    Executor memory: 226K bytes.  
   (slice1)    Executor memory: 172K bytes avg x 48 workers, 172K bytes max (seg0).  
 Statement statistics:  
   Memory used: 128000K bytes  
 Settings:  enable_bitmapscan=off; enable_seqscan=off; optimizer=off  
 Optimizer status: legacy query optimizer  
 Total runtime: 74.553 ms  
(13 rows)  

分析

在单个节点执行和在多个节点执行性能完全不一样对吧,从现象来看:

1、首先master需要生成分布式执行计划,耗费若干毫秒。

2、建立master与segment的连接,这一步可能是串行的,节点越多,建立连接耗时越久。

这个是在所有节点执行的,可以看到offset 58 ms,看起来是串行带来的问题。

Rows out:  1 rows (seg42) with 0.294 ms to first row, 0.297 ms to end, start offset by 58 ms.  

3、执行,执行实际上是并行的,而且也没有花多少时间。

优化

1、如果需要经常点查,并且条件字段分布均匀的话。可以选择这个字段作为分布键,提高查询效率。(并且这个表的UK或PK也是这个列,或者没有UK或者PK列的需求时。)

2、如果不能做到按查询条件字段进行分布,参数可以优化(效果不明显)。

gp_cached_segworkers_threshold

When a user starts a session with Greenplum Database and issues a query, the system creates groups or  
  
'gangs' of worker processes on each segment to do the work. After the work is done, the segment worker  
  
processes are destroyed except for a cached number which is set by this parameter. A lower setting  
  
conserves system resources on the segment hosts, but a higher setting may improve performance for  
  
power-users that want to issue many complex queries in a row.  

gp_interconnect_type

Sets the networking protocol used for Greenplum Database interconnect traffic. With the TCP protocol,  
  
Greenplum Database has an upper limit of 1000 segment instances - less than that if the query workload  
  
involves complex, multi-slice queries.  
  
The UDP protocol allows for greater interconnect scalability. Note that the Greenplum Database software  
  
does the additional packet verification and checking not performed by UDP, so reliability and performance  
  
is equivalent to TCP.  
  
UDPIFC specifies using UDP with flow control for interconnect traffic. Specify the interconnect flow control  
  
method with gp_interconnect_fc_method.  
  
  
Note: The Greenplum Database interconnect types TCP and UDP are deprecated. In the next  
  
major release, only the UDPIFC interconnect type will be supported by Greenplum Database.  

3、在master与segment之间使用连接池,也可能是一种优化方法。

https://www.linkedin.com/pulse/scaling-greenplum-pgbouncer-sandeep-katta-/?articleId=6128769027482402816

https://greenplum.org/docs/admin_guide/access_db/topics/pgbouncer.html

目录
相关文章
|
资源调度 监控 负载均衡
浅析PM2实用入门指南
PM2 是一个守护进程管理器,可以用它来管理你的node进程,负责所有正在运行的进程,并查看node进程的状态,也支持性能监控,负载均衡等功能。使用起来也是非常简单
2579 0
|
11月前
|
人工智能 搜索推荐 机器人
2026AI数字人国高技术企业介绍
截至2026年,AI数字人迈向“价值创造”新阶段:多模态融合大模型与AIGC,实现自然交互与内容自动生成;应用场景从展示延伸至电商、教育、政务等生产力领域;微调与RAG技术并行,平衡知识稳定性与实时性;用户体验更重情感化、个性化与跨端一致。工具平民化推动竞争转向提示词工程与运营优化,数字人正成为具备智能决策与持续学习能力的新型数字员工,加速产业落地。
|
9月前
|
存储 缓存 NoSQL
吃透 Redis 核心原理:内存模型、数据结构与持久化,从根上解决 90% 线上问题
本文深入剖析Redis三大核心基石:内存模型(含内存划分、碎片优化、过期与淘汰策略)、底层数据结构(String/Hash/List/Set/ZSet及扩展结构)和持久化机制(RDB/AOF/混合持久化),助开发者从set/get表层用法进阶到根因级问题解决。
886 2
|
9月前
|
Ubuntu Linux iOS开发
给计算机新生的操作系统全景指南
资深开发工程师致大一新生的操作系统入门指南:从Windows的易用、macOS的优雅,到Linux的开源力量,详解三大系统核心特性与开发者价值,助你夯实基础、拓宽视野。
|
11月前
|
算法 数据库
医疗器械唯一标识(UDI)HIBCC编码
医疗器械唯一标识(UDI)是全球通用的器械追溯系统,HIBC是由HIBCC制定的医疗专用条码标准,支持Code 128/39编码,具高可读性、抗污损、带校验位,广泛用于美国FDA合规产品,适用于高值耗材、植入物及医院资产管理,可通过HCreateLabelView工具生成符合规范的HIBCC条码,实现UDI数据自动采集与追溯。
1127 2
|
数据采集 存储 SQL
五问数据质量,一文讲透从根源到治理应用
在国家推动数据要素化改革背景下,数据已成为驱动新质生产力和产业变革的核心要素。本文聚焦企业在数据质量治理中的五大核心问题,解析数据质量问题来源、治理目标、责任划分、实施路径与评估方法,为企业构建可持续的数据质量保障机制提供实践指导。
|
机器学习/深度学习 算法 搜索推荐
数据不忽悠:如何用大数据预测未来?
数据不忽悠:如何用大数据预测未来?
885 13
|
SQL 存储 关系型数据库
第一篇:数据库基础与概念
这篇文档面向数据库初学者,系统介绍了数据库的基础概念、类型、管理工具及实践方法。内容涵盖数据库定义、应用场景(如电商、银行系统)、数据库管理系统(DBMS)的功能与常见系统(MySQL、PostgreSQL等),以及关系型与非关系型数据库的区别。同时,文章详细解析了基本术语(表、记录、字段、主键、外键)和ER图设计,并提供了实践建议,包括创建简单数据库、学习SQL语言、使用管理工具等。最后推荐了学习资源和书籍,鼓励读者通过实际项目巩固知识,逐步掌握数据库的核心技能。
1883 11
|
SQL 存储 容灾
关于主从延迟,一篇文章给你讲明白了!
在实际的生产环境中,由单台MySQL作为独立的数据库是完全不能满足实际需求的,无论是在安全性,高可用性以及高并发等各个方面 因此,一般来说都是通过集群主从复制(Master-Slave)的方式来同步数据,再通过读写分离(MySQL-Proxy)来提升数据库的并发负载能力进行部署与实施总结MySQL主从集群带来的作用是:提高数据库负载能力,主库执行读写任务(增删改),备库仅做查询。提高系统读写性能、可扩展性和高可用性。数据备份与容灾,备库在异地,主库不存在了,备库可以立即接管,无须恢复时间。用于记录数据库执行的写入性操作(不包括查询)信息,以二进制的形式保存在磁盘中。可以简单理解为记录的就是sq
2965 0
|
存储 监控 数据可视化
在Linux中,如何查看系统日志?
在Linux中,如何查看系统日志?