2012-02-24 74 views
2

我一直在阅读使用UUID作为数据库中的主键的优缺点。自定义UUID作为主键

我一直听到这种做法的主要论点是,如果它们不是按顺序生成的,它们可能会使您的索引分段并产生分页问题(我也听说这会吹出数据库的大小,但让暂且不谈)。

MSSQL Server允许您使用自定义的方法来在数据库中创建顺序的UUID(例如CREATE TABLE MyUniqueTable(UniqueColumn UNIQUEIDENTIFIER DEFAULT NEWSEQUENTIALID())。

的问题,它虽然是它创建了一个不符合标准UUID是心不是很明显的顺序我已经落后的设计格式和封装它在生成器类使用或学习:

/** 
* <p> 
* Reverse engineering effort to replicate how SQL Server creates ordered 
* UUIDs so that we may construct them within the application. The builder will 
* only accept version 1 and version 14 (Microsoft specific) uuid objects as a 
* seed. 
* </p> 
* <p> 
* The algorithm is reversible so that a version 1 uuid may be created from a version 
* 14 uuid and vice versa. 
* </p> 
* @author Michael Lambert 
* 
*/ 
public static class MsSqlOrderedUuidBuilder { 

    private static final TimeBasedGenerator generator = Generators.timeBasedGenerator(); 

    private final UUID uuid; 

    public MsSqlOrderedUuidBuilder(UUID uuid) { 

     if(uuid.version() != 1 && uuid.version() != 14) { 
      throw new IllegalArgumentException(String.format("UUID is not a version 1 UUID (version is %d)", uuid.version())); 
     } 
     this.uuid = uuid; 
    } 

    public MsSqlOrderedUuidBuilder() { 
     this(generator.generate()); 
    } 

    private long getMostSignificantBits() { 

     ByteBuffer buffer = ByteBuffer.wrap(new byte[8]); 

     buffer.putLong(uuid.getMostSignificantBits()); 
     buffer.rewind(); 

     byte[] timeLow = new byte[4]; 
     buffer.get(timeLow); 

     byte[] timeMid = new byte[2]; 
     buffer.get(timeMid); 

     byte[] timeHigh = new byte[2]; // time_high and version 
     buffer.get(timeHigh); 

     buffer.clear(); 

     buffer.order(buffer.order().equals(ByteOrder.LITTLE_ENDIAN) ? ByteOrder.BIG_ENDIAN : ByteOrder.LITTLE_ENDIAN); 

     buffer.put(timeHigh); 
     buffer.put(timeMid); 
     buffer.put(timeLow); 

     return buffer.getLong(0); 
    } 

    private long getLeastSignificantBits() { 
     return uuid.getLeastSignificantBits(); 
    } 

    public UUID build() { 
     return new UUID(getMostSignificantBits(), getLeastSignificantBits()); 
    } 
} 

如果我尝试使用这个类来存储在不同的数据库中的结果UUID(我也必须写入MySQL)是不是最终被迫d和我回到我原来的问题。

我的解决方案是创建自己的定制可逆的UUID,当序列化为一个字节数组顺序排序:

/** 
* <p> 
* Creates a custom UUID type with sequential bytes. The builder must be seeded with a version 1 uuid and the 
* algorithm is reversible. 
* </p> 
* @author Michael Lambert 
* 
*/ 
public static class SequentialUuidBuilder { 

    private static final TimeBasedGenerator generator = Generators.timeBasedGenerator(); 

    private final UUID uuid; 

    public SequentialUuidBuilder(UUID uuid) { 

     if(uuid.version() != 1 && uuid.version() != 13) { 
      throw new IllegalArgumentException(String.format("UUID is not a version 1 UUID (version is %d)", uuid.version())); 
     } 
     this.uuid = uuid; 
    } 

    public SequentialUuidBuilder() { 
     this(generator.generate()); 
    } 

    private long getVersion13MostSignificantBits() { 

     if(uuid.version() == 1) { 

      // System.out.println(String.format("original: %x", version1.getMostSignificantBits())); 
      // 
      // System.out.println(String.format("lowa %x", timeLowA)); 
      // 
      // 0xAAAAA00000000000L 
      // 0x0000000AAAAA0000L 
      // 
      long timeLowPartA = (uuid.getMostSignificantBits() & 0xFFFFF00000000000L) >>> 28; 
      // 
      // 0x00000BBB00000000L 
      // 0x0000000000000BBBL 
      // 
      long timeLowPartB = (uuid.getMostSignificantBits() & 0x00000FFF00000000L) >>> 32; 
      // 
      // System.out.println(String.format("lowb %x", timeLowB)); 
      // 
      // 0x00000000MMMM0000L 
      // 0x000MMMM000000000L 
      // 
      long timeMid = (uuid.getMostSignificantBits() & 0x00000000FFFF0000L) << 20; 
      // 
      // System.out.println(String.format("med %x", (timeMid))); 
      // 
      // 0x0000000000000HHHL 
      // 0xHHH0000000000000L 
      // 
      long timeHigh = (uuid.getMostSignificantBits() & 0x0000000000000FFFL) << 52; 
      // 
      // System.out.println(String.format("high %x", timeHigh)); 
      // 
      // 0x000000000000V000L 
      // 0x000000000000V000L 
      // 
      // long version = (version1.getMostSignificantBits() & 0x000000000000F000L); 
      // 
      // System.out.println(String.format("version %x", version)); 
      // 
      // 0x0000000AAAAA0000L 
      // 0x0000000000000BBBL 
      // 0x000MMMM000000000L 
      // 0xHHH0000000000000L 
      // 0x000000000000V000L <-- we don't change where the version is stored because we want to respect that part of the spec 
      // ____________________ 
      // 0xHHHMMMMAAAAAVBBBL 
      // 
      long ordered = timeLowPartA | timeLowPartB | timeMid | timeHigh | 0x000000000000D000L; // custom version 

      return ordered; 
     } 
     return 0; 
    } 

    public long getVersion1MostSignificantBits() { 
     // 
     // 0xHHHMMMMAAAAAVBBBL 
     // 
     long timeLowPartA = (uuid.getMostSignificantBits() & 0x0000000FFFFF0000L) << 28; 
     long timeLowPartB = (uuid.getMostSignificantBits() & 0x0000000000000FFFL) << 32; 
     long timeMid = (uuid.getMostSignificantBits() & 0x000FFFF000000000L) >> 20; 
     long timeHigh = (uuid.getMostSignificantBits() & 0xFFF0000000000000L) >> 52; 
     // 
     // 0xAAAAA00000000000L 
     // 0x00000000MMMM0000L 
     // 0x00000BBB00000000L 
     // 0x0000000000000HHHL 
     // 0x000000000000V000L 
     // ___________________ 
     // 0xAAAAABBBMMMMVHHHL 
     // 
     long bits = timeLowPartA | timeLowPartB | timeMid | timeHigh | 0x0000000000001000L; // reinstate version 

     return bits; 
    } 

    private long getMostSignificantBits() { 
     return (uuid.version() == 13) ? getVersion1MostSignificantBits() : getVersion13MostSignificantBits(); 
    } 

    private long getLeastSignificantBits() { 
     return uuid.getLeastSignificantBits(); 
    } 

    public UUID build() { 
     return new UUID(uuid.version() == 13 ? getVersion1MostSignificantBits() : getMostSignificantBits(), getLeastSignificantBits()); 
    } 
} 

我的问题是:这是一个可以接受的做法?我可以使用BINARY(16)来存储主键,并且可以以这种方式使用自定义标识吗?

谢谢大家提前。 Vive la Stackoverflow!

回答

0

使用序列生成器,除非您确实需要您的密钥具有通用的唯一性。

+0

它们需要在分布式数据库服务器集群中是唯一的。我还研究了使用具有顺序ID和日期的复合键作为元素的可能性,但是使用UUID看起来更加明显(因为其唯一目的是唯一标识实体而不考虑分布)。 – MacFlecknoe 2012-02-25 04:37:24

+0

http://www.digitilapia.com/custom_uuid_as_primary_key – MacFlecknoe 2012-03-03 06:17:05