MySQL PRIMARY KEYs: UUID / GUID vs BIGINT (timestamp+random)

后端 未结 4 1874
谎友^
谎友^ 2020-12-24 07:47

tl;dr: Is assigning rows IDs of {unixtimestamp}{randomdigits} (such as 1308022796123456) as a BIGINT a good idea if I don\'t want to deal with UUIDs?

4条回答
  •  独厮守ぢ
    2020-12-24 08:33

    You can manually change the autonumber starting number.

    ALTER TABLE foo AUTO_INCREMENT = ####
    

    An unsigned int can store up to 4,294,967,295, lets round it down to 4,290,000,000.

    Use the first 3 digits for the server serial number, and the final 7 digits for the row id.

    This gives you up to 430 servers (including 000), and up to 10 million IDs for each server.

    So for server #172 you manually change the autonumber to start at 1,720,000,000, then let it assign IDs sequentially.

    If you think you might have more servers, but less IDs per server, then adjust it to 4 digits per server and 6 for the ID (i.e. up to 1 million IDs).

    You can also split the number using binary digits instead of decimal digits (perhaps 10 binary digits per server, and 22 for the ID. So, for example, server 76 starts at 2^22*76 = 318,767,104 and ends at 322,961,407).

    For that matter you don't even need a clear split. Take 4,294,967,295 divide it by the maximum number of servers you think you will ever have, and that's your spacing.

    You could use a bigint if you think you need more identifiers, but that's a seriously huge number.

提交回复
热议问题