serg
serg

Reputation: 111285

Hibernate fetching strategy - when to use "join" and when to use "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.

Upvotes: 49

Views: 49891

Answers (6)

Daedalus
Daedalus

Reputation: 1667

JOIN is preferred, typically, for performance reasons.

The one reason to use SELECT is if you're paging results (setting an offset and a limit) that have a many-to-many relationship. If you use JOIN, the root entity will appear multiple times if it contains several many-to-many children and those "copies" count against your limit (even if Hibernate collapses them after the fact using DISTINCT_ROOT_ENTITY).

Upvotes: 2

MullaBihari
MullaBihari

Reputation: 61

If the parent has lots of children and those children in turn have many other, then in this case the initial 'join' might choke the network. My suggestion is to use 'select' in this case to split the selects.

Upvotes: 2

Jagadeesh
Jagadeesh

Reputation: 2800

People are always talk about performance hit using fetch=JOIN. But as i reckon, it is important for us to understand the number of parent/child records we are fetching:

If you want to fetch only single Parent record and expecting it doesn't have many children, then i would suggest you to use fetch=SELECT.

If you want to fetch all parent records including its children, then it would be better to go for fetch=JOIN

Just to add a note that, if records are lazily fetching children(lazy=true), then it wouldn't make any sense of using fetch=JOIN since the all the parent and child records gets loaded in a single shot.

Upvotes: 0

user528050
user528050

Reputation: 157

fetching="join" If you do fetching="join" it will retrive all the information in a single select statement.

fetching="select" if you want to paas the second select statement to fetch the associated collection than in that case you will use fetch="select".

source :Hibernate Fetching Strategies

Upvotes: 1

Brian Deterling
Brian Deterling

Reputation: 13724

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.

Upvotes: 42

jdmichal
jdmichal

Reputation: 11162

Select will fetch child items by issuing a new query to the database for them. Join will fetch child items by joining them into the parent's query. So that's why you're seeing similar performance, even with a drop in number of queries.

Select:

SELECT * FROM parent WHERE id=(whatever)
SELECT * FROM child WHERE id=(parent.child.id)

Join:

SELECT *
FROM parent
LEFT OUTER JOIN child ON parent.child.id=child.id
WHERE parent.id=(whatever)

As to when to use one over the other... Not entire sure. It likely depends on the database system. If one was always better than the other, I doubt they would bother to give you the option! If you're seeing similar performance for each, I wouldn't worry about it.

Upvotes: 9

Related Questions