在休眠本机查询或HQL中哪个性能更好 [英] Which is better performance in hibernate native query or HQL

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

问题描述

在服务器端代码中,通常为了获得更好的性能,我们不应该使用从表中选择*",而应该根据需要查询必要的列(选择名称,从员工中添加).我阅读了数据库性能指南文章.

In the server side code generally for better performance we should not use "select * from table" rather we should query the necessary column according to the need (Select name, add from employee).This i read in data base performance guidlines article.

现在我对休眠有了疑问,我读到最好在休眠中使用session.load(id)来基于主键检索记录.这将检索与给定"id"(表中的记录)相关联的与实体相关的所有列.

Now i have quetion with hibernate, i read that its better to use session.load(id) in hibernate to retreive the record based on the primary key. This will retreive all the column associated to the entity for the given 'id' (a record from table).

现在它与通用数据库性能指南没有矛盾吗?休眠的本机sql查询或休眠的查询语言哪个性能更好?

Now is it not contradicting the general data base performance guidline. which is better performance with hibernate native sql query or hibernate query language?

让我知道您的宝贵意见,因为我正在尝试调整我的代码以提高性能.

Let me know your valuable inputs as im trying to tune my code for better performance.

推荐答案

您正在弄乱某些部分.您也可以仅选择带有HQL的某些列,例如,可以在HQL中使用select column from table.

You're messing up some parts. You can select only certain columns with HQL, too, for example you can use select column from table in HQL.

本地SQL不一定比HQL快.最后,HQL还将转换为SQL(在show_sql属性设置为true的情况下运行应用程序时,您可以看到生成的语句).在某些情况下,Hibernate不会生成最有效的语句,因此本机SQL可能会更快-但是使用本机SQL,您的应用程序会失去从一个数据库到另一个数据库的可移植性,因此通常最好调整Hibernate映射和HQL语句以生成更有效的SQL语句.另一方面,对于本机SQL,您缺少Hibernate缓存-因此,在某些情况下,本机SQL可能比HQL慢.

Native SQL is not necessarily faster than HQL. HQL finally also is translated into SQL (you can see the generated statement when running the application with the show_sql property set to true). In some cases it can happen Hibernate does not generate the most efficient statements, so then native SQL can be faster - but with native SQL your application loses the portability from one database to another, so normally is better to tune the hibernate mapping and the HQL statement to generate more efficient SQL statements. On the other side with native SQL you're missing the Hibernate cache - as a consequence in some cases native SQL can be slower than HQL.

当您使用session.load(class, id)且行尚未在缓存中时,加载也会生成一个select * from classTable,因此速度与HQL from相同. (但是,当对象已经在缓存中时,load可能会更快.)

When you use session.load(class, id) and the row is not in the cache yet, the load also generates a select * from classTable, so the speed is same to the HQL from. (But when the object already is in the cache, then probably load is faster.)

我不同意您的性能指南:在大多数情况下,对于性能而言,仅加载所有列还是仅加载所需的列都是无关紧要的.在数据库访问中,搜索行会浪费时间,而将数据传输到应用程序中则会丢失时间.当您仅读取必要的列时,它具有以下缺点:

I disagree with your performance guidelines: In most cases for the performance it is irrelevant if your load all columns or only the needed columns. In database access the time is lost when searching the row, and not when transferring the data into your application. When you read only the necessary columns, it has the following disadvantages:

  • 当您需要尚未加载的列时,更改应用程序会遇到更多麻烦(或者必须再次加载该行,这意味着性能较差).
  • 这给您的应用程序带来了糟糕的设计(Hibernate更喜欢一个表-一个类)
  • 它在Hibernate缓存中不能很好地工作.

(以为,如果您的应用程序中不需要某些列,或者在应用程序完成后将添加的列,那么您就不必将它们放入类和映射中,而它们永远不会加载,并且您的应用程序设计仍然很好.Hibernate不会生成select * from table语句,而是始终生成select col1, col2, ... from table.)

(Thought, if there are columns which you never need in your application, or for columns which will be added after your application is finished, then you just don't put them into your class and your mapping, and they never will be loaded, and your application design still is good. Hibernate does not generate select * from table statements, it always generates select col1, col2, ... from table.)

有一个例外:如果您加载大量数据(数千行),那么仅加载必要的列会更快.

There is one exception: If you load huge amounts of data - thousands of rows - then loading only the necessary columns can be significantly faster.

这篇关于在休眠本机查询或HQL中哪个性能更好的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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