MySQL Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesMySQL Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Stop making mediocre tutorials.The best tutorials are video! Camtasia Studio makes it easy to create engaging, buzz-building screen videos at any size, in any popular format. Download the free trial!
  #1  
Old May 5th, 2008, 02:57 PM
DavidC99 DavidC99 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2007
Posts: 25 DavidC99 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 10 h 35 m 48 sec
Reputation Power: 0
Left Join not good?

Greetings,

My DBA was just by discussing some problems we have had with our 5.1 install. In particular how some queries were making a bunch of "temp" tables and not cleaning up after themselves.

I then said, that most of my queries were very simple (indeed classified as simply by "explain") and that at most I was using left joins to string tables together.

The DBA said: "Oh, Left joins ... that may not be the best way".

Question: What is wrong with left joins on keys?

ie:

Select A, B, C
FROM TABLE OrderDetail
LEFT JOIN OrderHeader on OrderDetail.Order_ID = OrderHeader.ID
LEFT JOIN Customer on OrderHeader.Cust_ID = Customer.ID
WHERE Customer.ID = 123

Isn't that the proper way to grab all the OrderDetail lines for a Customer if the Customer ID is only stored in the OrderHeader table?

And if the items on the order don't have product names, would the query not change to:

Select A, B, C, Item.ProductName
FROM TABLE OrderDetail
LEFT JOIN Item on OrderDetail.Item_ID = Item.ID
LEFT JOIN OrderHeader on OrderDetail.Order_ID = OrderHeader.ID
LEFT JOIN Customer on OrderHeader.Cust_ID = Customer.ID
WHERE Customer.ID = 123

Without comment on data structure :-), isn't that the fastest, cleanest way to get the data out of the DB?

Reply With Quote
  #2  
Old May 5th, 2008, 03:48 PM
MrFujin's Avatar
MrFujin MrFujin is offline
Lord of the Dance
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Oct 2003
Posts: 1,158 MrFujin User rank is Captain (20000 - 30000 Reputation Level)MrFujin User rank is Captain (20000 - 30000 Reputation Level)MrFujin User rank is Captain (20000 - 30000 Reputation Level)MrFujin User rank is Captain (20000 - 30000 Reputation Level)MrFujin User rank is Captain (20000 - 30000 Reputation Level)MrFujin User rank is Captain (20000 - 30000 Reputation Level)MrFujin User rank is Captain (20000 - 30000 Reputation Level)MrFujin User rank is Captain (20000 - 30000 Reputation Level)MrFujin User rank is Captain (20000 - 30000 Reputation Level) 
Time spent in forums: 1 Month 3 Days 15 h 35 m 28 sec
Reputation Power: 216
Quote:
Originally Posted by DavidC99
Without comment on data structure :-), isn't that the fastest, cleanest way to get the data out of the DB?


if you don't look at the data structure, how do you know what data to get and if you would need inner or left ?


Quote:
Question: What is wrong with left joins on keys?


it don't use index at the joined table.

with LEFT you are saying you want all orderDetails, no matter if it does or does not contains any items or orderHeader.
same with getting all orderHeader, even if user don't exists.


try read this post for more about INNER vs LEFT

and last, i would change the WHERE in
Code:
LEFT JOIN Customer on OrderHeader.Cust_ID = Customer.ID
WHERE Customer.ID = 123

to a AND in the join
Code:
LEFT JOIN Customer on OrderHeader.Cust_ID = Customer.ID AND Customer.ID = 123


as using WHERE would perform a CROSS JOIN and get all orders for each user first, and then limit the result for the specified user.

Last edited by MrFujin : May 5th, 2008 at 03:55 PM.

Reply With Quote
  #3  
Old May 5th, 2008, 04:26 PM
annlis1977 annlis1977 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2006
Posts: 9 annlis1977 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 21 m 54 sec
Reputation Power: 0
LEFT JOIN is using indexes if properly set.

Use LEFT JOIN, if the relation you have is 0,*. If you have 1,* use a simple join (In WHERE clause) instead.

Is your DBA a MySQL DBA or a "generic" DBA? You should work with him (trace to see the slow/big queries) to identify the exact query type which is causing problem.

He will have then to finetune the config or you will have to modify your queries/model.

Reply With Quote
  #4  
