MySQL Forums
Forum List  »  Performance

Re: Optimizing Problems with Where Statement
Posted by: Jay Pipes
Date: December 19, 2005 08:47PM

You are using MySQL < 5 correct? If so, this should speed things up quite a bit:

SELECT p.id, t.id2
FROM table1 p
LEFT JOIN table2 t
ON t.id2 = p.id1
WHERE p.time > 1135012444
AND p.id = t.col3
UNION
SELECT p.id, t.id2
FROM table1 p
LEFT JOIN table2 t
ON t.id2 = p.id1
WHERE p.id IN (<array>)
AND p.id = t.col3;

however, I have trouble understanding how correct results are returned for the second, since I cannot think of a situation where the following will be true:

p.id IN (<array>) *and* p.id = t.col3

unless, of course, t.col3 is actually in the array... so, you can shorten to:

SELECT p.id, t.id2
FROM table1 p
LEFT JOIN table2 t
ON t.id2 = p.id1
WHERE p.time > 1135012444
AND p.id = t.col3
UNION
SELECT p.id, t.id2
FROM table1 p
LEFT JOIN table2 t
ON t.id2 = p.id1
WHERE p.id = t.col3;

Jay Pipes
Community Relations Manager, North America, MySQL Inc.

Got Cluster? http://www.mysql.com/cluster
Personal: http://jpipes.com



Edited 1 time(s). Last edit at 12/19/2005 08:48PM by Jay Pipes.

Options: ReplyQuote


Subject
Views
Written By
Posted
Re: Optimizing Problems with Where Statement
1367
December 19, 2005 08:47PM


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.