Greenplum create a table -- distributed key _ PHP Tutorial

Source: Internet
Author: User
Greenplum creates a table-distribution key. Greenplum creates a table -- distribution key Greenplum is a distributed system. when creating a table, you must specify the distribution key (CREATEDBA permission is required for table creation) Greenplum create table -- distribution key

Greenplum is a distributed system. you must specify the distribution key when creating a table (the CREATEDBA permission is required for creating a table) to evenly distribute data to segments. It is very important to select the distribution key. if the distribution key is selected incorrectly, data is not unique. more seriously, SQL performance decreases sharply.


Greenplum has two distribution policies:

1. hash distribution.

Greenplum uses the hash distribution policy by default. This policy selects one or more columns as the distribution key (DK ). The distribution key uses the hash algorithm to confirm that the data is stored on the corresponding segment. Keys with the same distribution will be hashed to the same segment. It is better to have a unique key or primary key on the table to ensure that the data is not evenly distributed to each segment. Syntax: distributed.

If no primary key or unique key exists, the first column is selected as the distribution key by default. Add primary key



2. random (randomly) distribution.

The data will be randomly divided into segments, and the same records may be stored in different segments. Random distribution can ensure data average, but Greenplum does not have a unique key constraint across nodes, so it cannot guarantee data uniqueness. Based on uniqueness and performance considerations, hash distribution is recommended. the performance section will provide a detailed introduction to this document. Syntax: distributed randomly.

I. hash distribution key

Create a table without specifying the distribution column and distribution type. by default, create a hash distribution table and use the ID field of the first column as the distribution key.

testDB=# create table t_hash(id int,name varchar(50)) distributed by (id);CREATE TABLEtestDB=#  testDB=# \d t_hash           Table "public.t_hash" Column |         Type          | Modifiers --------+-----------------------+----------- id     | integer               |  name   | character varying(50) | Distributed by: (id)

After the primary key is added, the primary key is upgraded to the distribution key instead of the id column.

testDB=# alter table t_hash add primary key (name);NOTICE:  updating distribution policy to match new primary keyNOTICE:  ALTER TABLE / ADD PRIMARY KEY will create implicit index "t_hash_pkey" for table "t_hash" ALTER TABLEtestDB=# \d t_hash           Table "public.t_hash" Column |         Type          | Modifiers --------+-----------------------+----------- id     | integer               |  name   | character varying(50) | not nullIndexes:    "t_hash_pkey" PRIMARY KEY, btree (name)Distributed by: (name)

Verify that the hash distribution table can realize the uniqueness of the primary key or unique key value

testDB=# insert into t_hash values(1,'szlsd1');INSERT 0 1testDB=#testDB=# insert into t_hash values(2,'szlsd1');ERROR:  duplicate key violates unique constraint "t_hash_pkey"(seg2 gp-s3:40000 pid=3855)

In addition, the primary key column can still create a unique key

testDB=# create unique index u_id on t_hash(name);CREATE INDEXtestDB=#testDB=#testDB=# \d t_hash           Table "public.t_hash" Column |         Type          | Modifiers--------+-----------------------+----------- id     | integer               | name   | character varying(50) | not nullIndexes:    "t_hash_pkey" PRIMARY KEY, btree (name)    "u_id" UNIQUE, btree (name)Distributed by: (name)

However, you cannot create a unique index for a non-primary key column. to create a unique index, you must include multiple distributed key columns.

testDB=#  create unique index uk_id on t_hash(id);ERROR:  UNIQUE index must contain all columns in the distribution key of relation "t_hash"testDB=#  create unique index uk_id on t_hash(id,name);CREATE INDEXtestDB=# \d t_hash           Table "public.t_hash" Column |         Type          | Modifiers--------+-----------------------+----------- id     | integer               | name   | character varying(50) | not nullIndexes:    "t_hash_pkey" PRIMARY KEY, btree (name)    "uk_id" UNIQUE, btree (id, name)Distributed by: (name)

After the primary key is deleted, the original hash distribution key remains unchanged.

