2017-12-28 10 views
0

Apache Spark hat die Option, in mehrere Dateien mit dem BucketBy-Befehl aufzuteilen. Wenn ich zum Beispiel 100 Millionen Benutzer-IDs habe, kann ich die Tabelle in 32 verschiedene Dateien aufteilen, wobei eine Art Hash-Algorithmus verwendet wird, um die Daten zwischen Dateien zu verteilen und zu suchen.Postgresql-Partition in eine feste Reihe von Dateien von ID

Kann Postgrees Tabellen irgendwie in eine feste Anzahl von Partitionen aufteilen? Wenn es kein natives Feature ist, kann es immer noch ausgeführt werden, zum Beispiel einen Hash erzeugen; Hash in eine Zahl umwandeln; nimm modulo% 32 als Paritätsbereich.

+1

Lesen Sie über [Tabelle Partitionierung.] (Https://www.postgresql.org/docs/current/static/ddl-partitioning.html) – klin

Antwort

1

Beispiel mit Modulo:

eine kurze Partitionen Setup:

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 

Prüfung:

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

und Demo von Partitionen arbeiten:

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) 
Verwandte Themen