Old May 5th, 2008, 05:10 PM
DavidC99 DavidC99 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2007
Posts: 25 DavidC99 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 10 h 35 m 48 sec
Reputation Power: 0
Quote:
Originally Posted by MrFujin
if you don't look at the data structure, how do you know what data to get and if you would need inner or left ?


I meant more of a "that data structure sucks" view

Quote:
it don't use index at the joined table.

with LEFT you are saying you want all orderDetails, no matter if it does or does not contains any items or orderHeader.
same with getting all orderHeader, even if user don't exists.


The joins are fully indexed as they are really reaching back up the data tree. There can not be a OrderDetail, without an OrderHead, and there can not be a OrderHead, without a Customer.

And those are the actual UK fields in the tables.

Quote:
try read this post for more about INNER vs LEFT


For some reason, when I think of "inner join", I think of it joining upon itself. I can't seem to wrap my head around when I would want to use a left vs a inner vs an outer (and isn't left / outer the same?)


Quote:
and last, i would change the WHERE in
Code:
LEFT JOIN Customer on OrderHeader.Cust_ID = Customer.ID
WHERE Customer.ID = 123

to a AND in the join
Code:
LEFT JOIN Customer on OrderHeader.Cust_ID = Customer.ID AND Customer.ID = 123



Does that actually "speed" performance?

I currently put the Join statements in to get at Data fields not present in the base table. The where statement is dynamically built based upon what is passed into the method.

So unless there is a speed boost, I would want to keep using a dynamic where clause that is independent of the join statements.


Quote:
as using WHERE would perform a CROSS JOIN and get all orders for each user first, and then limit the result for the specified user.


That's actually not what is happening in the explain though. Since it's a backwards chain of indexed fields, it seems to be pulling exactly what it should ... so maybe my example was lacking something.

Main question was: Is left join the *fastest* method if everything is indexed properly (and in this case, they are actually the UK when linking upwards).

I think the answer was "yes" :-)

Reply With Quote
  #5  
Old May 5th, 2008, 05:17 PM
DavidC99 DavidC99 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2007
Posts: 25 DavidC99 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 10 h 35 m 48 sec
Reputation Power: 0
Quote:
Originally Posted by annlis1977
LEFT JOIN is using indexes if properly set.


That is what I thought. Left Join, when indexed properly, is the way to go.

Quote:
Use LEFT JOIN, if the relation you have is 0,*. If you have 1,* use a simple join (In WHERE clause) instead.

Not sure what that means.

0,* = 1 -> n
1,* = n -> n

Yes no?

Quote:
Is your DBA a MySQL DBA or a "generic" DBA? You should work with him (trace to see the slow/big queries) to identify the exact query type which is causing problem.

He will have then to finetune the config or you will have to modify your queries/model.

I think a cross between the two :-) I believe more experience on the MSSQL then MySQL ... I think the statement made to me was more of a blanket statement.

Reply With Quote
  #6  
Old May 5th, 2008, 05:40 PM
annlis1977 annlis1977 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2006
Posts: 9 annlis1977 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 21 m 54 sec
Reputation Power: 0
Quote:
Originally Posted by DavidC99
Not sure what that means.

0,* = 1 -> n
1,* = n -> n



0,* = 0, N (where N is bigger or equal to 1)
1,* = 1, N (where N is bigger or equal to 1)

Reply With Quote
  #7  
Old May 5th, 2008, 06:09 PM
DavidC99 DavidC99 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2007
Posts: 25 DavidC99 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 10 h 35 m 48 sec
Reputation Power: 0
Quote:
Originally Posted by annlis1977
0,* = 0, N (where N is bigger or equal to 1)
1,* = 1, N (where N is bigger or equal to 1)


Doesn't make sense in a context of 1-to-1 and 1-to-N (many) relationship.

So I am lost on that notation

Reply With Quote
  #8  