testDB=# alter table t_hash drop constraint t_hash_pkey;ALTER TABLEtestDB=# \d t_hash           Table "public.t_hash" Column |         Type          | Modifiers--------+-----------------------+----------- id     | integer               | name   | character varying(50) | not nullDistributed by: (name)

When the distribution key is not a primary key or a unique key, we verify that the same value of the distribution key falls into a segment.

In the following experiment, the name column is the distribution key. we insert the same name value. we can see that all seven records are in the segment 2 node.

testDB=#  insert into t_hash values(1,'szlsd');INSERT 0 1testDB=#  insert into t_hash values(2,'szlsd');INSERT 0 1testDB=#  insert into t_hash values(3,'szlsd');INSERT 0 1testDB=#  insert into t_hash values(4,'szlsd');INSERT 0 1testDB=#  insert into t_hash values(5,'szlsd');INSERT 0 1testDB=#  insert into t_hash values(6,'szlsd');INSERT 0 1testDB=#testDB=#testDB=# select gp_segment_id,count(*) from t_hash group by gp_segment_id;  gp_segment_id | count---------------+-------             2 |     7(1 row)

II. Random distribution key

The distributed randomly keyword is required to create a random distribution table. it is not clear which column is used as the distribution key.

testDB=# create table t_random(id int ,name varchar(100)) distributed randomly;CREATE TABLEtestDB=#testDB=#testDB=# \d t_random           Table "public.t_random" Column |          Type          | Modifiers--------+------------------------+----------- id     | integer                | name   | character varying(100) |Distributed randomly

Verify the uniqueness of the primary key/unique key. we can see that the primary key and unique key cannot be created in the random distribution table.

testDB=# alter table t_random add primary key (id,name);ERROR:  PRIMARY KEY and DISTRIBUTED RANDOMLY are incompatibletestDB=#testDB=# create unique index uk_r_id on t_random(id);ERROR:  UNIQUE and DISTRIBUTED RANDOMLY are incompatibletestDB=#

We can see from the experiment that data uniqueness cannot be achieved. In addition, the random distribution table for data insertion is not a round-robin insertion. There are three segments in the experiment, but three records are inserted on the first part. After two records are inserted on the second part, to insert data in segment 0. It is unclear how the random distribution table achieves the average data distribution. This experiment also verifies the conclusion that the same value of the random distribution table is distributed across different segments.

testDB=# insert into t_random values(1,'szlsd3');INSERT 0 1testDB=# 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 1testDB=# 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 1testDB=# 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 1testDB=# 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 1testDB=# 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 1testDB=# 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)

3. CTAS inherits the distribution key of the original table

Greenplum has two CTAS syntaxes. either of them inherits the distribution key of the original table by default. However, it does not inherit some special attributes of the table, such as the primary key, unique key, APPENDONLY, and COMPRESSTYPE (compression.

testDB=# \d t_hash;           Table "public.t_hash" Column |         Type          | Modifiers--------+-----------------------+----------- id     | integer               | name   | character varying(50) | not nullIndexes:    "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 0testDB=# \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 tableCREATE TABLEtestDB=# \d t_hash_2          Table "public.t_hash_2" Column |         Type          | Modifiers--------+-----------------------+----------- id     | integer               | name   | character varying(50) | not nullDistributed by: (name)

If the CTAS creates a table and changes the distribution key, add distributed.

testDB=# create table t_hash_3 as select * from t_hash distributed by (id);SELECT 0testDB=#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 TABLEtestDB=#testDB=# \d t_hash4Did 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 nullDistributed by: (id)

When using CTAS, pay special attention to the random distribution key of randomly. you must add distributed randomly. Otherwise, the original table is the hash distribution key, and the new CTAS table is the random distribution key.

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_random distributed randomly;SELECT 7testDB=#testDB=# \d t_random_2          Table "public.t_random_2" Column |          Type          | Modifiers--------+------------------------+----------- id     | integer                | name   | character varying(100) |Distributed randomly

Refer:

Greenplum enterprise application practices

Greenplum4.2.2 Administrator Guide

The above is the Greenplum table creation-distribution key _ PHP Tutorial content, for more information, please follow the PHP Chinese network (www.php1.cn )!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.