Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Performance of inner join compared to cross join

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); 
like image 823
soulmerge Avatar asked Mar 22 '09 13:03

soulmerge


People also ask

Is Cross join slower than inner join?

As per Prod server report, CROSS JOIN was performing faster but as per my theoretical knowledge, INNER JOIN should perform faster.

Which join has better performance?

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.

What is the difference between inner join and cross 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.

Is join or inner join faster?

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.


2 Answers

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

  1. MySQL Performance Tuning and Optimization Resources
  2. PostgreSQL Performance Optimization
like image 169
karim79 Avatar answered Sep 19 '22 15:09

karim79


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.

like image 34
Otávio Décio Avatar answered Sep 19 '22 15:09

Otávio Décio