• Status: Solved
  • Priority: Medium
  • Security: Private
  • Views: 50
  • Last Modified:

mySQL simple left join statement

Dear Experts,
below mysql statement is working but I think I do something wrong,
it takes too long to bring the data.

What is wrong with it? How should I improve it?
 I use MySQL

SELECT table1.uid,table1.isim, table2.toplam, table2.bayikodu
FROM table1
LEFT JOIN table2 ON table2.bayikodu = table1.uid	
Where bayikodu is Null

Open in new window

0
BR
Asked:
BR
3 Solutions
 
Leonidas DosasCommented:
Take a look this article about server memory buffet. sql cache buffer.
I think that there you must focus.
0
 
johnsoneSenior Oracle DBACommented:
Assuming TABLE2.BAYIKODU is indexed, try this:
SELECT table1.uid, 
       table1.isim, 
       NULL, 
       NULL 
FROM   table1 
WHERE  NOT EXISTS (SELECT 1 
                   FROM   table2 
                   WHERE  bayikodu = table1.uid); 

Open in new window

Functionally equivalent.  Your original is doing an outer join, but looking for rows where the join failed (the IS NULL condition in the WHERE clause).

Rather than join the tables together and looking for the missing join condition, just do the check in the second table.  Have to scan TABLE1 either way, so you will not get a benefit there.  Just doing a keyed lookup in an index on TABLE2 should be faster than pulling the row back and trying to fully join the tables.
2
 
David FavorLinux/LXD/WordPress/Hosting SavantCommented:
Also consider running an EXPLAIN on your query.

You'd be surprised how much you can learn from a simple EXPLAIN, for example, you may learn (see jonsone's comment) that adding a simple index in one place may make a huge difference in how fast your query runs, as your data size increases.
1
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

 
BRMarketingAuthor Commented:
Dear David, how can I run Explain on my query?
Thank you, it's a good advise
0
 
BRMarketingAuthor Commented:
Dera All,
Where can i learn all this mySql joint statement logics?
I think, i need to learn the logic behind
0
 
David FavorLinux/LXD/WordPress/Hosting SavantCommented:
Just insert the word EXPLAIN just before your SELECT.

As for learning SQL, start with YouTube + then move onto Udemy + other courseware providers.

There are so many great courses.

A really great starting point are the several books written on High Performance MariaDB, you can find at Amazon. All this information is available for free + these books do a great job of collecting + organizing this info. Likely these books will save you many hours of time.

Welcome to SQL-ing!
2
 
BRMarketingAuthor Commented:
THANK YOU All
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now