Doing a SQL rollup in unix


 
Thread Tools Search this Thread
Top Forums Programming Doing a SQL rollup in unix
# 1  
Old 05-24-2012
Doing a SQL rollup in unix

If anyone is familiar with Oracle, there is a way to trace a file. Then there is is a script called tkprof that will generate a report that includes a rollup of some values. The problem with this script is that it only does the rollup properly if the query is finished. So I need something quick and dirty that I can run while the query is running so I can get an idea of what part of the query is taking the longest.

There are ways to do this in the DB, but I want to try this.
Code:
 WAIT #10: nam='db file sequential read' ela= 12563 file#=1785 block#=1900856 blocks=1 obj#=378783 tim=5692604680960
 WAIT #10: nam='db file sequential read' ela= 12183 file#=1812 block#=57235 blocks=1 obj#=378760 tim=5692604693206
 WAIT #10: nam='db file sequential read' ela= 13177 file#=1785 block#=1914568 blocks=1 obj#=378783 tim=5692604707687
 WAIT #10: nam='db file sequential read' ela= 22778 file#=1759 block#=1849055 blocks=1 obj#=378760 tim=5692604730526
 WAIT #10: nam='db file sequential read' ela= 9205 file#=1812 block#=15570 blocks=1 obj#=378760 tim=5692604739987
 WAIT #10: nam='db file sequential read' ela= 483 file#=1812 block#=81772 blocks=1 obj#=378760 tim=5692604740550
 WAIT #10: nam='db file sequential read' ela= 293 file#=1812 block#=81646 blocks=1 obj#=378760 tim=5692604741094
 WAIT #10: nam='db file sequential read' ela= 372 file#=1825 block#=12076 blocks=1 obj#=378783 tim=5692604742348
 WAIT #10: nam='db file sequential read' ela= 10610 file#=1812 block#=122033 blocks=1 obj#=378760 tim=5692604753034
 WAIT #10: nam='db file sequential read' ela= 17977 file#=1812 block#=114700 blocks=1 obj#=378760 tim=5692604771284

I need to grep on WAIT

grep "WAIT" filename |

The awk is an issue. the nam= part can change. so a standard awk on spaces wont work.

basically I want to do this

for each obj#=<value>, I want to sum up the ela and tim fields. and I want to do a count of the total number of entries.

note that the tim= and ela= needs to be parsed out so I can some up the numbers.
# 2  
Old 05-24-2012
Quote:
it only does the rollup properly if the query is finished
You could still parse the incomplete trace file with tkprof to get
the overall statistics ...
# 3  
Old 05-25-2012
The following is my take on it, using only shell builtins. I had to trim the first few digits of "tim" to use integer arithmetics (otherwise there would be overruns), you might want to find a more "long-term solution" for this. After all, the script is still a quick hack.

After the sorting and parsing it comes down to a simple group change with "$obj" as the group identifier.

Code:
#!/bin/ksh

print_summary ()
{

typeset obj="$1"
typeset -i elasum="$2"
typeset -i timsum="$3"

print - "Summary Object: $obj"
print - " - Sum ela: $elasum"
print - " - Sum tim: $timsum"

return 0
}



# main ()
typeset    obj=""
typeset    ela=0
typeset    time=0
typeset    lastobj=""
typeset -i elasum=0
typeset -i timsum=0
typeset    infile="/home/bakunin/tmp/oralog/input"

IFS=' '
grep '^WAIT' $infile |\
sort -k 12.6 |\
while read x x x x x x x ela x x x obj time x ; do
     obj="${obj#obj#=}"                     # "cook" the input
     time="${time#tim=5692604}"
     # print obj=$obj ela=$ela tim=$tim     # just for testing
                                            # simple group change
     if [ "$obj" != "$lastobj" ] ; then
          if [ -n "$lastobj" ] ; then
               print_summary "$obj" $elasum $timsum
               elasum=0
               timsum=0
          fi
          lastobj="$obj"
     fi

     (( elasum += ela ))
     (( timsum += time ))
done

print_summary "$obj" $elasum $timsum        # print last group

exit 0

I hope this helps.

bakunin

Last edited by bakunin; 05-25-2012 at 03:01 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

5 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How can i run sql queries from UNIX shell script and retrieve data into text docs of UNIX?

Please share the doc asap as very urgently required. (1 Reply)
Discussion started by: 24ajay
1 Replies

2. Shell Programming and Scripting

rollup and concat fields in a group

Hello, I have a file with two fields in the following format.. NewYork|Rob Boston|Mellisa NewYork|Kevin Boston|John Chicago|Mike Boston|Tom My output should be: NewYork|Rob,Kevin Boston|Mellisa,John,Tom Chicago|Mike Basically I need to rollup on column A and stringconcat column... (5 Replies)
Discussion started by: bperl
5 Replies

3. Shell Programming and Scripting

How to use sql data file in unix csv file as input to an sql query from shell

Hi , I used the below script to get the sql data into csv file using unix scripting. I m getting the output into an output file but the output file is not displayed in a separe columns . #!/bin/ksh export FILE_PATH=/maav/home/xyz/abc/ rm $FILE_PATH/sample.csv sqlplus -s... (2 Replies)
Discussion started by: Nareshp
2 Replies

4. UNIX for Dummies Questions & Answers

Execute PL/SQL function from Unix script (.sql file)

Hi guys, I am new on here, I have a function in oracle that returns a specific value: create or replace PACKAGE BODY "CTC_ASDGET_SCHED" AS FUNCTION FN_ASDSCHEDULE_GET RETURN VARCHAR2 AS BEGIN DECLARE ASDSchedule varchar2(6); ASDComplete... (1 Reply)
Discussion started by: reptile
1 Replies

5. Shell Programming and Scripting

unix variables from sql / pl/sql

How do I dynamically assign the below output to unix shell variables so I can build a menu in a shell script? Example: var1 = 1 var2= SYSTEM var3 = 2 var4= UNDOTBS1 and so on, then in the shell script I can use the variables to build a menu. set serveroutput on declare... (2 Replies)
Discussion started by: djehres
2 Replies
Login or Register to Ask a Question