tags:

views:

122

answers:

4

What are the potential pros and cons of each of these queries given different databases, configurations, etc? Is there ever a time when one would be more efficient than the other? Vice versa? Is there an even better way to do it? Can you explain why?

Query 1:

SELECT 
  *
FROM
  table_a, table_b, table_c
WHERE
  table_a.id = table_b.id AND
  table_a.id = table_c.id AND
  table_a.create_date > DATE('1998-01-01');

Query 2:

SELECT 
  *
FROM
  table_a 
INNER JOIN table_b ON
  table_a.id = table_b.id
INNER JOIN table_c ON
  table_a.id = table_c.id
WHERE
  table_a.create_date > DATE('1998-01-01');
A: 

I agree, it's sounding a bit too much like Homework!

If it isn't homework then I guess the simplest answer is readability.

As stated before, both queries will produce the same execution plan. If this is the case then the only thing you need to worry about it maintainability.

Colorado
+2  A: 

Same query, different revision of SQL spec. The query optimizer should come up with the same query plan for those.

A: 

Nope. I'm just sharing a large, overwhelmed database with some coworkers and am trying to come up with some ways to get more processor bang for our buck. I've been looking around online but haven't found a good explanation for some questions like this.

Sorry for sounding homework-y. I guess I spent too many years as a TA.

A: 

Actually, I think query 2's more readable. Think about when you get to say 5,6, or 7 tables when you hit the where clause in query one. Following the joins could get messy.

As for performance, I have no idea. I bet if you go to the MySQL website, there would be info there - probably examples of joins.

Professionally, I've only worked on one project. But it was a big one, and they always followed query 2's format. This was using Microsoft SQL Server though.