troubleshooting Question

LEFT OUTER JOIN VS. RIGHT OUTER JOIN

Avatar of tantormedia
tantormediaFlag for United States of America asked on
MySQL Server
8 Comments1 Solution526 ViewsLast Modified:
Dear experts,

I have a query:
SELECT ...
MAX(CASE WHEN r.r_type = 'Audio' THEN r.Awards END) AudioReviews,
MAX(CASE WHEN r.r_type = 'Print' THEN r.Awards END) PrintReviews,
...
FROM BookList b
...
LEFT OUTER JOIN
(SELECT BookStem, r_type, GROUP_CONCAT(DISTINCT CASE WHEN Award IS NOT NULL THEN
CONCAT('<a href="/manage/review.asp?review=', CAST(ID AS CHAR), '" target="_blank">', Award, '</a>')
ELSE CONCAT('<a href="/manage/review.asp?review=', CAST(ID AS CHAR), '" target="_blank">Review</a>') END SEPARATOR '<br />') Awards
FROM Reviews
GROUP BY BookStem, r_type) r
ON b.BookStem = r.BookStem

That outer join part makes my query very slow. I replace the LEFT OUTER JOIN with RIGHT OUTER JOIN and swapped tables accordingly:
SELECT ...
MAX(CASE WHEN r.r_type = 'Audio' THEN r.Awards END) AudioReviews,
MAX(CASE WHEN r.r_type = 'Print' THEN r.Awards END) PrintReviews,
...
FROM
(SELECT BookStem, r_type, GROUP_CONCAT(DISTINCT CASE WHEN Award IS NOT NULL THEN
CONCAT('<a href="/manage/review.asp?review=', CAST(ID AS CHAR), '" target="_blank">', Award, '</a>')
ELSE CONCAT('<a href="/manage/review.asp?review=', CAST(ID AS CHAR), '" target="_blank">Review</a>') END SEPARATOR '<br />') Awards
FROM Reviews
GROUP BY BookStem, r_type) r
RIGHT OUTER JOIN
BookList b
ON b.BookStem = r.BookStem
...
To my surprise, now the query runs 3 times faster, though the result seems to be the same. Could you please explain this to me? I always thought it there should be no difference.
Also, maybe you could give me any suggestion how to improve the query further?
Thanks.
ASKER CERTIFIED SOLUTION
Join our community to see this answer!
Unlock 1 Answer and 8 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 8 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros