H2,Sql-Server,MySql中的二进制数据类型

时间:2022-09-11 14:45:07

I need to store a serialized Object as binary data in a H2 table (also in MySql, SqlServer, Hsqldb, etc, as the app have options to select the database). This object only weights few bytes, it is just a POJO with some Strings, Integers and Boolean values, nothing extraordinary.

我需要将一个序列化的对象作为二进制数据存储在H2表中(也可以在MySql,SqlServer,Hsqldb等中,因为应用程序有选择数据库的选项)。这个对象只加权几个字节,它只是一个带有一些字符串,整数和布尔值的POJO,没什么特别之处。

I create the VARBINARY(1000) column set the value with a PreparedStatement and save it to the table. My question and my concern is that, this code works in a website and as I read the definition in the H2 official site about the BINARY data type, there they say:

我创建VARBINARY(1000)列,使用PreparedStatement设置值并将其保存到表中。我的问题和我担心的是,这个代码在一个网站上工作,当我在H2官方网站上阅读关于BINARY数据类型的定义时,他们说:

BINARY Type: Represents a byte array. For very long arrays, use BLOB. The maximum size is 2 GB, but the whole object is kept in memory when using this data type. The precision is a size constraint; only the actual data is persisted. For large text data BLOB or CLOB should be used.

BINARY类型:表示字节数组。对于很长的数组,请使用BLOB。最大大小为2 GB,但使用此数据类型时整个对象将保留在内存中。精度是一个尺寸约束;只保留实际数据。对于大文本数据,应使用BLOB或CLOB。

What worries me is that statement where they say "but the whole object is kept in memory when using this data type". So, how much time actually is this object hold in memory? Until application shutdown?

让我担心的是那句话,他们说“但是当使用这种数据类型时整个对象都保存在内存中”。那么,这个对象在内存中实际存在多少时间?申请关闭之前?

What I don't (obviously) want is to have this column eating the memory by keeping this column endlessly. I tried to find any other source of information but no luck whatsoever.

我(显然)想要的是让这个专栏无休止地保留这一专栏。我试图找到任何其他信息来源,但没有任何运气。

2 个解决方案

#1


0  

The best data type for HSQLDB and MySQL is VARRBINARY(max_bytes_length). BINARY(fixed_bytes_length) takes the same amount of space even for the smallest saved object, while VARBINARY takes exactly the length of the saved object. The max_length parameter can be set to a value that is much larger than the maximum anticipated size of the object. Withe these databases, the value is held in memory only when the row of data is accessed.

HSQLDB和MySQL的最佳数据类型是VARRBINARY(max_bytes_length)。即使对于最小的保存对象,BINARY(fixed_bytes_length)也占用相同的空间量,而VARBINARY则采用所保存对象的长度。 max_length参数可以设置为远大于对象的最大预期大小的值。对于这些数据库,只有在访问数据行时,该值才会保留在内存中。

#2


0  

The BLOB type won't be kept fully in memory, so if you are concerned about memory usage you could use that. How much data are you storing anyway?

BLOB类型不会完全保留在内存中,因此如果您担心内存使用情况,可以使用它。你还存储了多少数据?

H2 is an embedded database, I don't think its intended to scale to large amounts of data. If you are talking about the other databases, you should refer to their documentation for details as they are unlikely to behave the same way.

H2是一个嵌入式数据库,我认为它不打算扩展到大量数据。如果您正在谈论其他数据库,您应该参考他们的文档以获取详细信息,因为它们不太可能以相同的方式运行。

#1


0  

The best data type for HSQLDB and MySQL is VARRBINARY(max_bytes_length). BINARY(fixed_bytes_length) takes the same amount of space even for the smallest saved object, while VARBINARY takes exactly the length of the saved object. The max_length parameter can be set to a value that is much larger than the maximum anticipated size of the object. Withe these databases, the value is held in memory only when the row of data is accessed.

HSQLDB和MySQL的最佳数据类型是VARRBINARY(max_bytes_length)。即使对于最小的保存对象,BINARY(fixed_bytes_length)也占用相同的空间量,而VARBINARY则采用所保存对象的长度。 max_length参数可以设置为远大于对象的最大预期大小的值。对于这些数据库,只有在访问数据行时,该值才会保留在内存中。

#2


0  

The BLOB type won't be kept fully in memory, so if you are concerned about memory usage you could use that. How much data are you storing anyway?

BLOB类型不会完全保留在内存中,因此如果您担心内存使用情况,可以使用它。你还存储了多少数据?

H2 is an embedded database, I don't think its intended to scale to large amounts of data. If you are talking about the other databases, you should refer to their documentation for details as they are unlikely to behave the same way.

H2是一个嵌入式数据库,我认为它不打算扩展到大量数据。如果您正在谈论其他数据库,您应该参考他们的文档以获取详细信息,因为它们不太可能以相同的方式运行。