Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Joins are for lazy people?

Tags:

java

c#

sql

join

I recently had a discussion with another developer who claimed to me that JOINs (SQL) are useless. This is technically true but he added that using joins is less efficient than making several requests and link tables in the code (C# or Java).

For him joins are for lazy people that don't care about performance. Is this true? Should we avoid using joins?

like image 326
Bastien Vandamme Avatar asked Apr 08 '11 13:04

Bastien Vandamme


3 Answers

No, we should avoid developers who hold such incredibly wrong opinions.

In many cases, a database join is several orders of magnitude faster than anything done via the client, because it avoids DB roundtrips, and the DB can use indexes to perform the join.

Off the top of my head, I can't even imagine a single scenario where a correctly used join would be slower than the equivalent client-side operation.

Edit: There are some rare cases where custom client code can do things more efficiently than a straightforward DB join (see comment by meriton). But this is very much the exception.

like image 88
Michael Borgwardt Avatar answered Nov 19 '22 10:11

Michael Borgwardt


It sounds to me like your colleague would do well with a no-sql document-database or key-value store. Which are themselves very good tools and a good fit for many problems.

However, a relational database is heavily optimised for working with sets. There are many, many ways of querying the data based on joins that are vastly more efficient than lots of round trips. This is where the versatilty of a rdbms comes from. You can achieve the same in a nosql store too, but you often end up building a separate structure suited for each different nature of query.

In short: I disagree. In a RDBMS, joins are fundamental. If you aren't using them, you aren't using it as a RDBMS.

like image 83
Marc Gravell Avatar answered Nov 19 '22 10:11

Marc Gravell


Well, he is wrong in the general case.

Databases are able to optimize using a variety of methods, helped by optimizer hints, table indexes, foreign key relationships and possibly other database vendor specific information.

like image 46
sehe Avatar answered Nov 19 '22 11:11

sehe