0

I'm wondering about the speed between explicitly saying LEFT JOIN, and then simply joining the values on each other. So here, we have:

SELECT example.id FROM example
LEFT JOIN finals ON example.id = finals.account_id";

vs.

SELECT example.id, finals.account_id
FROM example, finals
WHERE example.id = finals.account_id

I'm nearly certain the first is faster, but I cannot find an answer to suggest that. (I'm not actually even sure what to 'call' the second one, I just call it a soft join)

Vael Victus
  • 3,966
  • 7
  • 34
  • 55
  • Duplicate or similar question: http://stackoverflow.com/questions/894490/sql-left-join-vs-multiple-tables-on-from-line – jli Oct 27 '11 at 22:32

4 Answers4

2

There is a sematic difference between those queries.

The first join is an OUTER JOIN that includes all rows from the left table. The second is an INNER JOIN, and will not include rows where the match fails.

If you had written JOIN instead of LEFT JOIN the two queries would be identical, except the first syntax is preferred for readability and maintainability.

I'm not actually even sure what to 'call' the second one, I just call it a soft join)

The second syntax uses what MySQL calls the "comma operator". It is sometimes called an implicit join. It's also called an ANSI-89 join.

Mark Byers
  • 811,555
  • 193
  • 1,581
  • 1,452
2

The second is an INNER JOIN (the default) and it's less a question of speed than output - an inner join will only return where the specified field exists in both tables.

eg if there was an example record #3 but no finals record with an account_id of 3, neither record would be shown

A LEFT JOIN would list records from example even where there's no matching finals record - The missing fields would be set to NULL

Basic
  • 26,321
  • 24
  • 115
  • 201
0

This second example does an INNER JOIN and will return potentially different results.

Dylan Smith
  • 22,069
  • 2
  • 47
  • 62
0

You can't really compare the speed of the two because they will give different results. The second query requires an entry on both tables. The first query only requires an entry on the first table.

The second query is using a CROSS JOIN (no difference from JOIN in mysql .. comma is an alias for JOIN with no ON clause).

Explosion Pills
  • 188,624
  • 52
  • 326
  • 405