views:

112

answers:

2

Hi, I've been thinking of implementing this system, but can't help but feel there's a catch somewhere. One of the points of using GUID over incrementing int is that, in the future, if you were to merge databases together, you wouldn't have any clashes over the primary key/identifier. However, my approach is to set the increment size to X where X is the number of servers I'll most likely have in the future. Then, on each server, have the seed be an increment over the seed number on the previous server. That way, during merging, there would be no clashes with the primary key. Is this a safe, normal method or have I gone mental :)? Thanks

A: 

What scares me here is your use of "most likely". You're assuming on the future here, and typically that's not a good thing to do with things like this. Why not use a GUID?

What if you add one extra server over what you thought you'd have? I could see things getting really complicated really quickly.

David Morton
Hi David, thanks for the reply. I'm using GUIDs on a couple of tables, but for the sake of size and search performance, I decided against using it was everything. From what I've heard, the fact that GUIDs cannot be ordered makes querying slower.
keyboardP
+1  A: 

In multi-master SQL replication, you typically have primary keys defined as:

  • GUIDs
  • int's with a increment size > number of installs
  • int's with a fixed offset

The downside of GUIDs is they can be harder to read and take up slightly more space. However, it allows you to scale to n instances.

Integers are a bit easier to deal with. They also have the advantage of being able to easily tell which server created a record. The downside is you must either predict the maximum number of databases which might be merged, or guess the maximum number of rows a single instance might insert.

An example of a fixed offset is: site A starts at 0, site B starts at 1,000,000 and site C starts at 2,000,000. This scheme works fine until one site inserts one million rows. This scheme might work well for cars at car dealerships, where it's unlikely that any one dealer would ever sell more than 1,000,000 cars, and you might have hundreds of dealerships over the life of the application.

brianegge
Hi Brianegget, thanks for the reply. I managed to follow up to "guess the maximum number of rows a single instance might insert". What do you mean by that?
keyboardP