How does Oracle handle joins?
When you write a SQL query joining two or more tables, you are essentially asking Oracle to solve a massive puzzle. Oracle handles this using a two-part strategy: picking the Join Order (which tables to look at first) and the Join Method (the physical algorithm used to connect them).
Think of the Query Optimizer as a foreman at a construction site. It looks at the size of the materials (tables) and decides whether it’s faster to use a hammer, a drill, or a crane.
Oracle has three main algorithms for joining data. The "best" one depends entirely on the volume of data and the presence of indexes.
This is the most common method for small datasets or highly specific searches.
How it works: Oracle takes one row from the "Outer" table and scans the "Inner" table for matches.
Best for: Small result sets and situations where you have a highly efficient index on the join column of the second table.
Analogy: Checking a stack of IDs against a guest list one by one.
This is the modern powerhouse for large datasets.
How it works: Oracle takes the smaller table, builds a "hash table" in memory (PGA), and then scans the larger table once. As it reads the larger table, it "probes" the hash table for instant matches.
Best for: Joining large tables where no suitable indexes exist, or when you need to process a huge percentage of the table.
Analogy: Creating an alphabetized checklist of one table so you can quickly check off items from the second table.
This is the "old school" method, used less frequently now that Hash Joins are so efficient.
How it works: Oracle sorts both datasets by the join column and then "merges" them together, like zipping up a jacket.
Best for: Queries using inequality operators (like A.id > B.id) or when the data is already sorted by a previous operation.
Analogy: Two lines of students sorted by height; you just walk down the lines and pair them up.
The order in which Oracle joins tables is just as important as the method. Joining 10 tables means there are over 3.6 million possible permutations!
The Driving Table: Oracle tries to pick the table that, after filtering, provides the smallest number of rows.
The Goal: Reduce the "data volume" as early in the plan as possible. If you filter 1 million rows down to 10 in the first step, the remaining joins will be lightning-fast.
When you look at an Execution Plan, the join method is clearly labeled. Look for the indentation to see which table is "driving."
---------------------------------------------------------------------------
| Id | Operation | Name | Rows | Cost (%CPU)|
---------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 10 | 4 (0)|
|* 1 | HASH JOIN | | 10 | 4 (0)|
| 2 | TABLE ACCESS FULL| DEPT | 4 | 3 (0)| <-- Driving Table
| 3 | TABLE ACCESS FULL| EMP | 14 | 3 (0)|
---------------------------------------------------------------------------
In the example above, Oracle chooses a Hash Join and uses DEPT as the driving table because it has fewer rows.
MERGE JOIN CARTESIAN)If you forget a join condition (e.g., WHERE a.id = b.id), Oracle has to join every row of Table A to every row of Table B. If Table A has 1,000 rows and Table B has 1,000, you get 1,000,000 rows. This is usually a sign of a bug in your SQL.
If you see a NESTED LOOPS joining two tables with millions of rows, it usually means you are missing an index or your Statistics are stale. Oracle thinks it's joining 10 rows, but it's actually joining 10 million, leading to a query that never ends.
| Method | Best For... | Performance Key |
| Nested Loops | OLTP, small data, Index lookups. | Requires an index on the inner table. |
| Hash Join | Data Warehouse, large joins, no indexes. | Requires enough memory (PGA). |
| Sort Merge | Range joins (>, <), non-indexed data. | High CPU usage for sorting. |
If you are 100% sure that Oracle is picking the wrong join order, you can use the /*+ LEADING(table_name) */ hint to force a specific table to be the starting point. But remember: 99% of the time, fixing your Statistics is a better solution than using hints!