简体   繁体   English

通过多个线程从数据库加载数据的最佳方法是什么

[英]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.

相关问题 将变化的数据(多种格式)持久保存到公共数据库表中的最佳方法是什么? - What is the best approach to persist varying data (in multiple formats) into a common db table? 从具有多个线程的单个数据库表中获取数据的最佳方法? - Best way to fetch data from a single database table with multiple threads? 重用多个存储库条件的最佳方法是什么? - What is the best approach to reuse multiple repository criterias? 封装多个方法调用的最佳方法是什么? - What is the best approach to encapsulate multiple method calls? 什么是大规模系统的最佳方法-DB过程或编程逻辑 - What is the best approach for high scaled system - DB procedure or Programming logic 在libgdx上2D游戏加载区域资产的最佳方法是什么? - What will be the best approach for a 2D game loading area assets on libgdx? 从单个服务多次调用多个微服务的最佳方法或设计模式是什么? - What is the best approach or design pattern to call multiple microservices several times from a single service? 从多个线程进行Java日志记录的最佳实践? - Best practices for Java logging from multiple threads? 使用 Java 创建多个同名 xml 元素的最佳方法是什么? - What is the best approach to create multiple xml elements with same name with Java? 在资源控制器中使用多个服务的最佳方法是什么? - What is the best approach to use multiple services inside a resource controller?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM