Zum Hauptinhalt wechseln

Finding Unindexed Foreign Keys in Oracle

Ich bin über folgenden Blog-Post gestolpert: Finding Unindexed Foreign Keys in Oracle 

with owner_exclusion_list as (
    select
        username
    from dba_users
    where oracle_maintained = 'y'
    union all select 'ORDS_METADATA' from dual
    union all select 'ORDS_PUBLIC_USER' from dual
), constraint_columns as (
    select
        owner,
        table_name,
        constraint_name,
        listagg (column_name, ', ') within group (order by position) as constraint_column_list
    from dba_cons_columns
    join dba_constraints using (owner, table_name, constraint_name)
    where constraint_type = 'R' -- R = Referential Foreign Key Constraint
    and owner not in (
        select * from owner_exclusion_list
    )
    group by owner, table_name, constraint_name
), index_columns as (
    select
        index_owner as owner,
        table_name,
        index_name,
        listagg (column_name, ', ') within group (order by column_position) as index_column_list
    from dba_ind_columns
    where index_owner not in (
        select * from owner_exclusion_list
    )
    group by index_owner, table_name, index_name
)
select
    decode (ic.table_name, null, 'Could be missing', 'Exists')      as foreign_key_index,
    to_char (dbat.num_rows, '999,999,999,999,999,999')              as last_analyzed_row_count,
    dbat.last_analyzed,
    cc.owner,
    cc.table_name,
    constraint_name                                                 as foreign_key_constraint_name,
    constraint_column_list                                          as foreign_key_column_list,
    coalesce (index_name,'*** Possible Missing Index ***')          as index_name,
    coalesce (index_column_list,'*** Possible Missing Index ***' )  as index_column_list
from constraint_columns cc
join dba_tables dbat on (dbat.owner = cc.owner and dbat.table_name = cc.table_name)
left join index_columns ic on (cc.owner = ic.owner and cc.table_name = ic.table_name and ic.index_column_list like cc.constraint_column_list || '%')
where cc.owner = :MY_OWNER
order by dbat.num_rows desc nulls last, cc.owner, cc.table_name,  constraint_column_list;

 

In dem Zusammenhang gelernt:

SQL-Developer → Reports → All Reports → Database Administration → All Tables → Quality Assurance 

  • Tables with Unindexed Foreign Keys
  • Tables without Primary Keys
  • Tables without Indexes

 

Kommentare

Noch keine Kommentare zu diesem Beitrag.