Old May 6th, 2008, 03:11 AM
MrFujin's Avatar
MrFujin MrFujin is offline
Lord of the Dance
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Oct 2003
Posts: 1,158 MrFujin User rank is Captain (20000 - 30000 Reputation Level)MrFujin User rank is Captain (20000 - 30000 Reputation Level)MrFujin User rank is Captain (20000 - 30000 Reputation Level)MrFujin User rank is Captain (20000 - 30000 Reputation Level)MrFujin User rank is Captain (20000 - 30000 Reputation Level)MrFujin User rank is Captain (20000 - 30000 Reputation Level)MrFujin User rank is Captain (20000 - 30000 Reputation Level)MrFujin User rank is Captain (20000 - 30000 Reputation Level)MrFujin User rank is Captain (20000 - 30000 Reputation Level) 
Time spent in forums: 1 Month 3 Days 15 h 35 m 28 sec
Reputation Power: 216
Quote:
Originally Posted by DavidC99
Doesn't make sense in a context of 1-to-1 and 1-to-N (many) relationship.

So I am lost on that notation



can try give a examples, where you want customer and orders done.

you will use LEFT JOIN to get all customer, including those who haven't create a order yet, and inner INNER JOIN to only get those customer that has orders.

Reply With Quote
  #9  
Old May 6th, 2008, 05:42 AM
r937's Avatar
r937 r937 is offline
SQL Consultant
Dev Shed God 24th Plane (16500 - 16999 posts)
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 16,727 r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 3 Weeks 2 Days 19 h 22 m 42 sec
Reputation Power: 848
david, others have given you some good info, i will try to relate it back to your original query

Select A, B, C
FROM OrderDetail
LEFT JOIN OrderHeader on OrderDetail.Order_ID = OrderHeader.ID
LEFT JOIN Customer on OrderHeader.Cust_ID = Customer.ID
WHERE Customer.ID = 123

this query says "start with all OrderDetails" i.e. every item that was ever purchased

then join to OrderHeader, whether or not the OrderDetail has an OrderHeader = highly unlikely situation!!

knowing something about the data (by guessing the role of each table), i would have to conclude that this should not be a LEFT OUTER JOIN but rather an INNER JOIN

okay, now we have every item ever ordered, together with its order header

now join to the Customer table, whether or not the OrderHeader has a Customer = another highly unlikely situation!!

highly unlikely that you would accept an order from a customer who doesn't exist

so now we have all the items ever ordered, with their order headers, and their customers...

... and now you throw most of them away with the WHERE clause!!

doesn't sound efficient, right?



okay, now let's walk through this --

Select A, B, C
FROM Customer
LEFT JOIN OrderHeader on OrderHeader.Cust_ID = Customer.ID
LEFT JOIN OrderDetail on OrderDetail.Order_ID = OrderHeader.ID
WHERE Customer.ID = 123

here's how this works

start with customer 123

wait a sec! how did it know to start with only that customer? because the WHERE clause operates on the first table in the FROM clause -- the optimizer can figure this out, whereas it could not do so from the previous query

then get all the orders if any for customer 123, then all the order items for each order if any (and naturally, an order will have at least one item)

much faster, yes? because you're dealing only with customer 123's data, not every item that was ever purchased

does this explanation help?
__________________
r937.com | rudy.ca

Last edited by r937 : May 6th, 2008 at 05:45 AM.

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesMySQL Help > Left Join not good?


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

 Free IT White Papers!
 
Accelerating Trading Partner Performance
One in five. That's how many partner transactions have at least one error. That is an amazing statistic, particularly given the extraordinary leaps in innovation across the global supply chain during the past two decades. Download this white paper to learn more.

 
Competing on Analytics
This Tech Analysis is designed to help identify characteristics shared by analytics competitors, and includes information about 32 organizations that have made a commitment to quantitative, fact-based analysis.

 
Cost Effective Scaling with Virtualization and Coyote Point Systems
An overview of the industry trend toward virtualization, how server consolidation has increased the importance of application uptime and the steps being taken to integrate load balancing technology with virtualized servers.

 
Five Checkpoints to Implementing IP Telephony
Implementation planning for IP PBX software and IP telephony has become vital as businesses replace discontinued legacy PBX phone systems. This informative whitepaper outlines five "checkpoints" for any implementation plan that will help make IP communications a successful proposition.

 
Hosted Email Security: Staying Ahead of New Threats
In the last two years, email has become a fierce battleground between the nefarious forces of spam and malware, and the heroes of messaging protection. The spam volumes increased alarmingly every month, bringing clever new forms of phishing and virus propagation attacks.

 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway