Complex JOIN vs multiple selects

This is a discussion on Complex JOIN vs multiple selects within the MySQL Database forums, part of the Database Forums category; Hi, if I have 3 or more table all 'linked' by an ID. Is it better to have one query ...


Go Back   Usenet Forums > Database Forums > MySQL Database

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 01-17-2008
FFMG
 
Posts: n/a
Default Complex JOIN vs multiple selects


Hi,

if I have 3 or more table all 'linked' by an ID.
Is it better to have one query or 3 or more SELECT?

For example

// -- EXAMPLE A
SELECT something FROM t1 LEFT JOIN t2 ON t2.id=t1.id LEFT JOIN t3 ON
t3.id=t1.id WHERE t1.id=xx

// -- EXAMPLE B
SELECT something FROM t1 WHERE t1.id=xx
...
SELECT something FROM t2 WHERE t2.id = $t1id
SELECT something FROM t3 WHERE t3.id = $t1id

From the DB point of view does it really matter?
Doesn't MySQL break EXAMPLE A into EXAMPLE B?

And if I was to put it all into a stored procedure, should I keep the
LEFT JOIN or should I break it down into multiple selects?

I guess what I am asking is, does MySQL really care how I present it as
it will be hitting all 3 tables the same way in the end.

Thanks

FFMG


--

'webmaster forum' (http://www.httppoint.com) | 'Free Blogs'
(http://www.journalhome.com/) | 'webmaster Directory'
(http://www.webhostshunter.com/)
'Recreation Vehicle insurance'
(http://www.insurance-owl.com/other/car_rec.php) | 'Free URL
redirection service' (http://urlkick.com/)
------------------------------------------------------------------------
FFMG's Profile: http://www.httppoint.com/member.php?userid=580
View this thread: http://www.httppoint.com/showthread.php?t=24324

Message Posted via the webmaster forum http://www.httppoint.com, (Ad revenue sharing).

Reply With Quote
  #2 (permalink)  
Old 01-17-2008
Captain Paralytic
 
Posts: n/a
Default Re: Complex JOIN vs multiple selects

On 17 Jan, 06:50, FFMG <FFMG.33b...@no-mx.httppoint.com> wrote:
> Hi,
>
> if I have 3 or more table all 'linked' by an ID.
> Is it better to have one query or 3 or more SELECT?
>
> For example
>
> // -- EXAMPLE A
> SELECT something FROM t1 LEFT JOIN t2 ON t2.id=t1.id LEFT JOIN t3 ON
> t3.id=t1.id WHERE t1.id=xx
>
> // -- EXAMPLE B
> SELECT something FROM t1 WHERE t1.id=xx
> ..
> SELECT something FROM t2 WHERE t2.id = $t1id
> SELECT something FROM t3 WHERE t3.id = $t1id
>
> From the DB point of view does it really matter?
> Doesn't MySQL break EXAMPLE A into EXAMPLE B?
>
> And if I was to put it all into a stored procedure, should I keep the
> LEFT JOIN or should I break it down into multiple selects?
>
> I guess what I am asking is, does MySQL really care how I present it as
> it will be hitting all 3 tables the same way in the end.
>
> Thanks
>
> FFMG
>
> --
>
> 'webmaster forum' (http://www.httppoint.com) | 'Free Blogs'
> (http://www.journalhome.com/) | 'webmaster Directory'
> (http://www.webhostshunter.com/)
> 'Recreation Vehicle insurance'
> (http://www.insurance-owl.com/other/car_rec.php) | 'Free URL
> redirection service' (http://urlkick.com/)
> ------------------------------------------------------------------------
> FFMG's Profile:http://www.httppoint.com/member.php?userid=580
> View this thread:http://www.httppoint.com/showthread.php?t=24324
>
> Message Posted via the webmaster forumhttp://www.httppoint.com, (Ad revenue sharing).


A JOINed query allows the optimiser to work out the best way to get
all the information. It also saves on the connection, transmission and
setup time overhead for each query.
Reply With Quote
  #3 (permalink)  
Old 01-18-2008
Willem Bogaerts
 
Posts: n/a
Default Re: Complex JOIN vs multiple selects

>> Is it better to have one query or 3 or more SELECT?


> A JOINed query allows the optimiser to work out the best way to get
> all the information. It also saves on the connection, transmission and
> setup time overhead for each query.


And on the other hand, the join could cause some very large blob field
to be transmitted for each row, whereas 3 queries would do this only
once. So I guess the best answer is a bit situation-dependent.

Regards,
--
Willem Bogaerts

Application smith
Kratz B.V.
http://www.kratz.nl/
Reply With Quote
  #4 (permalink)  
Old 01-18-2008
Jerry Stuckle
 
Posts: n/a
Default Re: Complex JOIN vs multiple selects

Willem Bogaerts wrote:
>>> Is it better to have one query or 3 or more SELECT?

>
>
>> A JOINed query allows the optimiser to work out the best way to get
>> all the information. It also saves on the connection, transmission and
>> setup time overhead for each query.

>
> And on the other hand, the join could cause some very large blob field
> to be transmitted for each row, whereas 3 queries would do this only
> once. So I guess the best answer is a bit situation-dependent.
>
> Regards,


And why would that be, Willem?

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

Reply With Quote
  #5 (permalink)  
Old 01-18-2008
Willem Bogaerts
 
Posts: n/a
Default Re: Complex JOIN vs multiple selects

>> And on the other hand, the join could cause some very large blob field
>> to be transmitted for each row, whereas 3 queries would do this only
>> once. So I guess the best answer is a bit situation-dependent.
>>
>> Regards,

>
> And why would that be, Willem?


If you have, for instance a book table and an author table, and a book
is written by more than one author, you could write:

SELECT book.content, author.name FROM book NATURAL JOIN author WHERE
book.id=5;

This would return the entire content of the book for each author
(far-fetched example, I know). Whereas:

SELECT content, @author_id:=authorId FROM book WHERE id=5;
SELECT author.name FROM author WHERE id=@author_id;

Would pass the content only once. So less data would go over the line,
but effectively the exact amount of data is given, because the duplicate
fields are removed.

Or am I wrong in this?

Regards,
--
Willem Bogaerts

Application smith
Kratz B.V.
http://www.kratz.nl/
Reply With Quote
  #6 (permalink)  
Old 01-18-2008
Captain Paralytic
 
Posts: n/a
Default Re: Complex JOIN vs multiple selects

On 18 Jan, 12:34, Willem Bogaerts
<w.bogae...@kratz.maardanzonderditstuk.nl> wrote:
> >> And on the other hand, the join could cause some very large blob field
> >> to be transmitted for each row, whereas 3 queries would do this only
> >> once. So I guess the best answer is a bit situation-dependent.

>
> >> Regards,

>
> > And why would that be, Willem?

>
> If you have, for instance a book table and an author table, and a book
> is written by more than one author, you could write:
>
> SELECT book.content, author.name FROM book NATURAL JOIN author WHERE
> book.id=5;
>
> This would return the entire content of the book for each author
> (far-fetched example, I know). Whereas:
>
> SELECT content, @author_id:=authorId FROM book WHERE id=5;
> SELECT author.name FROM author WHERE id=@author_id;
>
> Would pass the content only once. So less data would go over the line,
> but effectively the exact amount of data is given, because the duplicate
> fields are removed.
>
> Or am I wrong in this?
>
> Regards,
> --
> Willem Bogaerts
>
> Application smith
> Kratz B.V.http://www.kratz.nl/


If book.id = 5 applied to more than one book then,
SELECT content, @author_id:=authorId FROM book WHERE id=5;
whould give you the content for every book where the id=5

Can't see the difference myself.
Reply With Quote
  #7 (permalink)  
Old 01-19-2008
Michael Austin
 
Posts: n/a
Default Re: Complex JOIN vs multiple selects

Captain Paralytic wrote:
> On 18 Jan, 12:34, Willem Bogaerts
> <w.bogae...@kratz.maardanzonderditstuk.nl> wrote:
>>>> And on the other hand, the join could cause some very large blob field
>>>> to be transmitted for each row, whereas 3 queries would do this only
>>>> once. So I guess the best answer is a bit situation-dependent.
>>>> Regards,
>>> And why would that be, Willem?

>> If you have, for instance a book table and an author table, and a book
>> is written by more than one author, you could write:
>>
>> SELECT book.content, author.name FROM book NATURAL JOIN author WHERE
>> book.id=5;
>>
>> This would return the entire content of the book for each author
>> (far-fetched example, I know). Whereas:
>>
>> SELECT content, @author_id:=authorId FROM book WHERE id=5;
>> SELECT author.name FROM author WHERE id=@author_id;
>>
>> Would pass the content only once. So less data would go over the line,
>> but effectively the exact amount of data is given, because the duplicate
>> fields are removed.
>>
>> Or am I wrong in this?
>>
>> Regards,
>> --
>> Willem Bogaerts
>>
>> Application smith
>> Kratz B.V.http://www.kratz.nl/

>
> If book.id = 5 applied to more than one book then,
> SELECT content, @author_id:=authorId FROM book WHERE id=5;
> whould give you the content for every book where the id=5
>
> Can't see the difference myself.


SELECT book.content, author.name FROM book NATURAL JOIN author WHERE
book.id=5;

This is valid syntax, however, if you are not also restricting your
query to include a single author, it shows a very poor working knowledge
of your data does it not?

onedbguru
Reply With Quote
  #8 (permalink)  
Old 01-19-2008
Jerry Stuckle
 
Posts: n/a
Default Re: Complex JOIN vs multiple selects

Willem Bogaerts wrote:
>>> And on the other hand, the join could cause some very large blob field
>>> to be transmitted for each row, whereas 3 queries would do this only
>>> once. So I guess the best answer is a bit situation-dependent.
>>>
>>> Regards,

>> And why would that be, Willem?

>
> If you have, for instance a book table and an author table, and a book
> is written by more than one author, you could write:
>
> SELECT book.content, author.name FROM book NATURAL JOIN author WHERE
> book.id=5;
>


Why would it? You asked for book.id = 5. It will return one row
(assuming book.id is a unique field).

> This would return the entire content of the book for each author
> (far-fetched example, I know). Whereas:
>
> SELECT content, @author_id:=authorId FROM book WHERE id=5;
> SELECT author.name FROM author WHERE id=@author_id;
>
> Would pass the content only once. So less data would go over the line,
> but effectively the exact amount of data is given, because the duplicate
> fields are removed.
>
> Or am I wrong in this?
>
> Regards,


You are incorrect. In fact, the second one will require two calls to
the database to retrieve the same information the first one did in one call.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

Reply With Quote
  #9 (permalink)  
Old 01-19-2008
Jerry Stuckle
 
Posts: n/a
Default Re: Complex JOIN vs multiple selects

Michael Austin wrote:
> Captain Paralytic wrote:
>> On 18 Jan, 12:34, Willem Bogaerts
>> <w.bogae...@kratz.maardanzonderditstuk.nl> wrote:
>>>>> And on the other hand, the join could cause some very large blob field
>>>>> to be transmitted for each row, whereas 3 queries would do this only
>>>>> once. So I guess the best answer is a bit situation-dependent.
>>>>> Regards,
>>>> And why would that be, Willem?
>>> If you have, for instance a book table and an author table, and a book
>>> is written by more than one author, you could write:
>>>
>>> SELECT book.content, author.name FROM book NATURAL JOIN author WHERE
>>> book.id=5;
>>>
>>> This would return the entire content of the book for each author
>>> (far-fetched example, I know). Whereas:
>>>
>>> SELECT content, @author_id:=authorId FROM book WHERE id=5;
>>> SELECT author.name FROM author WHERE id=@author_id;
>>>
>>> Would pass the content only once. So less data would go over the line,
>>> but effectively the exact amount of data is given, because the duplicate
>>> fields are removed.
>>>
>>> Or am I wrong in this?
>>>
>>> Regards,
>>> --
>>> Willem Bogaerts
>>>
>>> Application smith
>>> Kratz B.V.http://www.kratz.nl/

>>
>> If book.id = 5 applied to more than one book then,
>> SELECT content, @author_id:=authorId FROM book WHERE id=5;
>> whould give you the content for every book where the id=5
>>
>> Can't see the difference myself.

>
> SELECT book.content, author.name FROM book NATURAL JOIN author WHERE
> book.id=5;
>
> This is valid syntax, however, if you are not also restricting your
> query to include a single author, it shows a very poor working knowledge
> of your data does it not?
>
> onedbguru
>


No, it doesn't. It just means the book.id is a unique key. It's very
common to do something like this - i.e. show a list of books on a topic
and let the user pick one. The unique id is used to retrieve the data.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

Reply With Quote
  #10 (permalink)  
Old 01-19-2008
Paul Lautman
 
Posts: n/a
Default Re: Complex JOIN vs multiple selects

Michael Austin wrote:
> Captain Paralytic wrote:
>> On 18 Jan, 12:34, Willem Bogaerts
>> If book.id = 5 applied to more than one book then,
>> SELECT content, @author_id:=authorId FROM book WHERE id=5;
>> whould give you the content for every book where the id=5
>>
>> Can't see the difference myself.

>
> SELECT book.content, author.name FROM book NATURAL JOIN author WHERE
> book.id=5;
>
> This is valid syntax, however, if you are not also restricting your
> query to include a single author, it shows a very poor working
> knowledge of your data does it not?


What the h*ll are you talking about? What data of mine do I not know about?
I didn't start this thread. I never even posted a query on it???



Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 09:11 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0