The effect of issuing an inner join is the same as stating a cross join with the join condition in the WHERE-clause. I noticed that many people in my company use cross joins, where I would use inner joins. I didn't notice any significant performance gain after changing some of these queries and was wondering if it was just a coincidence or if the DBMS optimizes such issues transparently (MySql in our case). And here a concrete example for discussion:
SELECT User.* FROM User, Address WHERE User.addressId = Address.id; SELECT User.* FROM User INNER JOIN Address ON (User.addressId = Address.id);
As per Prod server report, CROSS JOIN was performing faster but as per my theoretical knowledge, INNER JOIN should perform faster.
If you dont include the items of the left joined table, in the select statement, the left join will be faster than the same query with inner join. If you do include the left joined table in the select statement, the inner join with the same query was equal or faster than the left join.
CROSS JOIN is the full cartesian product of the two sides of a JOIN. INNER JOIN is a reduction of the cartesian product—we specify a predicate and get a result where the predicate matches.
Returns only the rows that have matching values in both the tables. Includes the matching rows as well as some of the non-matching rows between the two tables. In case there are a large number of rows in the tables and there is an index to use, INNER JOIN is generally faster than OUTER JOIN.
Cross Joins produce results that consist of every combination of rows from two or more tables. That means if table A has 6 rows and table B has 3 rows, a cross join will result in 18 rows. There is no relationship established between the two tables – you literally just produce every possible combination.
With an inner join, column values from one row of a table are combined with column values from another row of another (or the same) table to form a single row of data.
If a WHERE clause is added to a cross join, it behaves as an inner join as the WHERE imposes a limiting factor.
As long as your queries abide by common sense and vendor specific performance guidelines (i), I like to think of the decision on which type of join to use to be a simple matter of taste.
(i) Vendor Specific Performance Guidelines
There is no difference other than the inner join is a lot clearer because it defines the join, leaving the where clause to be the actual limiting condition.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With