休眠性能 [英] Hibernate performance

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

问题描述

我有问题,可能来自某人认为是愚蠢的。休眠快吗?我在系统中使用它,对数据库的查询真的很大。性能会提醒。
和其他问题脱离当前的情况。什么会更好 - 许多简单的查询(使用单个表格)还是使用几个JOIN查询少一点?

坦克预付
Artem

解决方案

许多查询,你正在遇到典型的 N + 1查询问题。你用一个带有相关对象的Hibernate加载一个实体。随着LAZY加入,您将得到每个记录的单个查询。每个查询都通过网络传输到数据库服务器,并返回数据。这一切都需要时间(开放连接,网络延迟,查找,数据吞吐量等)。

对于单个查询(连接),查找和数据吞吐量较大比多个查询。但是,您只需要打开一次连接和网络延迟。因此,对于100个或更多的查询,您可以进行小的查找和数据吞吐量,但是您将拥有100次(包括打开连接和网络延迟)。

单个查询需要20ms。与100个需要1ms的查询。你做数学;)



如果它可以增长到1000的记录。单个查询的性能影响很小,但是对于100个查询的数量是1000个,是10倍。因此,对于多个查询,您将极大地降低性能。



使用HQL查询检索数据时,您可以为一个JOIN添加FETCH 以便用相同的查询加载数据(使用JOIN) / p>

有关此主题的更多信息,请查看 Hibernate Performance Tuning Tutorial。


I have question, maybe from someone it see as stupid. Is Hibernate fast? I use it in system which would have really large count of queries to database. And performance become alerted. And other question off the point of current context. What would better - many simple query (with single table) or a bit less query with several JOIN?

Tanks for advance Artem

解决方案

With "many" queries, you are meeting with the typical N+1 query problem. You load an Entity with Hibernate, which has related objects. With LAZY joins, you'll get a single query for every record. Each query goes through the network to your database server, and it returns the data. This all takes time (opening connection, network latency, lookup, data throughput, etc.).

For a single query (with joins) the lookup and data throughput are larger than with multiple queries. But you'll only have the opening of the connection and network latency just once. So with 100 or more queries you have a small lookup and data throughput, but you will have it 100 times (including opening the connection and network latency).

A single query that takes 20ms. vs 100 queries that take 1ms.? You do the math ;)

And if it can grow to be 1000's of records. The single query will have a small performance impact, but 1000's of queries vs 100's are 10 times more. So with multiple queries, you'll have reduced the performance greatly.

When using HQL queries to retrieve the data, you can add FETCH to a JOIN in order to load the data with the same query (using JOIN's).

For more info related to this topic, check out this Hibernate Performance Tuning Tutorial.

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

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