如何从PostgreSQL

时间:2016-01-13 12:24:41

标签: postgresql partitioning

我在PostgreSQL中有一个不相交条件的大表分区。

类似的东西:

CREATE TABLE child_table_1(check(my_condition = '01')) INHERITS (parent_table);
// ...
CREATE TABLE child_table_20(check(my_condition = '20')) INHERITS (parent_table);

当我进行如下查询时:

EXPLAIN SELECT * FROM parent_table WHERE my_condition = '12' or my_condition = '14';

然后查询规划器按预期工作并显示:

Append  (cost=0.00..21424.65 rows=100 width=60)
  ->  Seq Scan on parent_table  (cost=0.00..0.00 rows=1 width=30)
        Filter: ((my_condition = '12'::bpchar) OR (my_condition= '14'::bpchar))
  ->  Seq Scan on child_table_12  (cost=0.00..14790.10 rows=50 width=20)
        Filter: ((my_condition = '12'::bpchar) OR (my_condition = '14'::bpchar))
  ->  Seq Scan on child_table_14 (cost=0.00..6634.55 rows=50 width=10)
        Filter: ((my_condition = '12'::bpchar) OR (my_condition = '14'::bpchar))

但是,如果我尝试更动态的方法,

EXPLAIN SELECT * FROM parent_table WHERE my_condition = ANY(array['12','04'])

它疯了,它试图扫描分区中的所有表。

我应该如何修改子表的条件以便识别它?

否则,如何在不为每个键写入条件的情况下动态查询多个子表。

1 个答案:

答案 0 :(得分:0)

如果父表是使用char(2)创建的,那么char varying(2)会出现问题,然后使用该分区:

https://www.postgresql.org/message-id/CA%2BbJJby9Bo6wkHMvLYhRRBt%2BiLApTuHCO_-Ljmx8BnZjyu8ncg%40mail.gmail.com