在HSQLDB数据库中存储UUID

发布于 2024-08-14 07:38:03 字数 185 浏览 2 评论 0原文

我希望将使用 java.util.UUID 创建的 UUID 存储在 HSQLDB 数据库中。

显而易见的选择是将它们简单地存储为字符串(在代码中它们可能只是被视为字符串),即 varchar(36)。

考虑到数据库大小和查询速度等问题,我还应该考虑哪些其他选项(由于涉及的数据量,这些都不是一个大问题,但我至少想考虑它们)

I wish to store UUIDs created using java.util.UUID in a HSQLDB database.

The obvious option is to simply store them as strings (in the code they will probably just be treated as such), i.e. varchar(36).

What other options should I consider for this, considering issues such as database size and query speed (neither of which are a huge concern due to the volume of data involved, but I would like to consider them at least)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

风为裳 2024-08-21 07:38:03

HSQLDB 有一个内置的 UUID 类型。使用它

CREATE TABLE t (
  id UUID PRIMARY KEY
);

HSQLDB has a built-in UUID type. Use that

CREATE TABLE t (
  id UUID PRIMARY KEY
);
哭了丶谁疼 2024-08-21 07:38:03

您有几个选择:

每种方法的优点和缺点取决于您在应用程序中传递 UUID 的方式 - 如果您将它们作为字符串等效项传递,那么缺点是需要双倍的 VARCHAR(36) 存储容量这种方法可能更重要,因为不必在每次进行数据库查询或更新时都转换它们。如果您将它们作为本机 UUID 传递,那么 BIGINT 方法的开销可能相当低。

哦,很高兴您考虑速度和存储空间问题,但正如许多比我更好的人所说,考虑到您的应用程序将存储的数据量,您认识到这些可能不是至关重要的也很好和维护。一如既往,为了性能而进行的微优化只有在不这样做会导致不可接受的成本或性能的情况下才重要。否则,考虑到廉价的存储成本和数据库索引的能力,这两个问题——UUID 的存储空间以及在数据库中维护和查询它们所需的时间——的重要性相当低。容易多了。 :)

You have a few options:

  • Store it as a VARCHAR(36), as you already have suggested. This will take 36 bytes (288 bits) of storage per UUID, not counting overhead.
  • Store each UUID in two BIGINT columns, one for the least-significant bits and one for the most-significant bits; use UUID#getLeastSignificantBits() and UUID#getMostSignificantBits() to grab each part and store it appropriately. This will take 128 bits of storage per UUID, not counting any overhead.
  • Store each UUID as an OBJECT; this stores it as the binary serialized version of the UUID class. I have no idea how much space this takes up; I'd have to run a test to see what the default serialized form of a Java UUID is.

The upsides and downsides of each approach is based on how you're passing the UUIDs around your app -- if you're passing them around as their string-equivalents, then the downside of requiring double the storage capacity for the VARCHAR(36) approach is probably outweighed by not having to convert them each time you do a DB query or update. If you're passing them around as native UUIDs, then the BIGINT method probably is pretty low-overhead.

Oh, and it's nice that you're looking to consider speed and storage space issues, but as many better than me have said, it's also good that you recognize that these might not be critically important given the amount of data your app will be storing and maintaining. As always, micro-optimization for the sake of performance is only important if not doing so leads to unacceptable cost or performance. Otherwise, these two issues -- the storage space of the UUIDs, and the time it takes to maintain and query them in the DB -- are reasonably low-importance given the cheap cost of storage and the ability of DB indices to make your life much easier. :)

始终不够 2024-08-21 07:38:03
  1. 我建议使用 char(36) 而不是 varchar(36)。不确定 hsqldb,但在许多 DBMS 中,char 速度要快一些。

  2. 对于查找,如果 DBMS 很智能,那么您可以使用整数值来“更接近”您的 UUID。

例如,将 int 列以及 char(36) 添加到表中。当您插入表时,将 uuid.hashCode() 插入 int 列。那么你的搜索可以像这样

WHERE intCol = ? and uuid = ?

正如我所说,如果 hsqldb 像 mysql 或 sql server 一样智能,它会通过 intCol 缩小搜索范围,然后仅通过 uuid 比较最多几个值。我们使用这个技巧通过字符串搜索百万+记录表,它本质上与整数查找一样快。

  1. I would recommend char(36) instead of varchar(36). Not sure about hsqldb, but in many DBMS char is a little faster.

  2. For lookups, if the DBMS is smart, then you can use an integer value to "get closer" to your UUID.

For example, add an int column to your table as well as the char(36). When you insert into your table, insert the uuid.hashCode() into the int column. Then your searches can be like this

WHERE intCol = ? and uuid = ?

As I said, if hsqldb is smart like mysql or sql server, it will narrow the search by the intCol and then only compare at most a few values by the uuid. We use this trick to search through million+ record tables by string, and it is essentially as fast as an integer lookup.

美煞众生 2024-08-21 07:38:03

使用 BINARY(16) 是另一种可能性。比字符类型更少的存储空间。按照上面的建议使用 CREATE TYPE UUID .. 或 CREATE DOMAIN UUID .. 。

Using BINARY(16) is another possibility. Less storage space than character types. Use CREATE TYPE UUID .. or CREATE DOMAIN UUID .. as suggested above.

数理化全能战士 2024-08-21 07:38:03

我认为最简单的事情就是创建您自己的域,从而创建您自己的 UUID“类型”(不是真正的类型,但几乎是)。

您还应该考虑这个问题的答案(特别是如果您打算使用它而不是“普通”主键)

HSQLDB 中的 INT、BIGINT 或 UUID/GUID?(已被社区删除...)

HSQLDB:域创建和操作

I think the easiest thing to do would be to create your own domain thus creating your own UUID "type" (not really a type, but almost).

You also should consider the answer to this question (especially if you plan to use it instead of a "normal" primary key)

INT, BIGINT or UUID/GUID in HSQLDB? (deleted by community ...)

HSQLDB: Domain Creation and Manipulation

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文