简体   繁体   中英

Postgres table partitioning with star schema

I have a schema with one table with the majority of data, customer , and three other tables with foreign key references to customer.entry_id which is a BIGSERIAL field. The three other tables are called location , devices and urls where we store various data related to a specific entry in the customer table.

I want to partition the customer table into monthly child tables, and have that part worked out; customer will stay as-is, each month will have a table customer_YYYY_MM that inherits from the master table with the right CHECK constraint and indexes will be created on each individual child table. Data will be moved to the correct child tables while the master table stays empty.

My question is about the other three tables, as I want to partition them as well. However, they have no date information (at all), only the reference to the primary key from the master table. How can I setup the constraints on these tables? Is it even meaningful or possible without date information?

My application logic knows where to insert all the data (it's fairly trivial), but I expect to be able to do simple SELECT queries without specifying which child tables to get it from. So this should work as you would expect from non-partitioned tables:

SELECT l.*
FROM customer c
JOIN location l USING entry_id
WHERE c.date_field > '2015-01-01'

I would partition them by the reference key. The foreign key is used in join conditions and is not usually subject to change so it fulfills the following important points:

  • Partition by the information that is used mostly in the WHERE clauses of the queries or other parts where partitioning can be used to filter out tables that don't need to be scanned. As one guide puts it:

The objective when defining partitions should be to allow as many queries as possible to fetch data from as few partitions as possible - ideally one.

  • Partition by information that is not going to be changed so that rows don't constantly need to be thrown from one subtable to another

This all depends of the size of the tables too of course. If the sizes stay small then there is no need to partition.

Read more about partitioning here .

Use views:

create view customer as
select * from customer_jan_15 union all
select * from customer_feb_15 union all
select * from customer_mar_15;

create view location as
select * from location_jan_15 union all
select * from location_feb_15 union all
select * from location_mar_15;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM