本文介绍了Cassandra UUID 与 TimeUUID 的优缺点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

鉴于 TimeUUID 可以方便地让您在 CQL 中使用 now(),您是否有任何理由不继续使用 TimeUUID 而不是普通的旧 UUID?

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?

推荐答案

UUIDTIMEUUID 在 Cassandra 中的存储方式是一样的,它们只是真正代表了两种不同的排序方式实现.

UUID and TIMEUUID are stored the same way in Cassandra, and they only really represent two different sorting implementations.

TIMEUUID 列首先按时间分量排序,然后按原始字节排序,而 UUID 列首先按版本排序,如果两者都是版本 1按它们的时间分量,最后按它们的原始字节.奇怪的是,时间组件排序实现在 Cassandra 代码中的 UUIDTypeTimeUUIDType 之间重复,除了不同的格式.

TIMEUUID columns are sorted by their time components first, and then by their raw bytes, whereas UUID columns are sorted by their version first, then if both are version 1 by their time component, and finally by their raw bytes. Curiosly the time component sorting implementations are duplicated between UUIDType and TimeUUIDType in the Cassandra code, except for different formatting.

我认为 UUIDTIMEUUID 的问题主要是作为文档:如果你选择 TIMEUUID 你是说你正在存储事情按时间顺序排列,并且这些事情可以同时发生,所以简单的时间戳是不够的.使用 UUID 表示您不关心顺序(即使在实践中,如果您将版本 1 UUID 放入其中,列将按时间排序),您只想确保事物具有唯一性身份证件.

I think of the UUID vs. TIMEUUID question primarily as documentation: if you choose TIMEUUID you're saying that you're storing things in chronological order, and that these things can occur at the same time, so a simple timestamp isn't enough. Using UUID says that you don't care about order (even if in practice the columns will be ordered by time if you put version 1 UUIDs in them), you just want to make sure that things have unique IDs.

即使使用 NOW() 生成 UUID 值很方便,但对于阅读您代码的其他人来说也是非常令人惊讶的.

Even if using NOW() to generate UUID values is convenient, it's also very surprising to other people reading your code.

从整体上看,这可能无关紧要,但对非版本 1 UUID 进行排序比版本 1 快一点,因此如果您有一个 UUID 列并自己生成 UUID,换一个版本.

It probably does not matter much in the grand scheme of things, but sorting non-version 1 UUIDs is a bit faster than version 1, so if you have a UUID column and generate the UUIDs yourself, go for another version.

这篇关于Cassandra UUID 与 TimeUUID 的优缺点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-22 15:58