能不能两张表共用一个INDEX?
答案:两张表的ROWID都不一样,怎么可能共用索引。多么强有力的论据。
但是从9i开始引入了Bitmap join indexes可以索引来自其他表的列:
SQL> conn scott/tiger@afc
Connected.
SQL> desc dept
Name Null?
---------------------------------------------------------------------------------------------------
DEPTNO NOT NULL
DNAME
LOC
SQL> desc emp
Name Null?
---------------------------------------------------------------------------------------------------
EMPNO NOT NULL
ENAME
JOB
MGR
HIREDATE
SAL
COMM
DEPTNO
SQL> create bitmap index emp_bm_idx
2 on emp( d.dname )
3 from emp e, dept d
4 where e.deptno = d.deptno;
Index created.
SQL> exec dbms_stats.gather_schema_stats('scott',cascade=>true);
PL/SQL procedure successfully completed.
SQL> set autotrace traceonly explain;
SQL> select count(*)
2 from emp, dept
3 where emp.deptno = dept.deptno
4 and dept.dname = 'SALES';
Execution Plan
----------------------------------------------------------
Plan hash value: 2538954156
------------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
------------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 3 | 1 (0)| 00:00:01 |
| 1 | SORT AGGREGATE | | 1 | 3 | | |
| 2 | BITMAP CONVERSION COUNT | | 5 | 15 | 1 (0)| 00:00:01 |
|* 3 | BITMAP INDEX SINGLE VALUE| EMP_BM_IDX | | | | |
------------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
3 - access("EMP"."SYS_NC00009$"='SALES')
但是有个前提:
Bitmap join indexes do have a prerequisite. The join condition must join to a primary or
unique key in the other table. In the preceding example, DEPT.DEPTNO is the primary key of the
DEPT table, and the primary key must be in place, otherwise an error will occur:
ops$tkyte@ORA10G> create bitmap index emp_bm_idx
2 on emp( d.dname )
3 from emp e, dept d
4 where e.deptno = d.deptno
5 /
from emp e, dept d
*
ERROR at line 3:
ORA-25954: missing primary key or unique constraint on dimension
参考地址:
http://www.itpub.net/viewthread.php?tid=719902&extra=&page=1
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/9252210/viewspace-586508/
,如需转载,请注明出处,否则将追究法律责任。