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

后端 未结 4 1872
谎友^
谎友^ 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:28

    If you want to use the timestamp method then do this:

    Give each server a number, to that append the proccess ID of the application that is doing the insert (or the thread ID) (in PHP it's getmypid()), then to that append how long that process has been alive/active for (in PHP it's getrusage()), and finally add a counter that starts at 0 at the start of each script invocation (i.e. each insert within the same script adds one to it).

    Also, you don't need to store the full unix timestamp - most of those digits are for saying it's year 2011, and not year 1970. So if you can't get a number saying how long the process was alive for, then at least subtract a fixed timestamp representing today - that way you'll need far less digits.

提交回复
热议问题