简体   繁体   English

在 HSQLDB 数据库中存储 UUID

[英]Storing UUID in HSQLDB database

I wish to store UUIDs created using java.util.UUID in a HSQLDB database.我希望将使用java.util.UUID创建的 UUID 存储在 HSQLDB 数据库中。

The obvious option is to simply store them as strings (in the code they will probably just be treated as such), ie varchar(36).显而易见的选择是简单地将它们存储为字符串(在代码中,它们可能会被视为这样),即 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)考虑到数据库大小和查询速度等问题,我应该为此考虑哪些其他选择(由于涉及的数据量,这两个都不是一个大问题,但我想至少考虑它们)

You have a few options:您有几个选择:

  • Store it as a VARCHAR(36), as you already have suggested.正如您已经建议的那样,将其存储为 VARCHAR(36)。 This will take 36 bytes (288 bits) of storage per UUID, not counting overhead.这将占用每个 UUID 36 字节(288 位)的存储空间,不计算开销。
  • Store each UUID in two BIGINT columns, one for the least-significant bits and one for the most-significant bits;将每个 UUID 存储在两个 BIGINT 列中,一个用于最低有效位,一个用于最高有效位; use UUID#getLeastSignificantBits() and UUID#getMostSignificantBits() to grab each part and store it appropriately.使用UUID#getLeastSignificantBits()UUID#getMostSignificantBits()来抓取每个部分并适当地存储它。 This will take 128 bits of storage per UUID, not counting any overhead.这将占用每个 UUID 128 位的存储空间,不计算任何开销。
  • Store each UUID as an OBJECT;将每个 UUID 存储为一个 OBJECT; this stores it as the binary serialized version of the UUID class.这将它存储为 UUID 类的二进制序列化版本。 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.我必须运行一个测试来查看 Java UUID 的默认序列化形式是什么。

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.每种方法的优缺点取决于您如何在应用程序周围传递 UUID——如果您将它们作为它们的字符串等价物传递,那么需要将 VARCHAR(36) 的存储容量加倍的缺点不必在每次执行数据库查询或更新时都转换它们,这可能比方法更重要。 If you're passing them around as native UUIDs, then the BIGINT method probably is pretty low-overhead.如果您将它们作为本机 UUID 传递,那么 BIGINT 方法的开销可能非常低。

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.否则,考虑到廉价的存储成本和数据库索引的能力,这两个问题——UUID 的存储空间,以及在数据库中维护和查询它们所需的时间——是相当低的重要性容易多了。 :) :)

  1. I would recommend char(36) instead of varchar(36) .我会推荐char(36)而不是varchar(36) Not sure about hsqldb, but in many DBMS char is a little faster.不确定 hsqldb,但在许多 DBMS 中,char 会快一点。

  2. For lookups, if the DBMS is smart, then you can use an integer value to "get closer" to your UUID.对于查找,如果 DBMS 是智能的,那么您可以使用整数值来“更接近”您的 UUID。

For example, add an int column to your table as well as the char(36).例如,向表中添加一个 int 列以及 char(36)。 When you insert into your table, insert the uuid.hashCode() into the int column.插入表时,将 uuid.hashCode() 插入 int 列。 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.正如我所说,如果 hsqldb 像 mysql 或 sql server 一样智能,它将通过 intCol 缩小搜索范围,然后最多只通过 uuid 比较几个值。 We use this trick to search through million+ record tables by string, and it is essentially as fast as an integer lookup.我们使用这个技巧通过字符串搜索百万+记录表,它本质上和整数查找一样快。

Using BINARY(16) is another possibility.使用 BINARY(16) 是另一种可能性。 Less storage space than character types.比字符类型更少的存储空间。 Use CREATE TYPE UUID .. or CREATE DOMAIN UUID .. as suggested above.使用 CREATE TYPE UUID .. 或 CREATE DOMAIN UUID .. 如上所述。

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).我认为最简单的方法是创建自己的域,从而创建自己的 UUID“类型”(不是真正的类型,但几乎是类型)。

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? HSQLDB 中的 INT、BIGINT 或 UUID/GUID? (deleted by community ...) (已被社区删除...)

HSQLDB: Domain Creation and Manipulation HSQLDB:域创建和操作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM