2

I have a table A that has a couple of other tables (B, C) attached to it (many rows in B and C for one row in A).

I'm having performance problems with A, so I thought I would partition it using a date_deleted column (NULL/NOT NULL) into 2 tables A_active and A_deleted.

Data flow is as follows:

  1. Records are created at a rate 10k per day, inserted into A_active
  2. After 2 months records are moved to A_deleted
  3. Records are never un-deleted
  4. Nothing is ever hard deleted, currently there are 1.3M rows

I have written a full script to split my A into A_active and A_deleted, create triggers for inserts and updates, but I'm at a loss about the foreign keys.

Should I create B_active and B_deleted? The triggers will get super complex.

Should I create 2 FKs from B? One to A_active, one to A_deleted? Triggers will also be very complex.

Should I scrape this idea altogether? I kind of like the idea of having a dedicated table for historic records. It will simplify my selects.

asked Jan 23, 2016 at 13:41

1 Answer 1

2

A table of 1.3M rows is pretty small. It would be unusual for a table of that size to benefit from partitioning.

Have you looked at the queries which are slow with EXPLAIN (ANALYZE, BUFFERS) to see what the problems are?

When partitioning works well, it is because it works hand-in-glove with your use cases. So there can't really be generic partitioning advice. You would have to tell us much more about the types of queries you run and what your performance expectations of them are, and what the current execution plans look like.

answered Jan 24, 2016 at 0:26
2
  • Thank you. Currently the slowest are counts, which is really just a HW limit it seems. If I split the table, I could do these counts as approximate, which are fast. In the end I decided to normalize and split them naturally into 2 tables. Commented Jan 24, 2016 at 10:43
  • You can also get approximate count using partial indexes or functional indexes and looking up pg_class.reltuples for the index. It is an unintuitive use of such indexes, but it can work very well. Commented Jan 24, 2016 at 21:22

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.