首页 > 解决方案 > 具有数千个分区的 Postgres 表

问题描述

我有一个 postgres 表(在 postgres12 中),它应该在不久的将来有数千个分区(至少 200k)。

这是我创建父表的方式:

create table if not exists content (
    key varchar(20) not NULL,
    value json not null default '[]'::json
) PARTITION BY LIST(key)

然后添加任何给定的子表,例如:

create table if not exists content_123 PARTITION OF content for VALUES in ('123');

我还在子表顶部添加了一个索引以便快速访问(因为我将直接访问子表):

create index if not exists content_123_idx on content_123 using btree(key) 

这是我的问题:我过去从未在 postgres 表中管理过这么多分区,所以我只是想知道做我正在做的事情有什么缺点吗?另外,(如上所述)我不会直接从父表中查询,而是直接从各个子表中读取。

标签: postgresqlpartitioning

解决方案


有了这些表定义,索引就完全没用了。

有 200000 个分区,查询计划会变得非常慢,每条 SQL 语句都需要非常多的锁和打开文件。这不会很好地工作。

将几个键集中到一个分区中(然后索引可能有意义)。


推荐阅读