使用Spring Boot从DB2数据库填充内存中的h2数据库。

huangapple go评论64阅读模式
英文:

Populate h2 in memory database from a db2 database Spring Boot

问题

我目前正在构建一个使用h2内存数据库的Spring Boot服务。
这个数据库充当了中央db2数据库中部分数据的缓存,该db2数据库具有不同的数据库模式。

现在,当Spring Boot服务启动时,它需要使用来自中央数据库的最新数据填充h2数据库。

从性能的角度来看,有什么最佳方法可以做到这一点?
我目前正在考虑在我的服务中创建一个不同的数据源,首先获取数据,然后将数据保存到h2数据库。

这不像是一个好的解决方案,而且填充数据库需要相当长的时间。

英文:

I'm currently building a Spring Boot Service with a h2 in-memory database.
This Database acts as an cache for a part of the data on a central db2 database with a different database schema.

Now when the Spring boot service starts it needs to populate the h2 database with the latest data from the central database.

How can I do this in the best way performance wise?
I'm currently looking to create an different data-source in my service to first get the data and then save the data to the h2.

This doesn't feel like a good solution and it would take quite a long time to populate the database.

答案1

得分: 3

如果您想要使用 H2 而不是您的 DB2 数据库... 而且如果您不想在每次运行应用程序时重新创建数据库...

... 那么考虑使用 H2 文件,而不是内存:

http://www.h2database.com/html/features.html

jdbc:h2:[file:][<path>]<databaseName>
jdbc:h2:~/test
jdbc:h2:file:/data/sample
jdbc:h2:file:C:/data/sample(仅适用于 Windows)

您可以在任何时候“初始化”文件(也许只需一次)。

性能应该是非常优秀的。


根据您的更新:

我仍然需要访问中央数据库以尽可能快的方式获取最新数据。中央数据库需要为其他访问此数据库的服务保持开放。

获取最新数据的“最快”方式... 是直接查询中央数据库。毫不犹豫地进行查询 - 没有任何条件。

但是,如果出于某种原因,您想要“缓存”“近期”数据的子集... 那么 H2 是一个很好的选择。

如果您不想在每次启动 H2 数据库时“重建”,那么将 H2 保存到文件中,而不是将其保存在内存中。

与查询中央数据库的网络开销相比,H2:mem 和 H2:file 之间的性能差异很小。

希望对您有所帮助...

英文:

If you want to use H2 instead of your DB2 database ... and if you don't want to re-create the database each time you run your app ...

... then consider using an H2 file, instead of in-memory:

> http://www.h2database.com/html/features.html
>
> jdbc:h2:[file:][<path>]<databaseName>
> jdbc:h2:~/test
> jdbc:h2:file:/data/sample
> jdbc:h2:file:C:/data/sample (Windows only)

You can "initialize" the file whenever you want (perhaps just once).

Performance should be excellent.


Per your update:

> I still need to access the central db to get the latest data in the
> fastest way possible. The central db needs to stay for other services
> also accessing this

The "fastest" way to get the very latest data ... is to query the central db directly. Period - no ifs/ands/buts.

But, if for whatever reason, you want to "cache" a subset of "recent" data ... then H2 is an excellent choice.

And if you don't want to "rebuild" each time you start your H2 database, then save H2 to a file instead of making it in-memory.

The performance difference between H2:mem and H2:file is small, compared to the network overhead of querying your central db.

'Hope that helps...

huangapple
  • 本文由 发表于 2020年4月6日 23:03:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/61062778.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定