2017-12-28 1 views
0

Apache Spark에는 bucketBy 명령으로 여러 파일로 분할 할 수있는 옵션이 있습니다. 예를 들어, 1 억 명의 사용자 ID가있는 경우 테이블을 32 개의 다른 파일로 분할 할 수 있습니다. 여기서는 파일간에 데이터를 배포하고 조회하는 데 사용되는 해싱 알고리즘이 있습니다.ID로 고정 된 파일 세트로 Postgresql 파티션

Postgres는 테이블을 고정 된 수의 파티션으로 분할 할 수 있습니까? 네이티브 기능이 아니라면 해시를 생성 할 수 있습니다. 해시를 숫자로 바꾼다. % 32를 parititon 범위로 취하십시오. 모듈과

+1

[Table Partitioning.] (https://www.postgresql.org/docs/current/static/ddl-partitioning.html)에 대한 정보를 읽으십시오. – klin

답변

1

예 :

짧은 분할 설정 :

db=# create table p(i int); 
CREATE TABLE 
db=# create table p1 (check (mod(i,3)=0)) inherits (p); 
CREATE TABLE 
db=# create table p2 (check (mod(i,3)=1)) inherits (p); 
CREATE TABLE 
db=# create table p3 (check (mod(i,3)=2)) inherits (p); 
CREATE TABLE 
db=# create rule pir3 AS ON insert to p where mod(i,3) = 2 do instead insert into p3 values (new.*); 
CREATE RULE 
db=# create rule pir2 AS ON insert to p where mod(i,3) = 1 do instead insert into p2 values (new.*); 
CREATE RULE 
db=# create rule pir1 AS ON insert to p where mod(i,3) = 0 do instead insert into p1 values (new.*); 
CREATE RULE 

검사 :

db=# insert into p values (1),(2),(3),(4),(5); 
INSERT 0 0 
db=# select * from p; 
i 
--- 
3 
1 
4 
2 
5 
(5 rows) 

db=# select * from p1; 
i 
--- 
3 
(1 row) 

db=# select * from p2; 
i 
--- 
1 
4 
(2 rows) 

db=# select * from p3; 
i 
--- 
2 
5 
(2 rows) 

https://www.postgresql.org/docs/current/static/tutorial-inheritance.html https://www.postgresql.org/docs/current/static/ddl-partitioning.html

및 격벽 작업 데모 :

db=# explain analyze select * from p where mod(i,3) = 2; 
              QUERY PLAN 
---------------------------------------------------------------------------------------------------- 
Append (cost=0.00..48.25 rows=14 width=4) (actual time=0.013..0.015 rows=2 loops=1) 
    -> Seq Scan on p (cost=0.00..0.00 rows=1 width=4) (actual time=0.004..0.004 rows=0 loops=1) 
     Filter: (mod(i, 3) = 2) 
    -> Seq Scan on p3 (cost=0.00..48.25 rows=13 width=4) (actual time=0.009..0.011 rows=2 loops=1) 
     Filter: (mod(i, 3) = 2) 
Planning time: 0.203 ms 
Execution time: 0.052 ms 
(7 rows) 
관련 문제