此脚本不依赖统计信息。

    建议在外键列上创建索引,外键列不创建索引容易导致死锁。级联删除的时候,外键列没有索引会导致表被全表扫描。以下脚本抓出 Scott 账户下外键没创建索引的表。

    1. with cons as (select /*+ materialize */ owner, table_name, constraint_name
    2. from dba_constraints
    3. where owner = 'SCOTT'
    4. AND constraint_type = 'R'),
    5. idx as (
    6. select /*+ materialize */ table_owner,table_name, column_name
    7. from dba_ind_columns
    8. where table_owner = 'SCOTT')
    9. select owner,table_name,constraint_name,column_name
    10. from dba_cons_columns
    11. where (owner,table_name, constraint_name) in
    12. (select * from cons)
    13. and (owner,table_name, column_name) not in
    14. (select * from idx);

    在 Scott 账户中,EMP 表的 deptno 列引用了 DEPT 表的 deptno 列,但是没有创建索引,因此我们通过脚本可以将其抓出。

    1. with cons as (select /*+ materialize */ owner, table_name, constraint_name
    2. from dba_constraints
    3. where owner = 'SCOTT'
    4. AND constraint_type = 'R'),
    5. idx as (
    6. select /*+ materialize */ table_owner,table_name, column_name
    7. from dba_ind_columns
    8. where table_owner = 'SCOTT')
    9. select owner,table_name,constraint_name,column_name
    10. from dba_cons_columns
    11. where (owner,table_name, constraint_name) in
    12. (select * from cons)
    13. and (owner,table_name, column_name) not in
    14. (select * from idx);
    1. OWNER TABLE_NAME CONSTRAINT_NAME COLUMN_NAME
    2. -------- --------------- -------------------- --------------------
    3. SCOTT EMP FK_DEPTNO DEPTNO