Question

I am building a system that could be distributed over several machine after some time.
so I have gone through all the sequencing methods serial, uuid, comb ...

I found that no one of them is perfect for me.

And I came up with this:

  • give every machine a key ex: 3et6
  • give every table a key/alias ex: us // for users table
  • normal sequence
  • concatenate them to generate a PK

so PK for a user row would look like this:

3et6us1001

Is there anything I miss that could make this way don't work?

Was it helpful?

Solution

Your plan sounds fine. The only problem (not necessarily a big one) is that a lot of DBMSs are optimised to use integer keys. There are two things you could do to take advantage of this:

  1. Assign each machine a unique Id (1 to 999) and make the PK sequence
    • 1000 + machine id
  2. Just use a sequence as the PK. The only time you need worry about collisions is when you combine data across multiple machines; when you do that you can add the source machine name/id to the select to distinguish them. (This won't work, of course, if data from different machines is copied to all others.)

I don't think the table name is necessary, though. It's standard practice not to put it into a key: you know the table it came from because you know the query; adding this redundant data is over-engineering.

OTHER TIPS

You can use a compound key, with integers:

primary key(machine_id, record_id);

Some benefits over single column key is that you don't need to know the upper limit of nr machines. If your RDBMS support it, this also lends itself well to hash partitioning. This is benificial from an administrative point of view because you can move around large chunks of data very fast. Also, from a performance point of view, this can help with join-performance due to partition-wise joins.

A compound key will require more storage. If table compression is supported the additional storage will be minimal since you will have high degree of duplication per block. Especially so if you go with the hash-partitioning scheme.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top