GBase 8a将Hash分布表转成随机分布表的方法

本文介绍几种,将GBase 8a的Hash分布表转换为随机分布表的方法。

环境

一张id为分布列的hash分布表。

gbase> create table t_hash(id int, name varchar(100))distributed by('id');
Query OK, 0 rows affected (Elapsed: 00:00:00.66)

gbase> show create table t_hash;
+--------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table  | Create Table                                                                                                                                                                   |
+--------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| t_hash | CREATE TABLE "t_hash" (
  "id" int(11) DEFAULT NULL,
  "name" varchar(100) DEFAULT NULL
) ENGINE=EXPRESS DISTRIBUTED BY('id') DEFAULT CHARSET=utf8 TABLESPACE='sys_tablespace' |
+--------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (Elapsed: 00:00:00.00)

重建表

该方法适合所有版本,无任何风险。

新建一张表,表结构与hash分布表一样,但去掉hash分布列

create table t_nohash(id int, name varchar(100));
insert into t_nohash select * from t_hash;
rename table t_hash to t_hash_BAK;
reanem table t_nohash to t_hash ;

如上的rename操作确认所有业务正常后,可以考虑删除t_hash_BAK表以便节省空间。

删除hash列关键字

该方法在9.5的手册里出现,86未注意,但预计能用。

gbase> drop distributed column on t_hash;
Query OK, 0 rows affected (Elapsed: 00:00:00.02)

gbase> show create table t_hash;
+--------+-----------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table  | Create Table                                                                                                                                              |
+--------+-----------------------------------------------------------------------------------------------------------------------------------------------------------+
| t_hash | CREATE TABLE "t_hash" (
  "id" int(11) DEFAULT NULL,
  "name" varchar(100) DEFAULT NULL
) ENGINE=EXPRESS DEFAULT CHARSET=utf8 TABLESPACE='sys_tablespace' |
+--------+-----------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (Elapsed: 00:00:00.00)