MySQL Forums
Forum List  »  Optimizer & Parser

Re: Query Optimization Help
Posted by: Rick James
Date: June 05, 2009 08:37PM

Let's play a trick. If you SUM(RecordType) you get 1 for the ones you want, 2 can't happen?, or 3 if both rows are there. Assuming that will let us avoid the self join and instead use a GROUP BY.

Let's play another trick to avoid hitting the other table twice per result. Let's do the GROUP BY in a subquery that generates a tmp table. (Or you could do it as a separate step.)

SELECT d.OrderNumber,
       d.PhaseNumber,
       d.PartNumber,
       m.IssueDate
   FROM ( SELECT
                JobOrderNumber,
                OrderNumber,
                PhaseNumber,
                PartNumber
             FROM Job_Order_Detail
             GROUP BY JobOrderNumber, PhaseNumber, PartNumber,
                      IssueDate, LineItem
             HAVING SUM(RecordType) = 1
        ) AS d
   JOIN Job_Order_Master AS m
     ON m.JobOrderNumber = d.JobOrderNumber
   WHERE m.IssueDate > d.OpenDate

If this does not suffice, please provide
* SHOW CREATE TABLE tbl\G
* SHOW TABLE STATUS LIKE 'tbl'\G
* EXPLAIN SELECT ...\G
and surround them with [ code ] and [ / code ]

Options: ReplyQuote


Subject
Views
Written By
Posted
3143
June 04, 2009 11:37AM
Re: Query Optimization Help
2139
June 05, 2009 08:37PM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.