Showing posts with label Scripting. Show all posts
Showing posts with label Scripting. Show all posts

Monday, May 26, 2014

Is my index used ?

This oracle function will help you quickly check if an index is used or not, you have to enable his monitoring before

CREATE OR REPLACE FUNCTION is_index_used (owner         VARCHAR,
                                               table_name    VARCHAR,
                                               index_name    VARCHAR)
   RETURN VARCHAR
IS
   is_used   VARCHAR (3);
   cnt       NUMBER;
BEGIN
   is_used := 'NO';

   SELECT COUNT (*) CNT
     INTO cnt
     FROM SYS."_CURRENT_EDITION_OBJ" io,
          SYS."_CURRENT_EDITION_OBJ" T,
          sys.ind$ i,
          sys.USER$ iu,
          sys.USER$ tu,
          sys.object_usage ou
    WHERE     io.owner# = iu.USER#
          AND i.obj# = io.obj#
          AND io.obj# = ou.obj#(+)
          AND T.obj# = i.bo#
          AND T.owner# = Tu.USER#
          AND i.type# NOT IN (4, 8, 9)
          AND BITAND (io.flags, 128) <> 128
          AND iu.name = owner
          AND io.name = index_name
          AND T.name = table_name
          AND DECODE (BITAND (i.flags, 65536), 0, 'NO', 'YES') = 'YES'
          AND DECODE (BITAND (ou.flags, 1),  0, 'NO',  NULL, NULL,  'YES') =
                 'YES';

   IF cnt > 0
   THEN
      is_used := 'YES';
   ELSE
      is_used := 'NO';
   END IF;

   RETURN is_used;
EXCEPTION
   WHEN NO_DATA_FOUND
   THEN
      RETURN is_used;
   WHEN OTHERS
   THEN
      -- Consider logging the error and then re-raise
      RAISE;
END;
/

I recommend to wait minimum one week after enabling monitoring before you check it with a simple call like this ...

select INDEX_NAME,is_index_used(OWNER,TABLE_NAME,INDEX_NAME)  from dba_indexes where owner = 'FOMI' and table_name = 'ACCOUNTS'

Wednesday, July 25, 2012

Most useful grep Options

-h:   suppress file names in output
-w:   restrict search to whole words only
-c:   displays count of matching lines in each file, not the lines themselves
      (note this is not the same as $ grep index *.rb | wc -l)
-i:   ignore case
-l:   list only file names containing matching lines
-n:   precede each line with the line number where it was found
-v:   display all lines NOT matching pattern
-C:   Display NUM lines of context either side of matches
-B:   Display NUM lines of context Before the matches
-A:   Display NUM lines of context After the matches

-e pattern: 
      Search for a pattern. Can use multiple times on a line
-r:   Search recursively from the current or specificed directory
  --include=pattern:
      Only search in file names matching pattern
  --exclude=pattern:
      Do not search in file names matching pattern

Most useful awk programs

awk '{ if (NF > max) max = NF }
     END { print max }'
    This program prints the maximum number of fields on any input line. 
awk 'length($0) > 80'
    This program prints every line longer than 80 characters. The sole rule has
    a relational expression as its pattern, and has no action (so the default
    action, printing the record, is used).
awk 'NF > 0'
    This program prints every line that has at least one field. This is an easy
    way to delete blank lines from a file (or rather, to create a new file
    similar to the old file but from which the blank lines have been deleted).
awk '{ if (NF > 0) print }'
    This program also prints every line that has at least one field. Here we
    allow the rule to match every line, then decide in the action whether to
    print.
awk 'BEGIN { for (i = 1; i <= 7; i++)
               print int(101 * rand()) }'
    This program prints 7 random numbers from 0 to 100, inclusive. 
ls -l files | awk '{ x += $4 } ; END { print "total bytes: " x }'
    This program prints the total number of bytes used by files. 
expand file | awk '{ if (x < length()) x = length() }
                  END { print "maximum line length is " x }'
    This program prints the maximum line length of file. The input is piped
    through the expand program to change tabs into spaces, so the widths
    compared are actually the right-margin columns.
awk 'BEGIN { FS = ":" }
     { print $1 | "sort" }' /etc/passwd
    This program prints a sorted list of the login names of all users. 
awk '{ nlines++ }
     END { print nlines }'
    This programs counts lines in a file. 
awk 'END { print NR }'
    This program also counts lines in a file, but lets awk do the work. 
awk '{ print NR, $0 }'
    This program adds line numbers to all its input files, similar to `cat -n'.
awk -F "\"*,\"*" '{print $3}' file.csv
    CSV parsing, prints 3rd field

This can helps you remember the unix cron syntax


# * * * * * command to be executed
# - - - - -
# | | | | |
# | | | | +- - - - day of week (0 - 6) (Sunday=0)
# | | | +- - - - - month (1 - 12)
# | | +- - - - - - day of month (1 - 31)
# | +- - - - - - - hour (0 - 23)
# +- - - - - - - - minute (0 - 59)

Thursday, May 10, 2012

Apply chmod recursively on directories

suppose you want to chmod all subdirectories of current directory
find . -type d -exec chmod 755 {} \;


Similarly, the following will chmod all files only (and ignore the directories):
find . -type f -exec chmod 644 {} \;