设为首页 加入收藏

TOP

Spring 保存带Array字段的记录到PostgreSQL(一)
2023-07-25 21:41:57 】 浏览:62
Tags:Spring Array 段的记 PostgreSQL

前言

本文继续学习PostgreSQL, 看到PostgreSQL有个Array字段,感觉可以用来存储某种表,比如股票每天的价格, 我们称为market_price表,先来看下最开始market_price 表的定义

create table market_price(
id char(10),
trade_date  date, 
open float , 
high float, 
low float,
close float,
primary key (id,trade_date)
);

表说明

id 每支股票有个对应的编号,
trade_date是交易日期,
open high low close分别代表开盘价,最高价,最低价,收盘价。

这样定义表结构,我们要查询某天的价格非常方便,给定id和日期就能查出来,但是有个问题就是存到postgreSQL后, 记录会非常多,假设全球有10万只股票,我们存储从1990到今天的数据,那么中间的日期数量就是每支股票有大概12000条记录。总记录数就是有12亿条记录,对于关系型数据库数据上亿后,查询性能会下降比较明显, 有什么办法可以把记录数减少一些呢? 我们可以尝试一下Array来存储下, 看这样的表结构

create table market_price_month_array(
id char(10),
year smallint, 
month smallint,
open float array[31], 
high float array[31], 
low float array[31],
close float array[31]
primary key (id,year,month)
);

我们这里使用了Array,把每个月的数据存成1行,每个月都按31天算,open[1]就表示第一天, open[2] 就表示第2天, 这样数据行数能减少30倍,12亿行变成4千万行,查询性能会好很多。
下面是存入和更新的例子

postgres=# insert into market_price_month_array values('0P00000001',2023,2,'{2.11,2.12,2.13,2.14,2.15,2.16,2.17,2.18,2.19}','{4.11,4.12,4.13,4.14,4.15,4.16,4.17,4.18,4.19}','{1.11,1.12,1.13,1.14,1.15,1.16,1.17,1.18,1.19}','{3.11,3.12,3.13,3.14,3.15,3.16,3.17,3.18,3.19}');
INSERT 0 1
postgres=# select * from market_price_month_array;
 0P00000001 | 2023 |     2 | {2.11,2.12,2.13,2.14,2.15,2.16,2.17,2.18,2.19} | {4.11,4.12,4.13,4.14,4.15,4.16,4.17,4.18,4.19} | {1.11,1.12,1.13,1.14,1.15,1.16,1.17,1.18,1.19} | {3.11,3.12,3.
13,3.14,3.15,3.16,3.17,3.18,3.19}
(1 row)


postgres=# update market_price_month_array set open[19] = 2.19, high[19] = 4.19, low[19]= 1.19, close[19]=3.19 where id = '0P00000001' and year = 2023 and month = 2;
UPDATE 1
postgres=# select * from market_price_month_array;
 0P00000001 | 2023 |     2 | {2.11,2.12,2.13,2.14,2.15,2.16,2.17,NULL,2.19,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,2.19} | {4.11,4.12,4.13,4.14,4.15,4.16,4.17,NULL,4.19,NULL,NULL,NULL,
NULL,NULL,NULL,NULL,NULL,NULL,4.19} | {1.11,1.12,1.13,1.14,1.15,1.16,1.17,NULL,1.19,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,1.19} | {3.11,3.12,3.13,3.14,3.15,3.16,3.17,NULL,3.19,NULL,N
ULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,3.19}

插入的时候,值用“'{2.11,2.12,2.13,2.14,2.15,2.16,2.17,2.18,2.19}'”, 没有的日期就自动设置为NULL了。
想更新哪一天的,就直接用close[19]=3.19, 使用非常方便。

那么我们想要用Java来进行插入数据应该怎么做呢? 是不是和其他非数组的类型一样的用法呐?当然是有些不一样的,下面部分就是如何使用Spring来保存Array类型。

JPA 方式保存

JPA方式是我们存入数据库的时候最方便的方式,定义个entity, 然后定义个接口就能干活了。
但是这里直接在Entity里面这样定义Double[] open是不行的,需要加一个类型转化,我参考了这篇文章https://www.baeldung.com/java-hibernate-map-postgresql-array
这里直接给代码

package ken.postgresql.poc;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.Type;

import javax.persistence.Column;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.Table;

@Entity
@Table(name = "market_price_month_array")
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class MarketPriceMonth {
    @EmbeddedId
    private MarketPriceMonthKey id;

    @Column(columnDefinition = "float[]")
    @Type(type = "ken.postgresql.poc.arraymapping.CustomDoubleArrayType")
    private Double[] open;

    @Column(columnDefinition = "float[]")
首页 上一页 1 2 3 4 下一页 尾页 1/4/4
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇日志瘦身骚操作:从 5G 优化到 1G.. 下一篇Junit环境配置和在IDEA中使用Juni..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目