sql - Java Sequential UUID -


i need use uuid primary keys of of tables since need globally unique across several client databases , of data need merged @ point.

using varchar(36) column , java's version 4 uuid random generator solves issue, problem uuids aren't sequential bring issue indexes , inserts on large tables (1+ million rows).

i'm trying generate sequential yet random uuid, replacing significant bits uuid current timestamp (these bits represent timestamp anyway). know there solutions around web namely comb uuid's, strangely couldn't find java implementation it. thought common problem.

i found interesting implementation on c# here: http://www.codeproject.com/articles/388157/guids-as-fast-primary-keys-under-multiple-database

a similar approach need, i'm having hard time converting java, if me out i'd appreciate it. think of problems regarding big endian/little endian think java use big endian , not 1 native os? not sure on how deal this.

my idea same, generate uuid using uuid.randomuuid() , replace msb resulting id system.currenttimemillis(). 1 thing i'm not sure amount of bytes i'd need because think can represent current timestamp 6 bytes , yet timestamp portion of uuid uses 7.5 bytes:

time_low               = 4*<hexoctet> time_mid               = 2*<hexoctet> time_high_and_version  = 2*<hexoctet> (1 byte uui algorithm version) 

edit: appreciate answers far, please understand question implementing above mentioned algorithm on java , not find alternative this. understand there several other possibilities, , 1 have been mentioned of including client identifier i've used in past, don't solution , wouldn't applicable project because of 2 things: - might work when amount of clients known, isn't case means i'd need generate random ids each client make unique possible mean 36 chars client id prefix , quite few more sequential part means primary keys of 50+ characters not idea. - won't solve issue i'm trying solve have sequential primary keys, once start inserting records different clients same table inserts no longer sequential , you'll have performance hit.

you may overthinking this.

you seem have 2 requirements

  1. globally unique (among databases) ids.
  2. sequentially-generated ids within each database.

i suggest assign each database unique identifier, , append sequentially-generated value identifier.

for example, if have 2 databases , b:

  • a produces identifiers in order: a-1, a-2, a-3, etc.
  • b produces identifiers in order: b-1, b-2, b-3, etc.

the identifiers globally unique , sequential.

i implement identifier 2 columns, 1 db identifier (e.g., or b) , second sequence number (stored integral type).

you delay creation of db identifiers until such time have merge rows between 2 databases.


Comments