- 论坛徽章:
- 0
|
oracle优化器的连接方式如何实现的?
take
select ENAME, DNAME from emp, dept where emp.DEPTNO=dept.DEPTNO;
for example.
assume emp has E rows, and dept has D rows
0) worst case
for each deptno in emp table, find the corresponding row in dept table through full table scan
this will do full table scan of table dept E times.
1) nested loop
for each deptno in emp table, find the corresponding row in dept table through index dept(deptno)
this will probe index dept(deptno) E times
2) sort merge
sort table emp by deptno and sort table dept by deptno
them joint the corresponding rows using two pointers. you just advance the two pinters a bit each time, and basically you will only need to scan the sorted tables once for each table
3) hash join
you build a hash table using one table and probe the hash table using the other table. for example
build a hash table using the dept table
assume the hash function is mod(DEPTNO, 7)
SQL>; select rowid, DEPTNO, DNAME, LOC from dept;
ROWID DEPTNO DNAME LOC
------------------ ---------- -------------- -------------
AAAD0AAAEAAAAAVAAA 10 ACCOUNTING NEW YORK
AAAD0AAAEAAAAAVAAB 20 RESEARCH DALLAS
AAAD0AAAEAAAAAVAAC 30 SALES CHICAGO
AAAD0AAAEAAAAAVAAD 40 OPERATIONS BOSTON
The hash table created looks like
HV ROWID
---------- ------------------
2 AAAD0AAAEAAAAAVAAC
3 AAAD0AAAEAAAAAVAAA
5 AAAD0AAAEAAAAAVAAD
6 AAAD0AAAEAAAAAVAAB
How do you get it?
deptno = 10 ->; mod(10, 7) ->; 3.
so hash value 3 corresponds to rowid AAAD0AAAEAAAAAVAAA
Now you can start to do the table join.
SQL>; select * from emp;
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- ---------- --------- ---------- --------- ---------- ---------- ----------
7369 SMITH CLERK 7902 17-DEC-80 800 20
...............
SMITH works in deptno 20, and it's hash value is mod(20,7) = 6,
so we use 6 as the index to probe the hash table, and find the the row in dept has physical address AAAD0AAAEAAAAAVAAB, and then we go that physical address to the corresponding row in the dept table. |
|