OushuDB 数据库基本用法 (上)

本文介绍了如何启动和停止OushuDB集群,包括通过`hawq`命令启动、停止以及重启master和segment。此外,还展示了创建数据库和表的过程,以及使用psql工具进行查询和性能检查的方法,如使用` iming`和`explainanalyze`来查看查询执行时间和计划。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、启动 / 停止 OushuDB

启动 OushuDB 有两种方式,一种是通过”hawq start cluster” 命令来启动整个集群,包括 master 和 segment。启动哪些 segment 是由”/hawq-install-path/etc/slaves” 中包含的节点确定的。

source /usr/local/hawq/greenplum_path.sh # 设置 OushuDB 环境变量 hawq start cluster # 启动整个 OushuDB 集群

另外一种方式是分别启动 OushuDB master 和 segment。因为 OushuDB master 和 segment 是解耦合的,分别启动 master 和 segment 是可行的。

hawq start master # 启动 master,指的是启动本地 masterhawq start segment # 启动 segment,指的是启动本地 segment

重新启动或者停止 OushuDB 也有两种方式:

# 方式一 hawq restart cluster # 重启 OushuDB 集群 hawq stop cluster # 停止 OushuDB 集群# 方式二 hawq restart master # 重启本机的 OushuDB masterhawq restart segment # 重启本机的 OushuDB segmenthawq stop master # 停止本机 OushuDB masterhawq stop segment # 停止本机 OushuDB segment

启动 / 停止 Magma

OushuDB4.0 实现了单独起停 Magma 服务,具体命令如下:

# 方式一 OushuDB4.0 集群起停带 Magma 服务 [只有 hawq init|start|stop cluster 命令可以带 --with_magma 选项]hawq init cluster --with_magma # 启动 OushuDB 集群时,使用 --with_magma 选项,同时启动 Magma 服务,3.X 版本不支持。# 方式二 Magma 服务单独起停 magma start|stop|restart clustermagma start|stop|restart node

关于 OushuDB hawq 命令的详细用法,可以通过”hawq –help” 命令得到。

changlei:build ChangLei$ hawq --help

usage: hawq <command> [<object>] [options]

[--version]

The most commonly used hawq "commands" are:

start Start hawq service.

stop Stop hawq service.

init Init hawq service.

restart Restart hawq service.

activate Activate hawq standby master as master.

version Show hawq version information.

config Set hawq GUC values.

state Show hawq cluster status.

filespace Create hawq filespaces.

extract Extract table metadata into a YAML formatted file.

load Load data into hawq.

scp Copies files between multiple hosts at once.

ssh Provides ssh access to multiple hosts at once.

ssh-exkeys Exchanges SSH public keys between hosts.

check Verifies and validates HAWQ settings.

checkperf Verifies the baseline hardware performance of hosts.

register Register parquet files generated by other system into the corrsponding table in HAWQ

See 'hawq <command> help' for more information on a specific command.

2、创建数据库和表

本节通过使用 OushuDB 的命令行工具 psql 来说明如何创建基本数据库对象:database 和 table。因为 OushuDB 和 PostgreSQL 兼容,所以使用 OushuDB 的方式和使用 PostgresSQL 的方式基本相同,如果 OushuDB 的文档有些地方说明不清楚的话,用户也可以通过查阅 PostgresSQL 的帮助文档来了解更多关于 OushuDB 的信息。

下面这条命令使用 psql 连接 OushuDB 缺省安装的数据库 postgres,然后创建一个新的数据库 test,并在新的数据库中创建一个表 foo。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

changlei:build ChangLei$ psql -d postgres

psql (8.2.15)

Type "help" for help.

postgres=# create database test; # 创建数据库 test

CREATE DATABASE

postgres=# \c test # 连接进入 test 数据库

You are now connected to database "test" as user "ChangLei".

test=# create table foo (id int, name varchar); # 创建表 foo

CREATE TABLE

test=# \d # 显示当前数据库 test 中所有表

List of relations

Schema | Name | Type | Owner | Storage

--------+------+-------+----------+-------------

public | foo | table | ChangLei | append only

(1 row)

test=# insert into foo values(1, 'hawq'),(2, 'hdfs');

INSERT 0 2

test=# select * from foo; # 从表 foo 中选择数据

id | name

----+------

1 | hawq

2 | hdfs

(2 rows)

如果想删除表或者数据库的话可以使用 drop 语句。

