CREATE TABLE range_list (a int,b timestamp) PARTITION BY RANGE (b); CREATE TABLE range_pa1 PARTITION OF range_list FOR VALUES from ('2000-01-01') TO ('2010-01-01') PARTITION BY RANGE (a); CREATE TABLE range_pa2 PARTITION OF range_list FOR VALUES from ('2010-01-01') TO ('2020-01-01') PARTITION BY RANGE (a); CREATE TABLE range_list_2000_2010_1_10 PARTITION OF range_pa1 FOR VALUES from (1) TO (10); CREATE TABLE range_list_2000_2010_10_20 PARTITION OF range_pa1 FOR VALUES from (11) TO (20); CREATE TABLE range_list_2010_2020_1_10 PARTITION OF range_pa2 FOR VALUES from (1) TO (10); CREATE TABLE range_list_2010_2020_10_20 PARTITION OF range_pa2 FOR VALUES from (11) TO (20); exampledb=> d List of relations Schema | Name | Type | Owner --------+--------------------+----------+---------- public | range_list1_10 | table | dbuser public | range_list1_20 | table | dbuser public | range_list2_10 | table | dbuser public | range_list2_20 | table | dbuser exampledb=> insert into range_list values(1,'2005-01-05 5:05'); INSERT 0 1 exampledb=> explain select * from range_list where a=1 and b='2005-01-05 5:05'; QUERY PLAN ---------------------------------------------------------------------------------------- Append (cost=0.00..40.60 rows=1 width=12) -> Seq Scan on range_list1_10 (cost=0.00..40.60 rows=1 width=12) Filter: ((a = 1) AND (b = '2005-01-05 05:05:00'::timestamp without time zone)) (3 rows) Time: 1.425 ms
一般开发中,大多需要用户ID和时间区间进行查询数据,可以使用此方法进行二位分区。