In a previous financial payments related database I'd worked on, it just had one large transactions table. We would insert scheduled transactions for the future, and when that time came, we'd execute them, and update that row to status completed.

I've kept wondering why they didn't have a separate 'completed transactions' table? Is that bad practice?

  • 1
    Depends on your DBMS. Some, eg Postgresql and Oracle support partitioned tables which allows you to keep your data together and separate at the same time. Postgresql also supports partial indexes, so that only a very small subset of rows need be indexed. – Colin 't Hart Sep 17 '14 at 9:32

There is one entity - financial transaction, that can be in a various states. Completed, scheduled, postponed, rolled back etc. State is the property of the entity, only part of the whole, like the field is a part of the row.

When the state is just a field of the table it is possible to create DB structure and code that are invariant regardless of how many states are possible. In the opposite case each new state produce the new table and lot of code should be rewritten to reflect the changes.

| improve this answer | |

You are right to wonder. When you have an entity that can take two states (let's call them A and B) where one transitions directly into the other, but not the other way around, you should generally implement it in two tables.

The reasons splitting into two tables is a good idea is:

  1. If A always transitions to B, then you will have a lot more B than A. This creates skew, which increases the likelihood of bad query plans
  2. When you have transitioned to B, it is often the case that the row is now readonly. Whereas rows in state A are malleable. By having the readonly rows in a separate table, you can improve your backup strategy quite a bit.
  3. With two tables, you can have separate indexing strategies for the two states. Often, you can get away with fewer indexes on the table that holds state B. This saves space.

An alternative approach to having two tables is to partition the table, with one partition for A and another for B. Some database allow separate indexing strategies on different partitions.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.