test=# drop table foo;

DROP TABLE

test=# \d

No relations found.

test=# drop database test; # 因为现在在 test 数据库中,所以不能删除

ERROR: cannot drop the currently open database

test=# \c postgres # 首先连接到 postgres 数据库,然后删除 test 数据库

You are now connected to database "postgres" as user "ChangLei".

postgres=# drop database test;

DROP DATABASE

3、查看查询执行情况

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

使用 \timing 命令可以打印出查询执行的时间。

test=# \timing on

Timing is on.

test=# select * from foo; # 这时再执行 SQL 语句会给出语句执行时间。

id | name

----+------

1 | hawq

2 | hdfs

(2 rows)

Time: 16.369 ms

test=# \timing off # 关闭时间输出

Timing is off.

使用 explain 语句可以显示出查询计划。

test=# explain select count(*) from foo;

QUERY PLAN

----------------------------------------------------------------------------------

Aggregate (cost=1.07..1.08 rows=1 width=8)

-> Gather Motion 1:1 (slice1; segments: 1) (cost=1.03..1.06 rows=1 width=8)

-> Aggregate (cost=1.03..1.04 rows=1 width=8)

-> Append-only Scan on foo (cost=0.00..1.02 rows=2 width=0)

Settings: default_hash_table_bucket_number=6

(5 rows)

使用 explain analyze 可以显示出查询在具体执行时的状态,包括每一个操作符开始执行时间,以及结束时间,可以帮助用户找到查询的瓶颈,进而优化查询。关于查询计划以及 explain analyze 的执行结果的解释可以参考查询计划与查询执行章节。针对一个查询,可能会有无数个查询计划。得出优化的查询计划是查询优化器的功能。一个查询执行时间的长短与查询的计划有很大关系,所以熟悉查询计划以及具体查询的执行对查询优化有很大意义。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

test=# explain analyze select count(*) from foo;

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Aggregate (cost=1.07..1.08 rows=1 width=8)

Rows out: Avg 1.0 rows x 1 workers. Max/Last(seg-1:changlei/seg-1:changlei) 1/1 rows with 5.944/5.944 ms to end, start offset by 6.568/6.568 ms.

-> Gather Motion 1:1 (slice1; segments: 1) (cost=1.03..1.06 rows=1 width=8)

Rows out: Avg 1.0 rows x 1 workers at destination. Max/Last(seg-1:changlei/seg-1:changlei) 1/1 rows with 5.941/5.941 ms to first row, 5.942/5.942 ms to end, start offset by 6.569/6.569 ms.

-> Aggregate (cost=1.03..1.04 rows=1 width=8)

Rows out: Avg 1.0 rows x 1 workers. Max/Last(seg0:changlei/seg0:changlei) 1/1 rows with 5.035/5.035 ms to first row, 5.036/5.036 ms to end, start offset by 7.396/7.396 ms.

-> Append-only Scan on foo (cost=0.00..1.02 rows=2 width=0)

Rows out: Avg 2.0 rows x 1 workers. Max/Last(seg0:changlei/seg0:changlei) 2/2 rows with 5.011/5.011 ms to first row, 5.032/5.032 ms to end, start offset by 7.397/7.397 ms.

Slice statistics:

(slice0) Executor memory: 223K bytes.

(slice1) Executor memory: 279K bytes (seg0:changlei).

Statement statistics:

Memory used: 262144K bytes

Settings: default_hash_table_bucket_number=6

Dispatcher statistics:

executors used(total/cached/new connection): (1/1/0); dispatcher time(total/connection/dispatch data): (1.462 ms/0.000 ms/0.029 ms).

dispatch data time(max/min/avg): (0.029 ms/0.029 ms/0.029 ms); consume executor data time(max/min/avg): (0.012 ms/0.012 ms/0.012 ms); free executor time(max/min/avg): (0.000 ms/0.000 ms/0.000 ms).

Data locality statistics:

data locality ratio: 1.000; virtual segment number: 1; different host number: 1; virtual segment number per host(avg/min/max): (1/1/1); segment size(avg/min/max): (56.000 B/56 B/56 B); segment size with penalty(avg/min/max): (56.000 B/56 B/56 B); continuity(avg/min/max): (1.000/1.000/1.000); DFS metadatacache: 0.049 ms; resource allocation: 0.612 ms; datalocality calculation: 0.085 ms.

Total runtime: 13.398 ms

(20 rows)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值