如何从Postgresql中的表中的现有数据分片

问题描述

我有一个大表inter,其中包含500亿行。每行由两列组成,它们实际上都是其他两个表的ID的外键(只是关系,数据库中未设置外键约束)。

我的表结构如下:

create table test_1(
    id integer primary key,content varchar(300),content_len integer
);

create index test_1_id_len on test_1(id,content_len);

--this has 1.5 billion rows.
-- example row1: 1,'alskfnla',8
-- example row2: 1,'asdgaagder',10
-- example row3: 1,'dsafnlakdsvn',12

create table test_2(
    id integer primary key,split_str char(3)
);

--this has 60,000 rows.
-- example row1: 1,'abc'
-- example row2: 2,'abb'

create table inter(
    id_1 integer,-- id of test_1
    id_2 integer     -- id of test_2 
);

create index test_index_1 on inter(id_1);
create index test_index_2 on inter(id_2);
create index test_index_1_2 on inter(id_1,id_2);

--this has 50 billion rows.
-- example row1: 1,2
-- example row2: 1,3
-- example row3: 1,4

此外,我需要进行一些查询,例如

select * 
from inter 
  inner join test_1 on(test_1.id = inter.id_1) 
where id_2 in (1,2,3,4,5,67,8,9,10) 
  and test_1.content_len = 30 
order by id_2;

我想对表进行分片的原因是我无法在两列上创建索引(事务没有结束一周,并且它占用了全部虚拟内存)。

因此,我正在考虑按表中的一列对表进行分片。该列的值约为60,000,从1到60,000。我想将该表分片为60,000个子表。我进行了一些搜索,但是大多数文章都是通过触发器来完成的,由于数据已经在表中,因此无法应用于我的情况。有谁知道该怎么做,非常感谢!

ENV:redhat,RAM 180GB,PostgreSQL 11.0

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)