一、环境准备
GP集群环境情况如下图所示,master实现容错,配置standby master。两个节点为segment节点,其中每个几点配置两个segment,未配置mirror segment。

二、使用案例
2.1登录数据库
登录Greenplum数据库,默认的数据库为postgres
| [gpadmin@sparkproject1 ~]$ psql -d testDB psql (8.2.15) Type "help" for help.
testDB=# |
2.2创建数据库
| testDB=# create database mydb; CREATE DATABASE |
2.3创建表
Greenplum中创建表语句与普通的数据库的建表语句区别不大,不同之处主要包括以下几点:
- 在greenplum中建表时需要指定表的分布键(DISTRIBUTED BY)。
- 如果表需要用某个字段分区,可以使用PARTITION BY将表建成分区表。
- 使用like操作创建一样表结构的表。
Greenplum有两种数据分布策略:
- Hash分布。通过hash值路由到特定的Segment,语法为DISTRIBUTED BY(..),如果不指定分布键,则默认第一个字段为分布键。
- 随机分布。也叫平均分布,在执行关联等操作时性能较差,语法为Distributed randomly
以下两种方式结果一样,都是以id作为分布键。
| testDB=# create table test1(id int,name varchar(128)); NOTICE: Table doesn't have 'DISTRIBUTED BY' clause -- Using column named 'id' as the Greenplum Database data distribution key for this table. HINT: The 'DISTRIBUTED BY' clause determines the distribution of data. Make sure column(s) chosen are the optimal data distribution key to minimize skew. CREATE TABLE |
| testDB=# create table test2(id int,name varchar(128)) distributed by (id); CREATE TABLE |
描述表信息
| testDB=# \d test1 Table "public.test1" Column | Type | Modifiers --------+------------------------+----------- id | integer | name | character varying(128) | Distributed by: (id) |
下面的创建表语句采用了随机分布的方式
| testDB=# create table test3(id int,name varchar(128)) distributed randomly; CREATE TABLE |
使用like创建表
| testDB=# create table test3_like(like test3); NOTICE: Table doesn't have 'distributed by' clause, defaulting to distribution columns from LIKE table CREATE TABLE |
2.4显示数据库
|
testDB=# \l List of databases Name | Owner | Encoding | Access privileges -----------+---------+----------+--------------------- mydb | gpadmin | UTF8 | postgres | gpadmin | UTF8 | template0 | gpadmin | UTF8 | =c/gpadmin : gpadmin=CTc/gpadmin template1 | gpadmin | UTF8 | =c/gpadmin &n |

本文介绍了Greenplum5.9.0的环境准备和使用案例,包括登录数据库、创建数据库和表,展示数据库和表,插入和查询数据,以及创建分区表和外部表的操作。特别强调了在Greenplum中建表时指定分布键的重要性,以及Explain命令用于查询执行计划的功能。
最低0.47元/天 解锁文章
644

被折叠的 条评论
为什么被折叠?



