Greenplum5.9.0简单使用

本文介绍了Greenplum5.9.0的环境准备和使用案例,包括登录数据库、创建数据库和表,展示数据库和表,插入和查询数据,以及创建分区表和外部表的操作。特别强调了在Greenplum中建表时指定分布键的重要性,以及Explain命令用于查询执行计划的功能。
摘要由CSDN通过智能技术生成

一、环境准备

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中创建表语句与普通的数据库的建表语句区别不大,不同之处主要包括以下几点:

  1. 在greenplum中建表时需要指定表的分布键(DISTRIBUTED BY)。
  2. 如果表需要用某个字段分区,可以使用PARTITION BY将表建成分区表。
  3. 使用like操作创建一样表结构的表。

Greenplum有两种数据分布策略:

  1. Hash分布。通过hash值路由到特定的Segment,语法为DISTRIBUTED BY(..),如果不指定分布键,则默认第一个字段为分布键。
  2. 随机分布。也叫平均分布,在执行关联等操作时性能较差,语法为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  | Enc

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值