Java Hibernate:动态更新动态插入 - 性能效果
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3404630/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Hibernate : dynamic-update dynamic-insert - Performance Effects
提问by Sandeep Jindal
Using dynamic-update or dynamic-insert has positive, though generally slight only on performance, as also mentioned by http://www.mkyong.com/hibernate/hibernate-dynamic-update-attribute-example/
使用 dynamic-update 或 dynamic-insert 有积极作用,但通常只对性能影响很小,正如http://www.mkyong.com/hibernate/hibernate-dynamic-update-attribute-example/ 所提到的
But the reference documentation mentions that this could have negative performance effects also as mentioned below in http://docs.jboss.org/hibernate/core/3.3/reference/en/html/mapping.html#mapping-declaration-class:
但是参考文档提到这也可能对性能产生负面影响,如下面的http://docs.jboss.org/hibernate/core/3.3/reference/en/html/mapping.html#mapping-declaration-class 所述:
Although these settings can increase performance in some cases, they can actually decrease performance in others.
尽管这些设置在某些情况下可以提高性能,但在其他情况下它们实际上会降低性能。
Can anybody please suggest some example/scenario mentioning negative performance impact of the same?
任何人都可以建议一些示例/场景提及相同的负面性能影响吗?
采纳答案by Pascal Thivent
Hibernate caches the actual INSERT/SELECT/UPDATE SQL strings for eachentity and the obvious benefit is that it doesn't have to compute the SQL when you want to persist, find or update an entity.
Hibernate 缓存每个实体的实际 INSERT/SELECT/UPDATE SQL 字符串,明显的好处是当您想要持久化、查找或更新实体时,它不必计算 SQL。
However, when using dynamic-insert or dynamic-update, Hibernate has to generate the corresponding SQL string each time and there is thus a performance cost on the Hibernate side.
但是,在使用动态插入或动态更新时,Hibernate 每次都必须生成相应的 SQL 字符串,因此在 Hibernate 端存在性能成本。
In other words, there is a trade-off between overhead on the database side and on the Hibernate side.
换句话说,在数据库端和 Hibernate 端的开销之间存在权衡。
My point of view is that dynamic insert and dynamic update can be interesting for tables with a fat blob column or tables with a huge number of columns. In other cases, I'm not convinced that dynamic insert or update always means performance boost (I do not use them by default). But as always, you should measure it.
我的观点是动态插入和动态更新对于具有胖 blob 列的表或具有大量列的表可能很有趣。在其他情况下,我不相信动态插入或更新总是意味着性能提升(默认情况下我不使用它们)。但与往常一样,你应该衡量它。
See also
也可以看看
- Re: Request for dynamic update-SQLfor some feedback from the Hibernate developers
- 回复:请求动态更新 SQL以获取 Hibernate 开发人员的一些反馈
回答by Erik Hart
I think many indices also slow down updates and inserts, so, beside large columns, dynamic-update should be good for tables with great width/content per row and many indices. You know, in "real life", databases aren't always with small, normalized tables...
我认为许多索引也会减慢更新和插入的速度,因此,除了大列之外,动态更新应该适用于每行宽度/内容很大且索引很多的表。你知道,在“现实生活”中,数据库并不总是有小的、规范化的表......
Rebuilding indices on large tables may take much longer than the overhead for creating and parsing SQL queries.
在大表上重建索引可能比创建和解析 SQL 查询的开销要长得多。
回答by codefly
The other reason is when updating a previously detached object. For that to work the record first needs to be fetched from the db, as a detached object isn't in the session cache. Hence dynamic update in this case requires an extra round trip to perform the initial fetch.
另一个原因是更新先前分离的对象时。为此,首先需要从数据库中获取记录,因为分离的对象不在会话缓存中。因此,这种情况下的动态更新需要额外的往返来执行初始提取。