Inner joins and WHERE clause in Oracle
In Oracle database, the difference between using inner join (INNER JOIN) and WHERE clause to join two tables is a common problem. While there may be subtle differences between the two in specific situations, the overall performance difference is usually negligible.
Example below:
<code class="language-sql">Select * from Table1 T1 Inner Join Table2 T2 On T1.ID = T2.ID</code>
and
<code class="language-sql">Select * from Table1 T1, Table2 T2 Where T1.ID = T2.ID</code>
Both queries perform the same operation: join the rows in Table1 with the rows in Table2 based on equality of the ID columns. To understand this better, let's create two example tables:
<code class="language-sql">CREATE TABLE table1 ( id INT, name VARCHAR(20) ); CREATE TABLE table2 ( id INT, name VARCHAR(20) );</code>
Run execution plan for queries using inner joins:
<code class="language-sql">-- 使用内连接 EXPLAIN PLAN FOR SELECT * FROM table1 t1 INNER JOIN table2 t2 ON t1.id = t2.id; SELECT * FROM TABLE (DBMS_XPLAN.DISPLAY);</code>
...gets the following output:
<code>-- 0 select statement -- 1 hash join (access("T1"."ID"="T2"."ID")) -- 2 table access full table1 -- 3 table access full table2</code>
Similarly, the execution plan for a query using the WHERE clause:
<code class="language-sql">-- 使用 WHERE 子句 EXPLAIN PLAN FOR SELECT * FROM table1 t1, table2 t2 WHERE t1.id = t2.id; SELECT * FROM TABLE (DBMS_XPLAN.DISPLAY);</code>
...returns the following output:
<code>-- 0 select statement -- 1 hash join (access("T1"."ID"="T2"."ID")) -- 2 table access full table1 -- 3 table access full table2</code>
As you can see, both queries use hash joins to perform join operations, and there is no significant difference in the execution plans.
Thus, the choice between joining tables using inner joins or WHERE clauses in Oracle mainly depends on personal preference or the specific needs of the database schema or query used.
The above is the detailed content of Inner Join vs. WHERE Clause in Oracle: What's the Real Performance Difference?. For more information, please follow other related articles on the PHP Chinese website!