Hibernate抓取策略 - 何时使用“加入”以及何时使用“选择”? [英] Hibernate fetching strategy - when to use "join" and when to use "select"?

查看:110
本文介绍了Hibernate抓取策略 - 何时使用“加入”以及何时使用“选择”?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大多数Hibernate关联支持fetch参数:
$ b $ pre $ fetch =join | select

,其中select为默认值。



如何决定使用哪一个关联?



我尝试将所有选项从select更改为加入应用程序范围 - 生成的查询数量减少了10倍,但性能保持完全相同(甚至变得更糟)。



谢谢。

解决方案

1问题。如果你有10个父母,每个有10个孩子,加入将需要一个查询,选择需要11个(父母一个,每个父母的孩子一个)。如果数据库与应用程序位于同一台服务器上,或者网络速度非常快,但这可能不是什么大问题,但如果每个数据库调用都存在延迟,则可能会加起来。连接方法在初始查询时效率稍低一些,因为您要复制每一行中的父列,但您只需往返一次数据库。



通常,如果我知道我将需要所有父母的孩子,我会加入。如果我只需要一些父母的孩子,我就用select。


Most Hibernate associations support "fetch" parameter:

fetch="join|select"

with "select" being default value.

How to decide which one to use for which association?

I tried changing all from "select" to "join" application wide - number of generated queries decreased probably 10 times but performance stayed exactly the same (even become a tiny bit worse).

Thanks.

解决方案

Join is supposed to solve the n+1 problem. If you have 10 parents, each with 10 children, join will require one query and select will require 11 (one for the parents and one for the children of each parent). This may not be a big deal if the database is on the same server as the application or if the network is really fast, but if there is latency in each database call, it can add up. The join method is a little less efficient on the initial query because you're duplicating the parent columns in every row, but you only make one round-trip to the database.

Generally, if I know I'm going to need the children of all the parents, I go with join. If I'm only going to need the children of a few parents, I use select.

这篇关于Hibernate抓取策略 - 何时使用“加入”以及何时使用“选择”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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