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

本文探讨了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

Greenplum 是一种基于 PostgreSQL 的大规模并行处理(MPP)架构的分布式数据库系统,广泛用于数据仓库和大数据分析场景。为了在 Greenplum 中实现高效的 SQL 查询,必须采用一系列特定的优化技术和策略,这些技术不仅包括传统的查询优化方法,还涵盖了分布式环境下的并行处理和数据分布策略。 ### 查询优化技术 Greenplum 使用基于代价的查询优化器(Cost-Based Optimizer, CBO),它会根据统计信息选择最优的执行计划。这种优化器可以决定如何最好地分布数据、如何并行执行查询以及如何选择连接顺序和连接方法等[^3]。此外,Greenplum 支持多种高级优化技术,如谓词下推(Predicate Pushdown)、连接下推(Join Pushdown)和分区剪枝(Partition Pruning),这些都可以显著提高查询性能。 ### 数据分布策略 Greenplum 中的数据分布策略对于查询性能至关重要。数据可以在多个段(Segment)之间分布,通常使用哈希分布或随机分布。合理选择分布键可以减少跨段通信的需求,从而降低网络开销并提高查询速度。此外,数据倾斜(Data Skew)是分布式数据库中常见的问题,它会导致某些段的负载远高于其他段,影响整体性能。通过监控和重新设计分布策略可以缓解这一问题。 ### 并行处理机制 Greenplum 的 MPP 架构允许查询在多个段上并行执行,从而加速大规模数据集的处理。为了充分利用并行处理能力,优化器会将查询分解为可以在各个段上独立执行的任务。此外,Greenplum 还支持并行排序、并行聚合和并行连接等操作,进一步提升了查询性能。 ### 物理存储优化 Greenplum 提供了多种物理存储优化选项,如列式存储(Column-Oriented Storage)、压缩(Compression)和分区(Partitioning)。列式存储特别适合于分析型查询,因为它只读取查询所需的列数据,减少了 I/O 开销。压缩可以减少磁盘空间占用并加快数据传输速度。分区则可以帮助优化器更有效地定位数据,避免全表扫描。 ### 示例:查询重写与索引优化 以下是一个简单的 SQL 查询重写示例,旨在减少不必要的数据处理: ```sql -- 原始查询 SELECT * FROM sales WHERE region = 'North America'; -- 优化后的查询 SELECT id, amount, region FROM sales WHERE region = 'North America'; ``` 在上述示例中,优化后的查询仅选择必要的列,而不是使用 `SELECT *`,这可以减少数据传输量并提高查询性能。 此外,Greenplum 支持多种索引类型,如 B-Tree 索引、位图索引和 GiST 索引。合理使用索引可以显著提高查询速度,尤其是在频繁查询的列上创建索引。然而,过多的索引会影响写入性能,因此需要权衡查询和更新的需求。 ### 总结 Greenplum 的 SQL 优化涉及多个层面,包括查询优化、数据分布、并行处理、物理存储以及索引设计。通过综合运用这些技术,可以有效提升查询性能并充分利用分布式架构的优势。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值