简体   繁体   English

Java中的DAO类花费太多时间来获取数据

[英]DAO Class in java taking too much time to fetch Data

I have the following Data in my DAO class 我的DAO课上有以下数据

public List<Environment> fetchMiddlewareVersions() throws SQLException{
      System.out.println("reached version");
      Environment environment;
      List<Environment> environments=new ArrayList<Environment>();
 try{
         connection=DBConnectionUtil.getConnection();
         statement=connection.createStatement();
         statement.setFetchSize(100);
         preparedStatement=connection.prepareStatement("select * from     middleware_version_details order by application,environment");
         resultSet=preparedStatement.executeQuery();
            while(resultSet.next())
            {
                environment = new Environment();
                environment.setAppName(resultSet.getString("APPLICATION"));
                environment.setHostName(resultSet.getString("HOSTNAME"));
                environment.setSoftwareComponent(resultSet.getString("SOFTWARE_COMPONENT"));
                environment.setVersion(resultSet.getString("VERSION"));
                environment.setInstallPath(resultSet.getString("INSTALL_PATH"));
                environment.setRemarks(resultSet.getString("REMARKS"));
                environment.setEnvironmental(resultSet.getString("ENVIRONMENT"));
                environments.add(environment);  

            }
    }

By the time I get the entire data into JSP page, it has consumed 20-30 seconds already. 当我将所有数据放入JSP页面时,它已经消耗了20-30秒。 How do I Increase speed of the Fetch. 如何提高提取速度。 I tried DynaCache and it hasn't helped. 我尝试了DynaCache,但并没有帮助。

So barring any sort of connectivity issues, it almost always comes down to the number of records you're fetching. 因此,除非出现任何类型的连接问题,否则几乎总归结为您要获取的记录数。 If you're fetching A TON of records, the method will not return until it has gone through each item and created an array object. 如果您要获取大量记录,则该方法只有在遍历每个项目并创建数组对象后才会返回。

I would try adding a LIMIT and OFFSET clause to your SQL Statement to only retrieve records, say, 25 at a time. 我会尝试在您的SQL语句中添加LIMITOFFSET子句,以一次仅检索25条记录。 setFetchSize( int ) does not affect the number of overall records, only the number of records the underlying transport will fetch at a time from your sever. setFetchSize( int )不会影响总记录数,仅会从您的服务器一次获取基础传输的记录数。 (also, move your SQL query into a static final variable: (此外,将您的SQL查询移至静态最终变量中:

 private static final String SQL_FETCH_MIDDLEWARE_VERSION = 
      "SELECT * FROM middleware_version_details order by application, environment " +
      "LIMIT = ? OFFSET = ?";

then set the limit and the offset in your prepared statement like so: 然后在准备好的语句中设置限制和偏移量,如下所示:

 preparedStatement.setInt( 1, <RECORD COUNT> );
 preparedStatement.setInt( 2, <RECORD START> );

Third, do you have an index on application and environment ? 第三,您是否有关于applicationenvironment的索引? If you do not and you will be constantly ordering, filtering and joining on those columns, you should add an index. 如果不这样做,并且您将不断对这些列进行排序,过滤和联接,则应添加索引。

Fourth, and it's a minor point but one that I adhere to, is that doing resultSet.getString( "<COLUMN NAME>" ) will cause another call to go look up the column index. 第四,这是一个次要的要点,但我要坚持的一点是,执行resultSet.getString( "<COLUMN NAME>" )将导致另一个调用来查找列索引。 It's not usually a huge deal, but if you're trying to be as performant as possible, you should use the numeric index. 通常这不是什么大问题,但是如果您想尽可能地表现出色,则应该使用数字索引。 You can do this by creating private static variables holding the index: 您可以通过创建包含索引的私有静态变量来做到这一点:

 private static int INDEX_ENVIRONMENT = 6;

or you can use a counter and just insure that the columns are in the correct order, something like this: 或者您可以使用计数器并确保列的顺序正确,如下所示:

while(resultSet.next())
{
     int iC = 0;

     environment = new Environment();
     environment.setAppName(resultSet.getString( iC++ ));
     environment.setHostName(resultSet.getString( iC++ ));
     environment.setSoftwareComponent(resultSet.getString( iC++ ));
     environment.setVersion(resultSet.getString( iC++ ));
     environment.setInstallPath(resultSet.getString( iC++ ));
     environment.setRemarks(resultSet.getString( iC++ ));
     environment.setEnvironmental(resultSet.getString( iC++ ));
     environments.add(environment);  
 }

Just insure that you're setting the variables in the correct order and it will be slightly more performant. 只要确保您以正确的顺序设置变量,它的性能就会更高。 I like this counter approach as well because it allows me to easily adapt to changing schemas. 我也喜欢这种相反的方法,因为它使我可以轻松适应不断变化的模式。

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

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