简体   繁体   English

如何在使用大量记录时使用Hibernate更快地进行更新

[英]How to make update faster with Hibernate while using huge number of records

I am facing issue while using the hibernate update (Session.update()) portion with huge number of records. 我在使用具有大量记录的hibernate更新(Session.update())部分时遇到问题。 it is becoming very slower. 它变得非常慢。 but there is no issue with the insert (Session.insert()) portion. 但插入(Session.insert())部分没有问题。 is there any way to do the update portion while we do update on lakh's of records.is there any way to tune the sql server so that the update will become faster. 有没有办法在我们更新lakh的记录时执行更新部分。有没有办法调整sql服务器,以便更新将变得更快。 while we add seperate indexes to all the primary fields then the delete portion is taking time. 当我们为所有主要字段添加单独的索引时,删除部分需要时间。 is there any better way to tune sql server so that it performs well with insert, delete and update. 是否有更好的方法来调整SQL服务器,以便它与插入,删除和更新表现良好。

Thank you, Saif. 谢谢,赛义夫。

do a batch update instead of individual update for each record. 为每条记录执行批量更新而不是单独更新。 this way you will only hit the database once for all the records. 这样,您只需为所有记录命中一次数据库。

When you do a save only the data is saved into the database whereas when your updating a record it has to first perform the search operation and then update the record that is why your facing issues on update and not on save when your are handling huge number of records can use hibernate's BATCH PROCESSING to update your records. 当您执行保存时,只将数据保存到数据库中,而当您更新记录时,它必须首先执行搜索操作,然后更新记录,这就是您面临更新问题的原因,而不是在处理大量数据时保存记录可以使用hibernate的BATCH PROCESSING来更新您的记录。 Here is a good link for batch processing in hibernate from tutorials point: 从教程点来看,这是hibernate中批处理的一个很好的链接:

http://www.tutorialspoint.com/hibernate/hibernate_batch_processing.htm http://www.tutorialspoint.com/hibernate/hibernate_batch_processing.htm

There may be other solutions to this but one way I know is: 可能还有其他解决方案,但我知道的一种方法是:

Whenever you save or update an instance through session (eg session.save(), session.update(), session.saveOrUpdate() etc.) , it also updates the instance FK associations. 无论何时通过session (eg session.save(), session.update(), session.saveOrUpdate() etc.)保存或更新实例session (eg session.save(), session.update(), session.saveOrUpdate() etc.) ,它还会更新实例FK关联。

So if your POJO has multiple FK associations, it will fire queries on those tables as well. 因此,如果您的POJO具有多个FK关联,它也将触发对这些表的查询。

So instead of updating instance in this way, I would suggest to use HQL (if it applies to your requirement) to save or update instance. 因此,我建议使用HQL(如果它适用于您的要求)来保存或更新实例,而不是以这种方式更新实例。

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

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