News for hackers. Stuff that matters.
Re:Query optimization in MySQL with Subqueries by jswinghammer on Friday February 11, 2011 @09:50AM (#2206468)
I'm reading the first query and maybe it's just early but why isn't that subquery just a join? It seems to be acting that way.
Re:Query optimization in MySQL with Subqueries by IgorPartola on Friday February 11, 2011 @01:51PM (#2207634)
By definition all queries using JOINs can be re-written as sub-queries and vice versa. However, in some cases one is more intuitive, convenient or faster than the other. With MySQL you are better off with JOINs in most cases, and use subqueries only when there is a clear benefit and you understand why that would be faster. One exception to this is using a subquery with EXISTS, which is often way faster. While MySQL will tell you that it will look at all matching rows (when looking at EXPLAIN), it will only look at the first one it finds, so practically it ends up being faster.
Re:Query optimization in MySQL with Subqueries by nphase on Friday February 11, 2011 @11:07AM (#2206768)
It basically is; the technique used here is sometimes called a delayed join, which is also extremely effective on huge sorted queries.
Re:Query optimization in MySQL with Subqueries by serdarbicer on Friday February 11, 2011 @10:08AM (#2206523)
Good point! Actually, in early stages of our development, it was accomplished using two different queries and we needed to combine them to a single query. It was just a scratch and maybe it caused a bias which prevented me to see shorter way.