[英]What is the best approach for loading data from DB by multiple threads
I have some data in database in parent child relation, where my table is actually representing a forest of tree data structure. 我在父子关系中的数据库中有一些数据,其中我的表实际上代表树数据结构的森林。
And the table structure is like: 表格结构如下:
row1 parent: null
row2 parent:row1
row3 parent:row2
row4 parent:row1
Now when I am loading this data from DB to my data structure by JDBC, then what I am doing now is: 现在,当我通过JDBC将数据从数据库加载到我的数据结构中时,我现在正在做的是:
1. load all row where parent is null [load all the root of trees]
2. Then for each child of each root load the child in a recursive manner.
3. But here all the trees are loaded one by one.
What I want to do, spawn a thread for each tree, thus all the trees can be loaded concurrently. 我想做的是为每棵树生成一个线程,因此可以同时加载所有树。 Any framework I can use with JDBC for the same, or any suggestion, help please.
我可以将JDBC与JDBC一起使用的任何框架或任何建议,请提供帮助。
您可以使用休眠框架在任何数据库中操作(创建,删除,插入,更新..)数据,这对于数据操作非常灵活
Whilst this does not directly answer your exact question, I would encourage you to review your data structure. 尽管这不能直接回答您的确切问题,但我鼓励您检查一下数据结构。 You have used the
Adjacency List Model
which, whist simple to implement initially, opens a whole ugly can of worms when it comes to querying. 您已经使用了
Adjacency List Model
,该Adjacency List Model
最初易于实现,但是在查询时却打开了一堆丑陋的蠕虫。
There are other models to consider, such as Nested Set Model
, for which querying of trees and sub-trees is much easier. 还有其他模型需要考虑,例如
Nested Set Model
,对于它们而言,查询树和子树要容易得多。
Both are explained well in this article: Managing Hierarchical Data in MySQL 这篇文章对这两种方法都进行了很好的解释: 在MySQL中管理分层数据
Although the article is RDBMS vendor specific, if could easily be adjusted to suit your system. 尽管本文是RDBMS供应商特定的,但是如果可以轻松调整以适合您的系统。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.