常见的休眠表现陷阱 [英] Usual hibernate performance pitfall

查看:65
本文介绍了常见的休眠表现陷阱的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们刚完成了剖析我们的应用程序。 (她开始变慢)。
问题似乎是在休眠。

这是一个传统的映射。谁工作,做这个工作。后面的关系式shema也可以。



但是有些要求很慢。



因此,我们希望对常见和常见的错误提出任何意见,这些错误最终会导致响应缓慢。



例:渴望代替懒惰可以戏剧性地改变响应时间....






编辑:像往常一样,阅读手册通常是一个好主意。整个章节涵盖了这个主题:

http://docs.jboss.org/hibernate/core/3.3/reference/en/html/performance.html

解决方案

最常见的陷阱之一是臭名昭着的 n + 1选择问题 。默认情况下,Hibernate不加载你没有要求的数据。这可以减少内存消耗,但会让您接触到n + 1选择问题,您可以通过切换到正确的获取策略来检索所需的所有数据,以便以适当的初始化状态加载对象,从而避免这种情况。



但是也不要太多,否则会遇到相反的问题,即笛卡尔产品问题:不是执行许多SQL语句,而是最终创建语句检索太多的数据。



这就是整个调整的重点:找到应用程序每个用例中没有足够的和太多数据的中间点(或者至少,这是需要调整的那个)。



我的建议是:


  • 首先在Hibernate的级别激活SQL日志记录
  • 运行关键用例(80%的时间使用20%),或者如果你有这种奢侈的话,甚至可以全部使用$ / $>
  • 识别可疑查询并优化提取计划,检查索引使用恰当,等等

  • 涉及您的DBA


We have just finish to profile our application. ( she's begin to be slow ). the problem seems to be "in hibernate".

It's a legacy mapping. Who work's, and do it's job. The relational shema behind is ok too.

But some request are slow as hell.

So, we would appreciate any input on common and usual mistake made with hibernate who end up with slow response.

Exemple : Eager in place of Lazy can change dramaticly the response time....


Edit : As usual, read the manual is often a good idea. A whole chapter cover this subject here :

http://docs.jboss.org/hibernate/core/3.3/reference/en/html/performance.html

解决方案

One of the most common pitfalls is the infamous n+1 selects problem. By default, Hibernate doesn't load data you didn't ask for. This reduces memory consumptions but exposes you to the n+1 selects problem which can be avoided by switching to the right fetching strategy to retrieve all the data you need to load objects in their appropriately initialized state.

But also don't fetch too much or you'll run into the opposite problem, the cartesian product issue: instead of executing to many SQL statements, you may end up creating statements that retrieve too much data.

That's the whole point of the tuning: finding the middle between not enough and too much data for each use case of your application (or at least, for the one that require to be tuned).

My recommendations:

  • first activate SQL logging at Hibernate's level
  • run the critical use cases (the 20% used 80% of the time) or even all of them if you have that luxury
  • identify suspicious queries and optimize the fetch plan, check if indices are used appropriately, etc
  • involve your DBA

这篇关于常见的休眠表现陷阱的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