Tag Archives: code

Finding Unindexed Foreign Keys in Oracle, now with EVEN BETTER index creation DDL!

It all started here: https://carsandcode.com/2023/07/27/finding-unindexed-foreign-keys-in-oracle/

It got better here: https://carsandcode.com/2023/09/01/finding-unindexed-foreign-keys-in-oracle-now-with-index-creation-ddl/

But now, it’s even better!

Changes:

  • A comment section at the front.
  • Formatting changes.
  • Better table aliases.
  • Indexes are now owned by the table owner rather than the DBA running the script.
  • All missing indexes are created in a single tablespace named “missing_foreign_key_indexes”. Obviously, this tablespace needs to exist for the statements to work. You might want to change this clause to use “your user’s index tablespaces”, and you should drop this if you are on Oracle Autonomous on Oracle Cloud, since you only get one tablespace for your stuff and everything will automatically go to it.
  • There are suggestions for making shorter index names if needed/wanted.
  • The “local” clause was added for partitioned tables.
  • You can optionally require validated foreign keys, valid indexes, and visible indexes. Today’s invalid or invisible index can turn into tomorrow’s index, so I left the default to show all indexes.
  • We are now sorting numerically.
  • Change “select *” into “select username” for the not in clause that eliminates users.
  • Added a where clause to find missing indexes by default.

Again, here’s the formatted code that doesn’t look great, but if you copy it, it should be formatted as it is in the above picture.

-- Created by Rich Soule of Talan's Oracle Group in collaboration with Lance Eaton.
-- 
-- Notes:
--   * Can ignore unusable/invisible/bitmap indexes; prefers NORMAL (and function-based NORMAL) b-trees
--   * Emits LOCAL for partitioned child tables
--   * change 'missing_foreign_key_indexes' tablespace to taste (or drop on Autonomous)
--   * if you want to shorten the index names, you can replace the two lines with comments below with something like: 'missing_fk_index'||rownum
--   * designed to be run by a DBA with access to the DBA views, but can also be run by a regular user by replacing the dba_ views with 
--     all_ views or user_ views (search and replace dba_ with all_ or user_)

    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_column_list 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'
                                        and status = 'ENABLED'
                                     -- and validated = 'VALIDATED' -- uncomment to require validated fks
                                        and owner not in (select username from owner_exclusion_list)
                                   group by owner, table_name, constraint_name )
       , index_column_list as (      select di.owner
                                          , di.table_name
                                          , di.index_name
                                          , listagg(dic.column_name, ', ') within group (order by dic.column_position) as index_column_list
                                       from dba_indexes di
                                       join dba_ind_columns dic on (dic.index_owner = di.owner and dic.index_name  = di.index_name)
                                      where di.owner not in (select username from owner_exclusion_list)
                                     -- and di.status     = 'VALID'   -- uncomment to require valid indexes
                                     -- and di.visibility = 'VISIBLE' -- uncomment to require visible indexes
                                        and di.index_type in ('NORMAL','FUNCTION-BASED NORMAL')
                                   group by di.owner, di.table_name, di.index_name )
       , foreign_key_index_query as (select decode(icl.table_name, null, 'Missing', 'Exists')                      as index_existence
                                          , dt.num_rows                                                            as last_analyzed_row_count_number
                                          , to_char(dt.num_rows, '999,999,999,999,999')                            as last_analyzed_row_count 
                                          , dt.last_analyzed           
                                          , ccl.owner                                                              as table_owner
                                          , ccl.table_name           
                                          , ccl.constraint_name                                                    as foreign_key_name
                                          , ccl.constraint_column_list                                             as foreign_key_column_list
                                          , coalesce(icl.index_name,        '*** Missing Index ***')               as index_name 
                                          , coalesce(icl.index_column_list, '*** Missing Index ***')               as index_column_list
                                          , decode(icl.table_name, null,'create index "'||ccl.owner||'".'||
                                                                        lower(ccl.table_name||'_foreign_key_index_on_'|| -- Shorten these two lines to have
                                                                        replace(replace(ccl.constraint_column_list,',','_'),' '))|| -- smaller index names
                                                                        ' on "'||ccl.owner||'"."'||ccl.table_name||'"('||
                                                                        replace(replace(ccl.constraint_column_list,',','","'),' ')||')'||
                                                                        decode(dt.partitioned, 'YES', ' local', '')||
                                                                        ' tablespace missing_foreign_key_indexes;'
                                                                       ,'*** supporting index already exists ***') as create_index_ddl
                                       from constraint_column_list ccl
                                       join dba_tables dt on (dt.owner = ccl.owner and dt.table_name = ccl.table_name)
                                  left join index_column_list icl on (     icl.owner = ccl.owner and icl.table_name = ccl.table_name
                                                                       and icl.index_column_list like ccl.constraint_column_list || '%' ))
  select index_existence
       , last_analyzed_row_count
       , last_analyzed
       , table_owner
       , table_name
       , foreign_key_name
       , foreign_key_column_list
       , index_name
       , index_column_list
       , create_index_ddl
    from foreign_key_index_query
   where index_existence = 'Missing'  -- comment to see both Exists & Missing
