Cassandra UUID vs TimeUUID benefits and disadvantages

前端 未结 3 716
我寻月下人不归
我寻月下人不归 2020-12-04 17:57

Given that TimeUUID handily allows you to use now() in CQL, are there any reasons you wouldn\'t just go ahead and always use TimeUUID instead of plain old UUID?

3条回答
  •  抹茶落季
    2020-12-04 18:20

    A TimeUUID is a plain old UUID according to the documentation.

    A UUID is simply a 128-bit value. Think of it as an unimaginably large number.

    The particular bits may be determined by any of several methods. The original method involved taking the MAC address of the computer's networking hardware, combining the current date and time, plus an arbitrary number and a random number. Squish all that together to get a virtually unique number.

    Later, for various reasons (security, privacy), other methods were invented to assemble the bits when generating a UUID value. These other methods omit date-time and/or MAC address as an ingredient. The point being: Not all UUID values have an embedded date-time value.

    The Cassandra doc incorrectly refers to its TimeUUID being a "Type 1 UUID". The correct term is Version 1 UUID. This version is sometimes called the "time-based version".


    A Bit Of Advice

    Cassandra seems to identify this specific version of UUID for the purpose of extracting the date and time portion of the 128-bits. Extracting the date-time from a UUID is a bad idea.

    For one thing, UUID was never intended to be used for such history tracking. Indeed, the spec for UUID specifically recognizes that (a) computer clocks can be reset and therefor (b) UUIDs generated later may actually record an earlier date-time than previous UUIDs. Another reason to not extract date-time from a UUID is because you may well have UUIDs that were not generated by the time method, therefore you will be building a data-time value based on bits that do not in fact represent the date-time of creation. A third reason is that when programming code is later refactored, the UUID may be generated at a different time than the database record so using the UUID's date-time would be misleading.

    If you need to track date-time history, do so explicitly. Create a date-time field in your data. By the way, track that date-time in UTC, but that’s another topic.

提交回复
热议问题