Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is so bad about using SQL INNER JOIN

Every time a database diagram gets looked out, one area people are critical of is inner joins. They look at them hard and has questions to see if an inner join really needs to be there.

Simple Library Example:

A many-to-many relationship is normally defined in SQL with three tables: Book, Category, BookCategory.

In this situation, Category is a table that contains two columns: ID, CategoryName.

In this situation, I have gotten questions about the Category table, is it need? Can it be used as a lookup table, and in the BookCategory table store the CategoryName instead of the CategoryID to stop from having to do an additional INNER JOIN. (For this question, we are going to ignore the changing, deleting of any CategoryNames)

The question is, what is so bad about inner joins? At what point is doing them a negative thing (general guidelines like # of transactions, # of records, # of joins in a statement, etc)?

like image 753
SBurris Avatar asked Mar 16 '10 00:03

SBurris


People also ask

Is it good to use inner join?

While both queries are well-written, I would suggest that you always use INNER JOIN instead of listing tables and joining them in the WHERE part of the query. There are a few reasons for that: Readability is much better because the table used and related JOIN condition are in the same line.

Should I use inner join in SQL?

Generally, we use INNER JOIN when we want to select only rows that match an ON condition. If no rows match the ON condition, then it will not return any results. This can be somewhat stricter than using a LEFT JOIN .

Why is join bad in SQL?

Joins do require extra processing since they have to look in more files and more indexes to "join" the data together.

Is inner join better than left join?

A LEFT JOIN is absolutely not faster than an INNER JOIN . In fact, it's slower; by definition, an outer join ( LEFT JOIN or RIGHT JOIN ) has to do all the work of an INNER JOIN plus the extra work of null-extending the results.


Video Answer


2 Answers

Your example is a good counterexample. How do you rename categories if they're spread throughout the various rows of the BookCategory table? Your UPDATE to do the rename would touch all the rows in the same category.

With the separate table, you only have to update one row. There is no duplicate information.

like image 84
John Saunders Avatar answered Oct 09 '22 19:10

John Saunders


I would be more concerned about OUTER joins, and the potential to pick up info that wasn't intended.

In your example, having the Category table means that a book is limited to being filed under a preset Category (via a foriegn key relationship), if you just shoved multiple entries in to the BookCategory table then it would be harder to limit what is selected for the Category.

Doing an INNER join is not so bad, it is what databases are made for. The only time it is bad is when you are doing it on a table or column that is inadequately indexed.

like image 25
slugster Avatar answered Oct 09 '22 20:10

slugster