These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 
Author Message
 Post subject: why use outer join instead of inner join for eager fetching?
PostPosted: Fri May 12, 2006 1:26 am 
Beginner
Beginner

Joined: Mon Sep 27, 2004 11:51 pm
Posts: 22
Hi ,

This may be a stupid question and discuss b4, l wonder why a lot ORMs use outer join instead of inner join for eager fetching ?

let's consider a scenario , l have three tables - Books , Authors and Publishers , their relationship are ..

Publishers <-- 1:M -->Books <-- M:M--> Authors

Let's assume it is a web application (library automation system) , and l have a search page "search book", there are three search criteria input boxs( bookTitle , authorName and publisherName) , and a submit button.


Scenarias :

1. if l leave the three input boxs blank and press the submit button , l expect -- A. display all books (may be it is not a good idea , but can be accepted if l limit it with maximum nos return) , or B. display none (this probably the most people choice)

2. if l fill the input box "authorName" a keyword , l hope to get a list books (a collections if more than one) with all authors and publishers "initialized".

Question : For scenario No.2 , we use "inner join" or "outer join" ?

if l choose "inner join" as eager loading both publisher and authors , then how l do it in hibernate ? oh ... we can use "inner join fetch" , there is an "inner join fetch" in Hibernate Reference Doc 3.1.3 , pg143.

Quote:
from Cat as cat inner join fetch cat.mate left join fetch cat.kittens


or

initialize() method describe in HIA or
"Ensuring Initialized Association Paths"http://www.javalobby.org/java/forums/t62077.html

if l choose "outer join" , the result is not what l want , it will alway return ALL books for me whether l fill in either publisherName or authorName and leave bookTitle blank ...

this seem to be a common use for "inner eager fetching" than "outer eager fetching" ....

l am confusing ...... any one can help me ?

moon


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 12, 2006 3:34 am 
Expert
Expert

Joined: Thu Sep 22, 2005 10:29 am
Posts: 285
Location: Almassera/Valencia/Spain/EU/Earth/Solar system/Milky Way/Local Group/Virgo Supercluster
I'd recommend you reading a good book about SQL.

You could try:
SQL: Practical Guide for Developers


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 12, 2006 6:21 pm 
Beginner
Beginner

Joined: Mon Sep 27, 2004 11:51 pm
Posts: 22
Quote:
if l choose "outer join" , the result is not what l want , it will alway return ALL books for me whether l fill in either publisherName or authorName and leave bookTitle blank ...


no no ... this is my mistake .... it is not true that it will return ALL books.

Let's try a simple example , here are my tables ,

Code:

authors           link_b_a             books               publishers
-------           ---------            -------              -----------
author_id(pk)     author_id(pk)        book_id(pk)
author_name       book_id(pk)          title               publisher_name
                                       publisher_id(fk)    publisher_id(pk)

----------------------- with data below ---------------------------------

1  author1         1  1              1   book1  1           1   publisher1
2  author2         1  2              2   book2  1           2   publisher2
3  author3         2  1              3   book3  2           3   publisher3
                   2  2              4   book4  null
                   3  2



this is outer join :

Quote:
SELECT * FROM books b
left outer join link_b_a ba on b.book_id=ba.book_id
left outer join authors a on ba.author_id=a.author_id

it return :

Quote:
"BOOK_ID","TITLE","PUBLISHER_ID","BOOK_ID","AUTHOR_ID","AUTHOR_ID","AUTHOR_NAME"
1,"book1",1,1,1,1,"author1"
1,"book1",1,1,2,2,"author2"
2,"book2",1,2,1,1,"author1"
2,"book2",1,2,2,2,"author2"
2,"book2",1,2,3,3,"author3"
3,"book3",2,,,,""
4,"book4",,,,,""


this is inner join :

Quote:
SELECT * FROM books b
inner join link_b_a ba on b.book_id=ba.book_id
inner join authors a on ba.author_id=a.author_id


it return:
Quote:
"BOOK_ID","TITLE","PUBLISHER_ID","BOOK_ID","AUTHOR_ID","AUTHOR_ID","AUTHOR_NAME"
1,"book1",1,1,1,1,"author1"
2,"book2",1,2,1,1,"author1"
1,"book1",1,1,2,2,"author2"
2,"book2",1,2,2,2,"author2"
2,"book2",1,2,3,3,"author3"


The above simple example showing that l "do" know the different between inner join and outer join , : ) .. hihi

.... to be continue

moon


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 12, 2006 7:07 pm 
Beginner
Beginner

