H2数据库:聚簇索引支持

Tim*_*fey 3 h2 clustered-index

我使用H2数据库来处理包含大量时间序列的环境数据.时间序列只是传感器的测量值,它们定期记录在数据库中(比如每小时一次).

存储在表中的数据:

CREATE TABLE hydr
(dt timestamp
,value double
,sensorid int)
Run Code Online (Sandbox Code Playgroud)

我想对表进行范围查询,例如:

select * from hydr
where dt between '2010-01-01' and '2010-10-01'
Run Code Online (Sandbox Code Playgroud)

为了提高性能,我想在dt列上构建聚簇索引,但问题是,如果H2支持聚簇索引,我还没有找到.有人知道H2中是否支持聚簇索引

Tho*_*ler 10

简短回答:您的表的主键必须是BIGINT类型:

CREATE TABLE hydr(dt bigint primary key, value double, sensorid int);
Run Code Online (Sandbox Code Playgroud)

在这种情况下,使用"dt"列组织表.这称为"聚集索引".不支持数据类型TIMESTAMP,主要是因为它还包含纳秒.你可以做的是将unix时间戳(自1970年以来的毫秒)存储为BIGINT.

答案很长:数据如何在H2内部存储的文档很差.我将在H2数据库的"性能"文档中添加以下部分.我希望这会清理一些事情(如果没有请告诉我):

如何在内部存储数据

对于持久性数据库,如果使用BIGINT,INT,SMALLINT,TINYINT类型的单列主键创建表,则以这种方式组织表的数据.这有时也称为"聚簇索引"或"索引组织表".

H2 internally stores table data and indexes in the form of b-trees. Each b-tree stores entries as a list of unique keys (one or more columns) and data (zero or more columns). The table data is always organized in the form of a "data b-tree" with a single column key of type long. If a single column primary key of type BIGINT, INT, SMALLINT, TINYINT is specified when creating the table, then this column is used as the key of the data b-tree. If no primary key has been specified, if the primary key column is of another data type, or if the primary key contains more than one column, then a hidden auto-increment column of type BIGINT is added to the table, which is used as the key for the data b-tree. All other columns of the table are stored within the data area of this data b-tree (except for large BLOB, CLOB columns, which are stored externally).

对于每个附加索引,创建一个新的"索引b树".此b树的关键字包括索引列以及数据b树的键.如果在插入数据后创建主键,或者主键包含多个列,或者主键不是上面列出的数据类型,则主键存储在新的索引b树中.