order by last_analyzed_row_count_number desc nulls last, table_owner, table_name, foreign_key_column_list;

Too many audit logs? xargs to the rescue!

After watching a client pipe the results of a find command into a text file, and then edit the text file to add rm in front of each line and then turn the text file into a script and run it… I knew I needed to add this here.

Sometimes you have too many audit logs and rm *.aud returns the following:

-bash: /bin/rm: Argument list too long

If so, we can use xargs to remove them all:

find . -type f -name '*.aud' | xargs rm

Happy Linux’ing.


Command line editing with bash

I use Oracle Linux just about every day. I teach Oracle classes using Oracle Linux and work with a whole bunch of Oracle servers which, more often than not, use Oracle Linux (or sometimes Redhat Linux which is basically very similar to Oracle Linux). The vast majority of the time these systems are set up to use bash (From wikipedia: The name itself is an acronym, a pun, and a description. As an acronym, it stands for Bourne-again shell, referring to its objective as a free replacement for the Bourne shell. As a pun, it expressed that objective in a phrase that sounds similar to born again, a term for spiritual rebirth. The name is also descriptive of what it did, bashing together the features of sh, csh, and ksh.) as the default shell for users.

While teaching my classes, I frequently type commands while my students watch and it’s not unusual for me to make typing mistakes. I often bang the BACKSPACE key a bunch of times while removing a single word, or worse yet, I hold it down to erase and entire line and start all over again. Every time I did this I would think “I used to know how to do this better.”

Today I looked it up a book that I have sitting on my bookshelf, (I last read it in 1995), which I would highly recommend if you’re a Unix System Admin: Using csh & tcsh Type Less, Accomplish More. It’s a really great book if you are going to be using a Unix shell all day every day interactively. I read it way back when I was learning Unix and it made a huge difference in my productivity with interactive ‘shelling’. These days I’m mostly only working with Linux to get Oracle systems set up and configured correctly, so I’m usually sticking with the default shell… which is bash.

I tested out some of the things in the book and they work a bit differently in tcsh than they do in bash. Since I’ve become (largely by default) a bash user, here goes:

Remove the entire command line in bash with CTRL-u or CTLR-w

[oracle@multitenant ~]$ This is a command line that I want to erase

Type CTRL-u or CTRL-w (as an aside, CTRL-w in tcsh erases a single word) and voila:

[oracle@multitenant ~]$

Sometime I only mess up the last word…

Remove the last word typed in bash with ESC BACKSPACE

[oracle@multitenant ~]$ env | grep ORALCE

Oops! Messed up that ORACLE. Type ESC BACKSPACE and voila:

[oracle@multitenant ~]$ env | grep

Now I can correctly add ORACLE to the above without having to use backspace six times.

Reuse part of the previous commands arguments with !$, !^, !*

Often we’ll look at the contents of a file and then decide we want to edit it.

[oracle@multitenant ~]# cat /etc/oratab

Hmmm…. I see something I want to change, so rather than typing vi /etc/oratab I can just do the following:

[oracle@multitenant ~]# vi !$

!$ uses the last argument from the previous command, !^ uses the first argument from the previous command, and !* uses all arguments from the last command, so actually any of those would have worked in the example above. Remember that above I have two parts in the line I’m typing; The first is the actual command (cat) and then the second is the argument (/etc/oratab).

Hopefully this (along with the CTRL-r post from earlier) will help a few folks become just a bit more efficient in their day to day operations!