#!/usr/bin/ksh # author : B. Polarski # 20 September 2005 HOST=`hostname` HOST=`echo $HOST | awk '{ printf ("%-+15.15s",$1) }'` # ---------------------------------------------------- function get_ftype { if [ -n "$fpart" ];then ftype=`sqlplus -s "$CONNECT_STRING" < -t tbl -ext tbl -u -t -ldl tbl -u -ext -drop tbl -u -t -p -d tbl -u -t -ddl # get the ddl tbl -u -t -c -d tbl -u -t -pred # list table predicate usage tbl -u -t -a -d -i tbl -u -cd -x tbl -u -t -s tbl -u -log|-logc | -logu tbl -u -t -l tbl -t -lbs tbl -u -cl tbl -u -t -noidxfk tbl -u -ce -x tbl -n (Add a % before, after or at both end) to the part name tbl -txn -t -u < OWNER> [ -rn ] tbl -t -spc|-uspc -part [-u ] tbl -g -u -t tbl [-u ] -t -ch add to -t : ============== -u limit to owner -a Chained rows -c Constraints -d Table description -i additional info on table -lob show lobs info -p (sub)partitions -s List stats info gathered on columns -ldl display last ddl applied on the table -ddl extract ddl of table -drop drop external table -lbs List lob size distrubtion -spc List space map using dbms_space.space_usage -uspc List unused space using dbms_space.unused_space -dep List all dependent segemnts -txn List transaction available in flash. -pred show predicate usage; -ord # values are 3 to 7 and represent columns -g List table columns with histogram on them -ch List histograms on COLUMN_NAME -cl List shema Constraints. Constraint type is the letter representing the type of the constraint. type 'ALL' to see all constraintstype -cd Generate script to Disable shema|table Constraints. Constraint type is the letter representing the type of the constraint. type 'ALL' affect all constraintstype. If you omit table and leave only schema, then all constraints are taken in account -ce Generate script to Enable shema|table Constraints. Constraint type is the letter representing the type of the constraint. type 'ALL' affect all constraintstype. If you omit table and leave only schema, then all constraint are taken in account -truncate Generate script to truncate all tables in a given schema. '-x' option will not execute this. -ext List all externamal tables; drop it if -t -drop is added -noidxfk List Foreign key without index and the Parent table name and columns -n list all tables whose name is like %xxx%. You need to provide the % yourself : ie: tlb -u soe -n MV_% -log Supplemental log groups -logc supplemental column -logu supplemental column in table that without FK, PK or BITMAP Also you can use : 'idx -t ' to list associated indexes. It is intentional that -drop only access external table. If you want to drop a table, connect into db. The time it takes gives you a chance to realize what you do. Example: tbl -t EMP -u SCOTT -p # List all partitions tbl -t EMP -u SCOTT -g # display all colums of table EMP with histogram on them tbl -t EMP -u SCOTT -ch DEPNO # display the histogram on column DEPNO EOF exit } # ---------------------------------------------------- if [ -z "$1" ];then help fi PROMPT=" prompt . tbl -h for extended help prompt . use : idx -t to list associated indexes of this table " ALTERNATE_FIELD="b.num_rows snum," NUM_ROWS=30 desc=FALSE AND=" and " typeset -u ftable typeset -u fowner typeset -u CONS_TYPE while [ -n "$1" ] do case "$1" in -a ) req=chain ;; -c ) req=constraints ;; -ce ) req=enable_constraints ; CONS_TYPE=$2; shift ;; -cd ) req=disable_constraints ; CONS_TYPE=$2; shift ;; -ch ) req=histogram;COL_NAME=$2; shift ;; -cl ) req=list_constraints ; CONS_TYPE=$2; shift ;; -d ) desc=TRUE ;; -dep ) req=DEP ;; -ext ) EXTERNAL=TRUE if [ -n "$2" ]; then ftable=$2 shift fi;; -ldl ) SQL_DDL=TRUE ;; -ddl ) req=ddl ;; -drop ) DROP=TRUE ;; -g ) req=list_hist_col ;; -i ) ADDITIONAL_INFO=TRUE ;; -lbs ) req=dis ; COL=$2; shift;; -lob ) req=lobs;; -log ) req=LOG_GROUP ;; -logc ) req=LOG_COL ;; -logu ) req=LOG_NO_KEY ;; -n ) ftable=$2 ; TABLE_LIKE=" TABLE_NAME like '$ftable'" ; OBJECT_LIKE=" and OBJECT_NAME = '$ftable'" ; shift;; -noidxfk ) req=noidxfk ;; -ord ) ORD_PRED=$2; shift ;; -p ) req=part ;; -part ) fpart=$2 ; shift ;; -ph ) req=part_hv ;; -pred ) req=predicate_usage ;; -rn ) NUM_ROWS=$2 ; shift ;; -s ) req=stats ;; -spc ) req=SPACE_USAGE ;; -t ) ftable=$2 TABLE_LIKE=" TABLE_NAME = '$ftable'" AND_TABLE=" and TABLE_NAME = upper('$ftable') " OBJECT_LIKE=" and OBJECT_NAME = '$ftable'" shift;; -txn ) EXECUTE=YES; req=TXN ;; -truncate ) req="truncate" ;; -u ) fowner=$2 ; FOWNER="owner = '$fowner' " ; AND_FOWNER=" and $FOWNER" ; shift;; -uspc ) req=UNUSED_SPACE ;; -v ) SETXV="set -x" ;; -x ) EXECUTE="YES" ;; -h ) help ; exit ;; -w ) ALTERNATE_FIELD="(nvl(b.blocks,a.blocks) * &fsize)/1024/1024 fsize," ;; * ) echo "Invalid parameter $1" ;help ; exit ;; esac shift done FOUT=$SBIN/tmp/tbl_${ORACLE_SID}_${fowner}_${ftable}.log if [ -n "$SQL_DDL" ];then if [ -n "$FOWNER" ];then AND=" and " else unset AND fi SQL_DDL=" select object_name table_name, owner, to_char(created,'MM/DD/YYYY HH24:MI:SS') Created, to_char(last_ddl_time, 'MM/DD/YYYY HH24:MI:SS') last_ddl_time, temporary, object_id, data_object_id from dba_objects where object_type = 'TABLE' $AND_FOWNER $OBJECT_LIKE order by 2;" fi # -------------------------------------------------------------------------- . $SBIN/scripts/passwd.env . ${GET_PASSWD} SYS $ORACLE_SID if [ "x-$CONNECT_STRING" = "x-" ];then echo "could no get a the password of $S_USER" exit 0 fi # -------------------------------------------------------------------------- if [ "$EXTERNAL" = "TRUE" ];then FIELD_LIST="a.owner||'.'|| a.table_name tbl , a.DIRECTORY_NAME, b.directory_path||'/'||a.location fil" if [ -n "$ftable" ];then if [ "$DROP" = "TRUE" ];then SQL="DROP TABLE $ftable ;" sqlplus -s "$CONNECT_STRING" < \n" sqlplus -s "$CONNECT_STRING" < 9999999 then 10000 when fsize between 1000000 and 9999999 then 1000 when fsize between 100000 and 999999 then 100 when fsize between 50000 and 99999 then 50 when fsize between 30000 and 49999 then 30 when fsize between 20000 and 29999 then 20 when fsize between 10000 and 19999 then 10 when fsize between 9000 and 9999 then 9 when fsize between 8000 and 8999 then 8 when fsize between 7000 and 7999 then 7 when fsize between 6000 and 6999 then 6 when fsize between 5000 and 5999 then 5 when fsize between 4000 and 4999 then 4 when fsize between 3000 and 3999 then 3 when fsize between 2000 and 2999 then 2 when fsize between 1000 and 1999 then 1 when fsize between 1 and 999 then 0 when fsize = 0 then -1 else fsize end fsiz from ( select ceil(dbms_lob.getlength($COL))fsize from $fowner.$ftable) ) group by fsiz) order by 1 ; EOF # ........................................................... # histogram : show columns historgrams # ........................................................... elif [ "$req" = "histogram" ];then if [ -n "$FOWNER" ];then AND=" and" else unset AND fi sqlplus -s "$CONNECT_STRING" <) : 3=equality_preds 4=equijoin_preds 5=nonequijoin_preds prompt . : 5=range_preds 6=like_preds 7=null_preds select r.name ||'.'|| o.name "obj" , c.name "col1", equality_preds, equijoin_preds, nonequijoin_preds, range_preds, like_preds, null_preds, to_char(timestamp,'DD-MM-YY HH24:MI:SS') "Date" from sys.col_usage$ u, sys.obj$ o, sys.col$ c, sys.user$ r where o.obj# = u.obj# and $AND_TABLE $AND_OWNER c.obj# = u.obj# and c.col# = u.intcol# and o.owner# = r.user# and (u.equijoin_preds > 0 or u.nonequijoin_preds > 0) order by $ORD_PRED desc / EOF # ........................................................... # Columns histograms # ........................................................... elif [ "$req" = "list_hist_col" ];then if [ -n "$FOWNER" ];then AND=" and" else unset AND fi sqlplus -s "$CONNECT_STRING" < '||b.table_name||'.'||c.column_name col1, '('|| c.constraint_name|| ')' rc , c.position,substr(a.status,1,1) status, decode(a.delete_rule,'CASCADE','ON CASCADE',' ') drule from all_cons_columns d,all_cons_columns c,all_constraints a , all_constraints b where b.constraint_name = a.r_constraint_name and d.owner = a.r_owner and a.table_name = '$ftable' and d.constraint_name = a.constraint_name and d.owner = a.owner and c.constraint_name = a.r_constraint_name and c.owner = a.r_owner and d.position = c.position order by d.position / prompt prompt '-----------------------------' prompt . Who reference '$ftable' ? prompt '-----------------------------' prompt col rc form a61 head 'Remote|Constraint name' col col0 form a61 head 'Local |Table.Colmuns' select '['||a.owner||'].'||a.table_name||'.'||c.column_name||' ('||a.constraint_name||')' rc, '--> ['||a.r_owner||'].'||b.table_name||'.'||b.column_name ||' ('||a.r_constraint_name||')' col0 from all_constraints a , all_cons_columns b, all_cons_columns c where b.table_name= '$ftable' and b.constraint_name = a.r_constraint_name and c.constraint_name = a.constraint_name / prompt exit EOF # ........................................................... # extract DDL # ........................................................... elif [ "$req" = "ddl" ];then sqlplus -s "$CONNECT_STRING" < $FOUT ; rm $FOUT.tmp echo "results in $FOUT" exit # ........................................................... # Lobs # ........................................................... elif [ "$req" = "lobs" ];then if [ -n "$fowner" ];then AND_OWNER=" and us.owner = '$fowner' " AND_TOWNER=" and table_owner = '$fowner' " AND_I_OWNER=" and ui.owner='$fowner' and ui.owner = uip.index_owner " AND_IS_OWNER=" and ui.owner='$fowner' and ui.owner = uisp.index_owner " fi sqlplus -s "$CONNECT_STRING" < 0 $AND $FOWNER order by 100- (chain_cnt*100/num_rows) / EOF # ........................................................... else if [ -z "$fowner" ];then unset AND elif [ -z "$ftable" ];then unset AND fi if [ "$ADDITIONAL_INFO" = "TRUE" ];then # avg row per block = ( (avg free space in block/block_size)/100 ) * (max num of rows in a block) SQL_INFO1=" prompt select ROW_MOVEMENT, EMPTY_BLOCKS, CHAIN_CNT, AVG_ROW_LEN, round(nvl(floor(&fsize - 66 - INI_TRANS * 24)/greatest(AVG_ROW_LEN + 2, 11), 1),0) max_rpb, (1-(avg_space/&fsize))* round(nvl(floor(&fsize - 66 - INI_TRANS * 24)/greatest(AVG_ROW_LEN + 2, 11), 1),0) avg_rpb, num_rows/(trunc(nvl(floor(&fsize - 66 - INI_TRANS * 24)/greatest(AVG_ROW_LEN + 2, 11), 1),0))/blocks dens, trunc((1+FREELIST_GROUPS+BLOCKS)*&fsize) /1048576 HWM from dba_tables where table_name = '$ftable' $AND_FOWNER ;" SQL_INFO2="prompt SELECT AVG_SPACE_FREELIST_BLOCKS avg_sfp, INI_TRANS, MAX_TRANS, freelists, freelist_groups, trim(DEGREE) deg, CACHE, BUFFER_POOL, USER_STATS, IOT_TYPE, SAMPLE_SIZE from dba_tables where table_name = '$ftable' $AND_FOWNER; " fi sqlplus -s "$CONNECT_STRING" <