views:

6389

answers:

14
+41  Q: 

Inner join vs Where

Is there a difference in performance (in oracle) between

Select * from Table1 T1 
Inner Join Table2 T2 On T1.ID = T2.ID

And

Select * from Table1 T1, Table2 T2 
Where T1.ID = T2.ID

?

+30  A: 

If the query optimizer is doing its job right, there should be no difference between those queries. They are just two ways to specify the same desired result.

Craig Trader
Yeah, performance should be the same. But the SELECT * FROM Table1, Table2 WHERE ... syntax is EVIL!
Joel Coehoorn
I find it much easier to comprehend FOR INNER JOINS than the SQL-92 syntax. Your mileage may vary.
Craig Trader
I find the WHERE syntax easier to read than INNER JION - I guess its like Vegemite. Most people in the world probably find it disgusting but kids brought up eating it love it.
ScottCher
Vegemite is nasty indeed, but then again I love scrapple. Go figure.
StingyJack
+1  A: 

In PostgreSQL, there's definitely no difference - they both equate to the same query plan. I'm 99% sure that's also the case for Oracle.

Nick Johnson
+18  A: 

They should be exactly the same. However, as a coding practice, I would rather see the Join. It clearly articulates your intent,

Nescio
I agree. Especially if you're joining to multiple tables, it's a lot easier to parse a select statement if you're doing explicit joins.
Paul Morie
+1  A: 

They're both inner joins that do the same thing, one simply uses the newer ANSI syntax.

firebird84
+1  A: 

Functionally they are the same as has been said. I agree though that doing the join is better for describing exactly what you want to do. Plenty of times I've thought I knew how I wanted to query something until I started doing the joins and realized I wanted to do a different query than the original one in my head.

MattC
+6  A: 

They're logically identical but in the earlier versions of Oracle that adopted ANSI syntax there were often bugs with it in more complex cases, so you'll sometimesencounter resistance to using it from Oracle people.

David Aldridge
Earlier versions of Oracle had bugs with this? How early? What version(s)?
ScottCher
Metalink has details ... they pop-up all over the place.
David Aldridge
+5  A: 

I don't know about Oracle but I know that the old syntax is being deprecated in SQL Server and will disappear eventually. Before I used that old syntax in a new query I would check what Oracle plans to do with it.

I prefer the newer syntax rather than the mixing of the join criteria with other needed where conditions. In the newer syntax it is much clearer what creates the join and what other conditions are being applied. Not really a big problem in a short query like this, but it gets much more confusing when you have a more complex query. Since people learn on the basic queries, I would tend to prefer people learn to use the join syntax before they need it in a complex query.

And again I don't know Oracle specifically, but I know the SQL Server version of the old style left join is flawed even in SQL Server 2000 and gives inconsistent results (sometimes a left join sometimes a cross join), so it should never be used. Hopefully Oracle doesn't suffer the same issue, but certainly left and right joins can be mcuh harder to properly express in the old syntax.

Plus it has been my experience (and of course this is strictly a personal opinion, you may have differnt experience) that developers who use the ANSII standard joins tend to have a better understanding of what a join is and what it means in terms of getting data out of the database. I belive that is becasue most of the people with good database understanding tend to write more complex queries and those seem to me to be far easier to maintain using the ANSII Standard than the old style.

HLGEM
Amen brother. Down with the JOINERS!!
ScottCher
+8  A: 

Using JOIN makes the code easier to read, since it's self-explanatory.

In speed, there's no difference (I have just tested it), and the execution plan is the same....

Thanks for testing it!
Juan Manuel
A: 

It is true that, functionally, both queries should be processed the same way. However, experience has shown that if you are selecting from views that use the new join syntax, it is important to structure your queries using it as well. Oracle's optimizer can get confused if a view uses a "join" statement, but a query accessing the view uses the traditional method of joining in the "where" clause.

JoshL
That's more a problem with views than with joins at all.
unexist
+3  A: 

The performance should be identical, but I would suggest using the join-version due to improved clarity when it comes to outer joins.

Also unintentional cartesian products can be avoided using the join-version.

A third effect is an easier to read SQL with a simpler WHERE-condition.

stili
+28  A: 

No! The same execution plan, look at these two tables:

CREATE TABLE table1 (
  id INT,
  name VARCHAR(20)
);

CREATE TABLE table2 (
  id INT,
  name VARCHAR(20)
);

The execution plan for the query using the inner join:

-- with inner join

EXPLAIN PLAN FOR
SELECT * FROM table1 t1
INNER JOIN table2 t2 ON t1.id = t2.id;

SELECT *
FROM TABLE (DBMS_XPLAN.DISPLAY);

-- 0 select statement
-- 1 hash join (access("T1"."ID"="T2"."ID"))
-- 2 table access full table1
-- 3 table access full table2

And the execution plan for the query using a WHERE clause.

-- with where clause

EXPLAIN PLAN FOR
SELECT * FROM table1 t1, table2 t2
WHERE t1.id = t2.id;

SELECT *
FROM TABLE (DBMS_XPLAN.DISPLAY);

-- 0 select statement
-- 1 hash join (access("T1"."ID"="T2"."ID"))
-- 2 table access full table1
-- 3 table access full table2
Kiewic
Perhaps the original question has been rephrased in editing: I think this answer should begin "No! ..." rather than "Yes! ...". ;)
cheduardo
I agree, this answer should begin with "No!"
Paul Morie
Agree.. it should says No! instead of Yes! It's like: "Yes! 1 = 2!" A bit strange.. however +1 for the rest, pretty well illustrated and explained.
+2  A: 
cheduardo
I rollbacked the edition because the previous revision changed the meaning of the answer
Juan Manuel
+2  A: 

[For a bonus point...]

Using the JOIN syntax allows you to more easily comment out the join as its all included on one line. This can be useful if you are debugging a complex query

As everyone else says, they are functionally the same, however the JOIN is more clear of a statement of intent. It therefore may help the query optimiser either in current oracle versions in certain cases (I have no idea if it does), it may help the query optimiser in future versions of Oracle (no-one has any idea), or it may help if you change database supplier.

Chris Gill
A: 

Can any one Tell me How to see the Above Execute Query Plan, Do i Need to use any tool ? I am presently I am working on Mysql Database and using SQLYog

harigm
`EXPLAIN [EXTENDED] SELECT select_options;` See here: http://dev.mysql.com/doc/refman/5.0/en/explain.html
Matt Huggins