If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > Optimize mysql join with 3 tables

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 11-02-11, 09:37
MironJ MironJ is offline
Registered User
 
Join Date: Nov 2011
Posts: 3
Question Optimize mysql join with 3 tables

Hello,

I need some help with optimizing this mysql query code:

Code:
SELECT tblinvoices.id, tblinvoices.userid, tblinvoices.datepaid, tblinvoices.total, tblinvoices.credit, tblclients.companyname, tblclients.firstname, tblclients.lastname, tblclients.currency AS clvaluta
FROM tblinvoices
LEFT JOIN racuni ON tblinvoices.id = racuni.pid
LEFT JOIN tblclients ON tblinvoices.userid = tblclients.id
WHERE NOT EXISTS (SELECT * FROM racuni WHERE racuni.pid = tblinvoices.id)
AND tblinvoices.paymentmethod = 'tco'
AND tblinvoices.status = 'Paid'
AND tblinvoices.date > '2011-01-01'
ORDER BY tblinvoices.datepaid ASC;

It's working like expected, but it's very slow, about 5-6 minutes to finish.

Is there any other way to write this query so it will finish much faster?

Thanks to all,
Miron J.
Reply With Quote
  #2 (permalink)  
Old 11-02-11, 10:10
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
optimizing a query requires ensuring that the appropriate indexes for that query have been defined

however, in some cases, the query itself has some nonsense in it, and this is one of those cases

remove this line --
Code:
LEFT JOIN racuni ON tblinvoices.id = racuni.pid
just remove it completely and see what happens
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 11-02-11, 13:16
MironJ MironJ is offline
Registered User
 
Join Date: Nov 2011
Posts: 3
Hi,

On every tables are added indexes on "id", "pid" or "userid" fields.

Removing line
Code:
LEFT JOIN racuni ON tblinvoices.id = racuni.pid
does not help, almost same result:

Before removing:
~~~~~~~~~~~~~~
2 rows in set (5 min 45.83 sec)
~~~~~~~~~~~~~~

After removing:
~~~~~~~~~~~~~~
2 rows in set (5 min 47.29 sec)
~~~~~~~~~~~~~~

Main issue is in line:
Code:
WHERE NOT EXISTS (SELECT * FROM racuni WHERE racuni.pid = tblinvoices.id)
when I remove this, query is executed very fast:
~~~~~~~~~~~~~~
17729 rows in set (0.72 sec)
~~~~~~~~~~~~~~

but then query return all rows, and I need only rows where "racuni.pid" are not same as "tblinvoices.id"


Thanks,
Miron J.
Reply With Quote
  #4 (permalink)  
Old 11-02-11, 13:56
it-iss.com it-iss.com is offline
Registered User
 
Join Date: Sep 2009
Location: San Sebastian, Spain
Posts: 620
What does EXPLAIN SELECT return? This will give an idea to the indexes that are being used.
__________________
Ronan Cashell
Senior Oracle/MySQL DBA
http://www.it-iss.com
Reply With Quote
  #5 (permalink)  
Old 11-02-11, 14:01
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
Quote:
Originally Posted by MironJ View Post
when I remove this, query is executed very fast:
aha!!

okay, please try this --
Code:
SELECT tblinvoices.id
     , tblinvoices.userid
     , tblinvoices.datepaid
     , tblinvoices.total
     , tblinvoices.credit
     , tblclients.companyname
     , tblclients.firstname
     , tblclients.lastname
     , tblclients.currency AS clvaluta
  FROM tblinvoices
LEFT OUTER
  JOIN tblclients 
    ON tblclients.id = tblinvoices.userid
LEFT OUTER
  JOIN racuni 
    ON racuni.pid = tblinvoices.id
 WHERE tblinvoices.paymentmethod = 'tco'
   AND tblinvoices.status = 'Paid'
   AND tblinvoices.date > '2011-01-01'
   AND racuni.pid IS NULL
ORDER 
    BY tblinvoices.datepaid ASC;
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #6 (permalink)  
Old 11-02-11, 14:09
MironJ MironJ is offline
Registered User
 
Join Date: Nov 2011
Posts: 3
Thumbs up

Hi,

Just tried, it's working just as I need, and it's fast:
~~~~~~~~~~~~~~~~~
2 rows in set (13.05 sec)
~~~~~~~~~~~~~~~~~

Thank you very much!

Miron J.
Reply With Quote
Reply

Tags
join table, optimize, query

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 On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On