程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> 關於PHP編程 >> Greenplum創建表--分布鍵

Greenplum創建表--分布鍵

編輯:關於PHP編程

Greenplum創建表--分布鍵



Greenplum創建表--分布鍵

Greenplum是分布式系統,創建表時需要指定分布鍵(創建表需要CREATEDBA權限),目的在於將數據平均分布到各個segment。選擇分布鍵非常重要,選擇錯了會導致數據不唯一,更嚴重的是會造成SQL性能急劇下降。
Greenplum有兩種分布策略: 1、hash分布。
Greenplum默認使用hash分布策略。該策略可選一個或者多個列作為分布鍵(distribution key,簡稱DK)。分布鍵做hash算法來確認數據存放到對應的segment上。相同分布鍵值會hash到相同的segment上。表上最好有唯一鍵或者主鍵,這樣能保證數據均衡分不到各個segment上。語法,distributed by。 如果沒有主鍵或者唯一鍵,默認選擇第一列作為分布鍵。增加主鍵


2、隨機(randomly)分布。 數據會被隨機分不到segment上,相同記錄可能會存放在不同的segment上。隨機分布可以保證數據平均,但是Greenplum沒有跨節點的唯一鍵約束數據,所以無法保證數據唯一。基於唯一性和性能考慮,推薦使用hash分布,性能部分會另開一篇文檔詳細介紹。語法,distributed randomly。

一、hash分布鍵創建表,未指定分布列、分布類型,默認創建hash分布表,把第一列ID字段作為了分布鍵。

testDB=# create table t_hash(id int,name varchar(50)) distributed by (id);

CREATE TABLE

testDB=#

testDB=# \d t_hash

Table "public.t_hash"

Column | Type | Modifiers

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

id | integer |

name | character varying(50) |

Distributed by: (id)

添加主鍵後,主鍵升級為分布鍵替代了id列。

testDB=# alter table t_hash addprimary key (name);

NOTICE: updating distribution policy to match new primary key

NOTICE: ALTER TABLE / ADD PRIMARY KEY will create implicit index "t_hash_pkey" for table "t_hash"

ALTER TABLE

testDB=# \d t_hash

Table "public.t_hash"

Column | Type | Modifiers

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

id | integer |

name | character varying(50) | not null

Indexes:

"t_hash_pkey" PRIMARY KEY, btree (name)

Distributed by: (name)

驗證hash分布表可實現主鍵或者唯一鍵值的唯一性

testDB=# insert into t_hash values(1,'szlsd1');

INSERT 0 1

testDB=#

testDB=# insert into t_hash values(2,'szlsd1');

ERROR: duplicate key violates unique constraint "t_hash_pkey"(seg2 gp-s3:40000 pid=3855)


另外,主鍵列上依然能夠創建唯一鍵

testDB=# create unique index u_id on t_hash(name);

CREATE INDEX

testDB=#

testDB=#

testDB=# \d t_hash

Table "public.t_hash"

Column | Type | Modifiers

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

id | integer |

name | character varying(50) | not null

Indexes:

"t_hash_pkey" PRIMARY KEY, btree (name)

"u_id" UNIQUE, btree (name)

Distributed by: (name)



但是,非主鍵列無法單獨創建唯一索引,想創建的話必須包含多有分布鍵列

testDB=# create unique index uk_id on t_hash(id);

ERROR: UNIQUE indexmust contain all columns in the distribution keyof relation "t_hash"

testDB=# create unique index uk_id on t_hash(id,name);

CREATE INDEX

testDB=# \d t_hash

Table "public.t_hash"

Column | Type | Modifiers

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

id | integer |

name | character varying(50) | not null

Indexes:

"t_hash_pkey" PRIMARY KEY, btree (name)

"uk_id" UNIQUE, btree (id, name)

Distributed by: (name)


刪除主鍵後,原hash分布鍵依然不變。

testDB=# alter table t_hash drop constraint t_hash_pkey;

ALTER TABLE

testDB=# \d t_hash

Table "public.t_hash"

Column | Type | Modifiers

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

id | integer |

name | character varying(50) | not null

Distributed by: (name)


當分布鍵不是主鍵或者唯一鍵時,我們來驗證分布鍵的相同值落在一個segment的結論。下面的實驗,name列是分布鍵,我們插入相同的name值,可以看到7條記錄都落在了2號segment節點中。

testDB=#insert into t_hash values(1,'szlsd');

INSERT 0 1

testDB=#insert into t_hash values(2,'szlsd');

INSERT 0 1

testDB=#insert into t_hash values(3,'szlsd');

INSERT 0 1

testDB=#insert into t_hash values(4,'szlsd');

INSERT 0 1

testDB=#insert into t_hash values(5,'szlsd');

INSERT 0 1

testDB=#insert into t_hash values(6,'szlsd');

INSERT 0 1

testDB=#

testDB=#

testDB=# select gp_segment_id,count(*) from t_hash group by gp_segment_id;

gp_segment_id | count

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

2 |7

(1 row)



二、隨機分布鍵創建隨機分布表需加distributed randomly關鍵字,具體使用哪列作為分布鍵不得而知。

testDB=# create table t_random(id int ,name varchar(100))distributed randomly;

CREATE TABLE

testDB=#

testDB=#

testDB=# \d t_random

Table "public.t_random"

Column | Type | Modifiers

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

id | integer |

name | character varying(100) |

