Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can an INNER JOIN offer better performance than EXISTS

I've been investigating making performance improvements on a series of procedures, and recently a colleague mentioned that he had achieved significant performance improvements when utilising an INNER JOIN in place of EXISTS.

As part of the investigation as to why this might be I thought I would ask the question here.

So:

  • Can an INNER JOIN offer better performance than EXISTS?
  • What circumstances would this happen?
  • How might I set up a test case as proof?
  • Do you have any useful links to further documentation?

And really, any other experience people can bring to bear on this question.

I would appreciate if any answers could address this question specifically without any suggestion of other possible performance improvements. We've had quite a degree of success already, and I was just interested in this one item.

Any help would be much appreciated.

like image 482
James Wiseman Avatar asked Feb 01 '10 14:02

James Wiseman


People also ask

Does inner join order matter for performance?

Basically, join order DOES matter because if we can join two tables that will reduce the number of rows needed to be processed by subsequent steps, then our performance will improve.

Are inner joins more performant?

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.

Which is better join or exists?

In cases like above the Exists statement works faster than that of Joins. Exists will give you a single record and will save the time also. In case of joins the number of records will be more and all the records must be used.

Is inner join better than in?

If all you need is to check for matching rows in the other table but don't need any columns from that table, use IN. If you do need columns from the second table, use Inner Join.


2 Answers

Generally speaking, INNER JOIN and EXISTS are different things.

The former returns duplicates and columns from both tables, the latter returns one record and, being a predicate, returns records from only one table.

If you do an inner join on a UNIQUE column, they exhibit same performance.

If you do an inner join on a recordset with DISTINCT applied (to get rid of the duplicates), EXISTS is usually faster.

IN and EXISTS clauses (with an equijoin correlation) usually employ one of the several SEMI JOIN algorithms which are usually more efficient than a DISTINCT on one of the tables.

See this article in my blog:

  • IN vs. JOIN vs. EXISTS
like image 51
Quassnoi Avatar answered Sep 17 '22 08:09

Quassnoi


Maybe, maybe not.

  • The same plan will be generated most likely
  • An INNER JOIN may require a DISTINCT to get the same output
  • EXISTS deals with NULL
like image 21
gbn Avatar answered Sep 17 '22 08:09

gbn