Mysql – generate unique primary key for all clients from the local database and NOT the server

distributed-databasesinnodbMySQLprimary-key

I'm using MySQL Innodb database to work as a distributed database among many places and also each place will work offline but at the end all the data will be centralized in one server,
I want to generate a unique primary key for registration table that will be unique for all clients, first I thought about int auto_increment but then I realized that this will cause conflict between each client as all of them will generate a new auto_increment from their local database and not from server so in this case all will start from 1 and increment for each new register

now I'm thinking of using a CHAR or VARCHAR to put a prefix for each registration in client's local database, or is there another way to do so?

Best Answer

If there are no natural way of identifying a registration, you may want to have a look at GUID:

https://en.wikipedia.org/wiki/Globally_unique_identifier

The upside is that there are plenty of libraries dealing with them. The downside is that they occupy space which may lead to higher B-Tree indexes, i.e. additional I/O. More read I/O is also required because GUID's are evenly distributed, when you read a page the likelihood that you get a related row is low which causes additional pages to be read.

Another option is to add another attribute representing the host and use this in combination with your sequence as an identifier.

primary key(host_id, seq_id)

Make sure that no two host uses the same host_id.

Yet a third option is to partition the seq_id for different hosts. I.e. host 1 uses seq_id 1..n, host 2 uses seq_id n+1..2n and so forth. Not an alternative for the heart of fainted ;-)