Joined: Mon Sep 27, 2004 11:51 pm
Posts: 22
before come to this thread , l found a thread http://saloon.javaranch.com/cgi-bin/ubb ... 8&t=001062 discuss the different between inner join and outer join , but now let's continue my problem ...

let's do a search by author_name (scenario no2.) , what will happen between outer join and inner join ?

1. outer join:
Quote:
SELECT * FROM books b
left outer join link_b_a ba on b.book_id=ba.book_id
left outer join authors a on ba.author_id=a.author_id
where a.author_name like '%author1%';


results (2 nos):
Quote:
"BOOK_ID","TITLE","PUBLISHER_ID","BOOK_ID","AUTHOR_ID","AUTHOR_ID","AUTHOR_NAME"
1,"book1",1,1,1,1,"author1"
2,"book2",1,2,1,1,"author1"


2. inner join:

Quote:
SELECT * FROM books b
inner join link_b_a ba on b.book_id=ba.book_id
inner join authors a on ba.author_id=a.author_id
where a.author_name like '%author1%';


result (2 nos) :
Quote:
"BOOK_ID","TITLE","PUBLISHER_ID","BOOK_ID","AUTHOR_ID","AUTHOR_ID","AUTHOR_NAME"
1,"book1",1,1,1,1,"author1"
2,"book2",1,2,1,1,"author1"


yes , you will get the *same* results .

This seem to imply that the criterias after the "where" clause make no different for the outer and inner join ! (right ?) it make different if we don't use where clause , as the example in the last post ....

For my search books page , if l hope to display

| title | author_name | publisher_name | (as the report data or whatever)

using an "eager fetch" to retrieve the book collections with authors and publishers initialized (may be add some criterias after the where clause , like publisher_name or author_name) , what would l use ? inner or outer ?

this seem both is OK , but since l consider the performance cost ....

Quote:
SQL Performance Tuning,

An outer join can be much slower than an inner join because Table1 must be the outer table and Table2 must be the inner table otherwise, the join won't work. So even if Table1 is smaller and better indexed and has a restrictive expression, it still can't be the inner table. And that means the DBMS can't pick the optimum join plan.


why not using inner join as eager fetching instead of outer ?

l must be missing something ! because simple logic telling me that those ORM guys are smarter than me ....hihihi

somebody help ~~~

* yes , l do know that the HIA tell us to use the method described in pg 262~265 (the Object[] array method) , but why using the "array" instead of "object" ?

moon


Top
 Profile  
 
 Post subject:
PostPosted: Sat May 13, 2006 9:48 pm 
Expert
Expert

Joined: Thu Sep 22, 2005 10:29 am
Posts: 285
Location: Almassera/Valencia/Spain/EU/Earth/Solar system/Milky Way/Local Group/Virgo Supercluster
Try with

Quote:
where b.title like '%book4%';


do you see it now?


Top
 Profile  
 
 Post subject:
PostPosted: Sun May 14, 2006 12:38 am 
Beginner
Beginner

Joined: Mon Sep 27, 2004 11:51 pm
Posts: 22
Aha .... , thanks ~ pepelnm.

this is the reason they use outer join instead of inner join , let's write it down for someone who have the same stupid question as l.

outer join :

Quote:
SELECT * FROM books b
left outer join link_b_a ba on b.book_id=ba.book_id
left outer join authors a on ba.author_id=a.author_id
left outer join publishers p on b.publisher_id=p.publisher_id
where b.title like '%book4%';


result (1 no) :

Quote:
"BOOK_ID","TITLE","PUBLISHER_ID","BOOK_ID","AUTHOR_ID","AUTHOR_ID","AUTHOR_NAME","PUBLISHER_ID","PUBLISHER_NAME"
4,"book4",,,,,"",,""


inner join :
Quote:
SELECT * FROM books b
inner join link_b_a ba on b.book_id=ba.book_id
inner join authors a on ba.author_id=a.author_id
inner join publishers p on b.publisher_id=p.publisher_id
where b.title like '%book4%';


result (0 no) :
none

this is indeed not a trivial answer for a sql beginner , although l understand what is inner and outer join is ....

if this example can be a "exercise" for those ORM doc / book , l think it will help a lot beginner as l .... : )

Now , let's proceed to some other derived questions -

1. Is this the only reason that they choice "outer" instead of "inner" ?

2. If the books table (or other tables) can ensure the non-nullable value , can l assert that the inner join is same as outer join again , and the outer join can be replaced by inner join (performance consideration) ?

moon


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.