gpt4 book ai didi

hibernate - Criteria 与 HQL。谁更快?

转载 作者:行者123 更新时间:2023-12-03 06:54:55 30 4
gpt4 key购买 nike

我已经阅读了一些答案,但我仍然很困惑。为什么?因为你提到的差异与性能无关。它们与易于使用有关。(Objetc(criteria)和SQL(hql))。但我想知道“criteria”是否由于某种原因比 hql 慢。

我在另一个答案中读到了这个

“HQL 和 criteriaQuery 之间在性能方面存在差异,每次使用 criteriaQuery 触发查询时,它都会为表名创建一个新别名,该别名不会反射(reflect)在任何数据库的最后一个查询缓存中。这会导致编译生成的 SQL 的开销,需要更多时间来执行。”作者:瓦伦·梅塔。

这非常接近但是!我在另一个网站上读到(http://gary-rowe.com/agilestack/tag/hibernate/)Hibernate 3.3及以上版本不再是这种情况(请阅读:9)Hibernate很慢,因为由Criteria接口(interface)不一致)

我做了一些测试,试图找出差异,但两者都会生成 qry,并且不会更改表的别名。

我很困惑。如果有人知道主要原因,请帮助我们。谢谢

最佳答案

总体而言,如果您使用 HQL 或 Criteria 创建最终 SQL,您在 Hibernate 的更高版本(版本 3.3 及更高版本)中不会发现性能有太大差异。

要测试这一点,您需要在 HQL 中并使用 Criteria 界面创建一个代表性查询。然后记录逐渐旧版本的 Hibernate 生成的 SQL(可能使用 Maven 以允许快速版本更改)。您会注意到,随着 Hibernate 版本的减少,您将看到最终 SQL 的变化。

尝试在代码中的 HQL 和 Criteria 之间进行优化没有什么意义,因为绝大多数时间损失将发生在应用程序和数据库之间的网络流量上。当然,这假设您有一个格式良好的查询,不需要对超过一亿行或其他内容进行多次全表扫描。

quoted blog is attempting to debunk myths所以要小心,不要断章取义(强调我的标题):

9) "Hibernate is slow because the SQL generated by the Criteria interface is not consistent"

It has been said that Hibernate can also incur a performance hit if all queries are constructed through the Criteria interface instead of directly in HQL. The argument posits that this is because every time the query builder code is executed, say in a DAO, Hibernate will generate new alias names for the tables in the query. In Oracle this means that every time a new Criteria-based query is run, the database must create a QEP – query execution plan – as it is unable to match the SQL it has been given to any in its cache. Creating the QEP can take 30% of the time it takes for Oracle to respond to a SQL statement, so for the second and subsequent executions of the same (but for alias names) SQL statement, Criteria has a built-in overhead that makes it 50% slower than direct HQL.

This is no longer the case with Hibernate 3.3 and above. If it was ever true at all is in doubt since the Hibernate team would certainly aim to create optimal SQL wherever possible. Independent tests demonstrate that the same query is generated by the Criteria interface after repeated calls spanning transactions which is the equivalent of running the application under load. In each case the query remained identical and was therefore able to be cached by Oracle.

There is one grain of truth, though, in that it is necessary to create the query every time using the Criteria interface, whereas using named queries defined in HQL allows for precompilation during application start-up. However, this needs some perspective. The time taken to create a simple “between” query using the Criteria interface is approximately 3ms on an average PC. Embedding HQL within the application is not a good alternative since it does not lead to an intuitive mechanism for maintaining queries with differing fetching strategies, and so the Criteria based approach is deemed the better of the two.

因此该博客本质上指出,主要区别在于 Criteria 接口(interface)可能会引入几毫秒的额外处理开销,这可以通过直接 HQL 方法来避免。 HQL 的查询表达也更加简洁,许多人会觉得很有吸引力。

简而言之,差异很小,您无需担心。

关于hibernate - Criteria 与 HQL。谁更快?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4444045/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com