Distributed randomly



驗證主鍵/唯一鍵的唯一性,可以看到隨機分布表不能創建主鍵和唯一鍵

testDB=# alter table t_random add primary key (id,name);

ERROR: PRIMARY KEY and DISTRIBUTED RANDOMLY are incompatible

testDB=#

testDB=# create unique index uk_r_id on t_random(id);

ERROR: UNIQUE and DISTRIBUTED RANDOMLY are incompatible

testDB=#


從實驗中可以看出無法實現數據的唯一性。並且,數據插入隨機分布表,並不是輪詢插入,實驗中共有3個segment,但是在1號插入3條記錄,在2號segment節點插入2條記錄後,才在0號segment中插入數據。隨機分布表如何實現數據平均分配不得而知。這個實驗也驗證了隨機分布表的相同值分布在不同segment的結論。

testDB=# insert into t_random values(1,'szlsd3');

INSERT 0 1

testDB=# select gp_segment_id,count(*) from t_random group by gp_segment_id;

gp_segment_id | count

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

1 | 1

(1 row)

testDB=#

testDB=# insert into t_random values(1,'szlsd3');

INSERT 0 1

testDB=# select gp_segment_id,count(*) from t_random group by gp_segment_id;

gp_segment_id | count

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

2 | 1

1 | 1

(2 rows)

testDB=# insert into t_random values(1,'szlsd3');

INSERT 0 1

testDB=# select gp_segment_id,count(*) from t_random group by gp_segment_id;

gp_segment_id | count

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

2 | 1

1 | 2

(2 rows)

testDB=# insert into t_random values(1,'szlsd3');

INSERT 0 1

testDB=# select gp_segment_id,count(*) from t_random group by gp_segment_id;

gp_segment_id | count

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

2 | 2

1 | 2

(2 rows)

testDB=# insert into t_random values(1,'szlsd3');

INSERT 0 1

testDB=# select gp_segment_id,count(*) from t_random group by gp_segment_id;

gp_segment_id | count

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

2 | 2

1 | 3

(2 rows)

testDB=# insert into t_random values(1,'szlsd3');

INSERT 0 1

testDB=# select gp_segment_id,count(*) from t_random group by gp_segment_id;

gp_segment_id | count

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

2 |2

1 |3

0 | 1

(3 rows)



三、CTAS繼承原表分布鍵 Greenplum中有兩種CTAS語法,無論哪種語法,都默認繼承原表的分布鍵。但是,不會繼承表的一些特殊屬性,如主鍵、唯一鍵、APPENDONLY、COMPRESSTYPE(壓縮)等。

testDB=# \d t_hash;

Table "public.t_hash"

Column | Type | Modifiers

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

id | integer |

name | character varying(50) | not null

Indexes:

"t_hash_pkey" PRIMARY KEY, btree (name)

"uk_id" UNIQUE, btree (id, name)

Distributed by: (name)

testDB=#

testDB=#

testDB=# create table t_hash_1 as select * from t_hash;

NOTICE: Table doesn't have 'DISTRIBUTED BY' clause -- Using column(s) named 'name' 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.

SELECT 0

testDB=# \d t_hash_1

Table "public.t_hash_1"

Column | Type | Modifiers

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

id | integer |

name | character varying(50) |

Distributed by: (name)

testDB=#

testDB=# create table t_hash_2 (like t_hash);

NOTICE: Table doesn't have 'distributed by' clause, defaulting to distribution columns from LIKE table

CREATE TABLE

testDB=# \d t_hash_2

Table "public.t_hash_2"

Column | Type | Modifiers

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

id | integer |

name | character varying(50) | not null

Distributed by: (name)


如果CTAS創建表改變分布鍵,加上distributed by即可。

testDB=# create table t_hash_3 as select * from t_hash distributed by (id);

SELECT 0

testDB=#

testDB=# \d t_hash_3

Table "public.t_hash_3"

Column | Type | Modifiers

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

id | integer |

name | character varying(50) |

Distributed by: (id)

testDB=#

testDB=#

testDB=# create table t_hash_4 (like t_hash) distributed by (id);

CREATE TABLE

testDB=#

testDB=# \d t_hash4

Did not find any relation named "t_hash4".

testDB=# \d t_hash_4

Table "public.t_hash_4"

Column | Type | Modifiers

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

id | integer |

name | character varying(50) | not null

Distributed by: (id)


CTAS時,randomly隨機分布鍵要特別注意,一定要加上distributed randomly,不然原表是hash分布鍵,CTAS新表則是隨機分布鍵。

testDB=# \d t_random

Table "public.t_random"

Column | Type | Modifiers

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

id | integer |

name | character varying(100) |

Distributed randomly

testDB=#

testDB=# \d t_random_1

Table "public.t_random_1"

Column | Type | Modifiers

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

id | integer |

name | character varying(100) |

Distributed by: (id)


testDB=# create table t_random_2 as select * from t_randomdistributed randomly;

SELECT 7

testDB=#

testDB=# \d t_random_2

Table "public.t_random_2"

Column |Type| Modifiers

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

id| integer|

name| character varying(100) |

Distributed randomly



參考:《Greenplum企業應用實戰》《Greenplum4.2.2管理員指南》


轉載請注明:
十字螺絲釘
http://blog.chinaunix.net/uid/23284114.html
QQ:463725310
E-MAIL:houora#gmail.com(#請自行替換為@




  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved