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

利用PolarDB-PG HTAP加速TPC-H(二)

PolarDB农夫山泉 2023-09-20
111

PolarDB PostgreSQL版(以下简称 PolarDB-PG)是一款阿里云自主研发的企业级数据库产品,采用计算存储分离架构,兼容 PostgreSQL 与 Oracle。PolarDB-PG 的存储与计算能力均可横向扩展,具有高可靠、高可用、弹性扩展等企业级数据库特性。同时,PolarDB-PG 具有大规模并行计算能力,可以应对 OLTP 与 OLAP 混合负载;还具有时空、向量、搜索、图谱等多模创新特性,可以满足企业对数据处理日新月异的新需求。
本节介绍利用PolarDB PostgreSQL版的HTAP能力加速TPC-H的执行的最佳实践案例。本案例将基于单机本地存储来运行。

导入数据

通过 psql 导入 TPC-H 数据。

注意:一直要在 tpch-dbgen/ 目录下执行

# 创建表 psql -f dss.ddl # 进入 psql 命令行 psql
复制
# 导入数据 \copy nation from 'nation.tbl' DELIMITER '|'; \copy region from 'region.tbl' DELIMITER '|'; \copy supplier from 'supplier.tbl' DELIMITER '|'; \copy part from 'part.tbl' DELIMITER '|'; \copy partsupp from 'partsupp.tbl' DELIMITER '|'; \copy customer from 'customer.tbl' DELIMITER '|'; \copy orders from 'orders.tbl' DELIMITER '|'; \copy lineitem from 'lineitem.tbl' DELIMITER '|';
复制

数据导入完成后,逐行执行如下命令,对创建的表设置最大并行度:

# 对需要 PX 查询的表设置最大并行度(若不设置则不会进入 PX 查询) alter table nation set (px_workers = 100); alter table region set (px_workers = 100); alter table supplier set (px_workers = 100); alter table part set (px_workers = 100); alter table partsupp set (px_workers = 100); alter table customer set (px_workers = 100); alter table orders set (px_workers = 100); alter table lineitem set (px_workers = 100);
复制

执行单机并行查询

模拟数据导入到 PolarDB for PostgreSQL 后,我们先执行单机并行查询,观测一下查询速度。

  1. psql 连入后,执行如下命令,开启计时。
\timing
复制
  1. 通过 max_parallel_workers_per_gather 参数设置单机并行度:
set max_parallel_workers_per_gather=2; -- 并行度设置为 2
复制
  1. 执行如下命令,查看执行计划。
\i queries/q18.explain.sql
复制

可以看到如图所示的 2 个并行度的并行计划:

                                                                        QUERY PLAN
-----------------------------------------------------------------------------------------------------------------------------------------------------------------
Limit  (cost=9364138.51..9364141.51 rows=100 width=71)
->  GroupAggregate  (cost=9364138.51..9380736.94 rows=553281 width=71)
      Group Key: orders.o_totalprice, orders.o_orderdate, customer.c_name, customer.c_custkey, orders.o_orderkey
      ->  Sort  (cost=9364138.51..9365521.71 rows=553281 width=44)
            Sort Key: orders.o_totalprice DESC, orders.o_orderdate, customer.c_name, customer.c_custkey, orders.o_orderkey
            ->  Hash Join  (cost=6752588.87..9294341.50 rows=553281 width=44)
                  Hash Cond: (lineitem.l_orderkey = orders.o_orderkey)
                  ->  Seq Scan on lineitem  (cost=0.00..1724338.96 rows=59979696 width=9)
                  ->  Hash  (cost=6749642.22..6749642.22 rows=138372 width=43)
                        ->  Hash Join  (cost=6110531.76..6749642.22 rows=138372 width=43)
                              Hash Cond: (orders.o_custkey = customer.c_custkey)
                              ->  Hash Join  (cost=6032162.96..6658785.84 rows=138372 width=24)
                                    Hash Cond: (orders.o_orderkey = lineitem_1.l_orderkey)
                                    ->  Seq Scan on orders  (cost=0.00..410917.44 rows=15000544 width=20)
                                    ->  Hash  (cost=6029892.31..6029892.31 rows=138372 width=4)
                                          ->  Finalize GroupAggregate  (cost=5727599.96..6028508.59 rows=138372 width=4)
                                                Group Key: lineitem_1.l_orderkey
                                                Filter: (sum(lineitem_1.l_quantity) > '313'::numeric)
                                                ->  Gather Merge  (cost=5727599.96..6016055.08 rows=830234 width=36)
                                                      Workers Planned: 2
                                                      ->  Partial GroupAggregate  (cost=5726599.94..5919225.45 rows=415117 width=36)
                                                            Group Key: lineitem_1.l_orderkey
                                                            ->  Sort  (cost=5726599.94..5789078.79 rows=24991540 width=9)
                                                                  Sort Key: lineitem_1.l_orderkey
                                                                  ->  Parallel Seq Scan on lineitem lineitem_1  (cost=0.00..1374457.40 rows=24991540 width=9)
                              ->  Hash  (cost=50827.80..50827.80 rows=1500080 width=23)
                                    ->  Seq Scan on customer  (cost=0.00..50827.80 rows=1500080 width=23)
(27 rows)
复制
  1. 执行 SQL,可以看到部分结果(按 q 不查看全部结果)和运行时间,运行时间为 1 分 23 秒:
\i queries/q18.sql
复制

image.png

如果单机并行度太高,可能会出现如下的错误提示:pq: could not resize shared memory segment "/PostgreSQL.2058389254" to 12615680 bytes: No space left on device。原因是 Docker 预设的 shared memory 空间不足,可以参考 该链接 设置参数并重启 Docker 进行解决。

「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

文章被以下合辑收录

评论

目录
  • 导入数据
  • 执行单机并行查询