简体   繁体   English

java:ResultSet迭代与List迭代:是否存在性能差异

[英]java: ResultSet iteration vs List iteration : Is there a performance difference

I have to make a reporting utility which takes data from a very old and large table. 我必须制作一个报表实用程序,该实用程序从一个非常大的旧表中获取数据。 My search criteria will take out say a million records at a time which later be used for some crap IO operation. 我的搜索条件一次将取出一百万条记录,以后将用于某些废话IO操作。 I have the option of using JDBC which will give me a ResultSet or Hibernate which will give me List. 我可以选择使用JDBC,它会给我一个ResultSet或Hibernate,它会给我List。 I want to know will there be a performance difference between the two while iteration. 我想知道两次while迭代之间会有性能差异。

That is dependent on: 这取决于:

  • Hibernate version 休眠版本
  • DBMS and version DBMS和版本
  • JDBC driver and version JDBC驱动程序和版本

How it often works is: if you create your Statement s with: 通常如何工作:如果使用以下方法创建Statement

PreparedStatement stmt = con.prepareStatement(sql,
                                              ResultSet.TYPE_FORWARD_ONLY,
                                              ResultSet.CONCUR_READ_ONLY);

then decent DBMS / drivers will stream big queries, and the memory impact will be small, at the cost of holding the Connection for longer. 那么, 体面的 DBMS /驱动程序将流式传输大量查询,并且对内存的影响将很小,以保持Connection更长的时间为代价。 If you get the List for a big query with Hibernate, it will try to load the entire result set in memory at once, and if GC kicks in, the whole thing will crawl at best and will crash at worst. 如果您使用Hibernate获得了用于大型查询的List ,它将尝试立即将整个结果集加载到内存中,如果启动了GC,则整个过程充其量将在最坏的情况下崩溃。 So for big result sets JDBC will be the better option. 因此,对于较大的结果集,JDBC将是更好的选择。

Now, if you don't actually mind having a List with Hibernate, you can work with ScrollableResults . 现在,如果您实际上不介意使用Hibernate创建List ,则可以使用ScrollableResults See this question : even if the approach did not work for that particular case, that technique will work right on every DBMS/driver combination that the JDBC approach would work right (after all it is just a thin Hibernate layer over the pure JDBC approach explained above). 看到这个问题 :即使该方法不适用于特定情况,该技术也可以在JDBC方法可以正常工作的每个DBMS /驱动程序组合上正常工作 (毕竟,在纯JDBC方法上,这只是一个薄的Hibernate层,因此进行了解释)以上)。 And you also get the ORM part of Hibernate. 您还将获得Hibernate的ORM部分。

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

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