简体   繁体   English

同时从数据库中选择多个数据

[英]Selecting multiple data from the database at the same time

I am creating a java application that needs to collect loads of data, process the data into objects and return the objects as a list. 我正在创建一个Java应用程序,该应用程序需要收集数据负载,将数据处理为对象并将对象作为列表返回。

All of the data collected is from different tables in my database (some are joined but all of them are different SQL calls) 收集的所有数据都来自我数据库中的不同表(有些已联接,但它们都是不同的SQL调用)

I was thinking of getting this data through different threads but since multiple threads cannot use the same connection to acess data in the database i would have to create a new connection for each of these threads. 我当时正在考虑通过不同的线程获取此数据,但是由于多个线程无法使用相同的连接访问数据库中的数据,因此我必须为每个线程创建一个新的连接。

My Question is: what is the best way to acess and process multiple data from database at the same time? 我的问题是:同时访问和处理数据库中的多个数据的最佳方法是什么?

If you have enough memory I would use a full second level change that syncs to the database. 如果您有足够的内存,我将使用完整的第二级更改来同步到数据库。 Using a cache makes it extremely faster. 使用缓存可以使其变得非常快。 If you won't have enough memory on the server/client you can cache your query on the sqlserver with a table that has all the values from your query and this table gets updated every second. 如果服务器/客户端上没有足够的内存,则可以使用包含查询中所有值的表在sqlserver上缓存查询,并且该表每秒钟更新一次。

Otherwise you can use a Threadpool with Threads which inserts the queryresults into a shared object for the result. 否则,您可以使用带有线程的线程池,该线程将查询结果插入共享对象的结果中。

I am using Spring framework .Suppose there is ModelBean class present where all constants are declared.In ModelBean class name is field which are declared. 我正在使用Spring framework假设存在所有常量都已声明的ModelBean类.ModelBean类名是声明的字段。

public class CurriculumReportDaoImpl extends JdbcDaoSupport{
   public List<String> fetchList(){
      String query="";
      List<ModelBean > tempList=new ArrayList<ModelBean >();
      List<Map<String,Object>> record = getJdbcTemplate().queryForList(query);

      for(Map<String,Object> result=record){
         /*create new instance of ModelBean*/
         model=new ModelBean();
         /*"name" is the column name which are fetch from db*/
         model.setName(result.get("name").toString);
         /*now set model in tempList*/
         tempList.add(ModelBean );
      }

      return tempList;
   }
}

If you have many connection Then you can create many List here and set into ModelBean class. 如果您有许多连接,则可以在此处创建许多List并将其设置为ModelBean类。 I think this will help you. 我认为这会对您有所帮助。

Ideally the database should be designed in such a way that you should be able to get all the related data in a single query(maybe with JOINS). 理想情况下,数据库的设计方式应使您能够在单个查询中获取所有相关数据(也许使用JOINS)。 Not sure if that is possible to achieve in your case. 不知道您的情况是否有可能实现。

There are three viable options, one you have already tried by creating multiple threads and fetching the data. 共有三种可行的选择,您已经尝试通过创建多个线程并获取数据来进行尝试。 I will just add an input to that approach and you may try in case that optimizes. 我将仅向该方法添加输入,并且您可以尝试优化。 Create Data Fetcher threads to fetch the data from different tables and create one Data Processor thread to process the data as it is fetched by Data Fetcher threads. 创建Data Fetcher线程以从不同的表中获取数据,并创建一个Data Processor线程以处理由Data Fetcher线程获取的数据。

or 要么

Second approach can be to to create a stored procedure, which will run directly on the database and can do some data processing for you. 第二种方法是创建一个存储过程,该过程将直接在数据库上运行并可以为您做一些数据处理。 This will avoid the need of creating too many threads and doing a lot of processing in your java code. 这将避免在Java代码中创建太多线程并进行大量处理的需要。

or 要么

Mix both the approaches to achieve the best results. 混合两种方法以获得最佳结果。

Good luck! 祝好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM