Wednesday 25 December 2013

Create .bat file which will open all files you wanted when you logon

run.bat
start C:\Program Files\Microsoft Office\Office12\excel.exe  file1.xls
start C:\Program Files\Microsoft Office\Office12\excel.exe  file2.xls
....more command

Put .bat file in Startup folder


C:\Users\xxxx\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup\ru.bat

REM copy all files from source to destination based on changed file, also it overwrite existing file based on latest update file from source

maint.bat
start xcopy c:\docs d:\cocs  /D /E /Y

Autosys- If jobs fails but its still successs in autosys.

If you have situation where your autosys calls any Shell script and it gets failed but still it says success in autosys dashboard, then you need to check exit statement in shell script (any code other that 0 autosys counts as failure)

e.g.

If [ expression]; then
        echo "Job failed"
        exit 1
else
        echo "Job success"
        exit 0
fi.

Sunday 8 December 2013

Parameter need to Change for Performance Tuning

Below setting can Improve overall performance of whole DB's


Default values 
optimizer_index_cost_adj=100  --> Low value means index scan is less costly (Low value force index use)
optimizer_index_caching=0     --> High cache means more chance for nested loop first in below loop style
 
  • Nested loop joins
  • Hash join access
  • Full-index scans
  • Full-table scan access 
 
I have a query that performs bad (did not return after more than one minute). 
After I issued the following if worked well.
alter session set optimizer_index_cost_adj=5
alter session set optimizer_index_caching=90
 
Can use hint in sql statement.
 
 
select /*+ opt_param('optimizer_mode','first_rows_10') */ col1, col2 . . .
select /*+ opt_param('optimizer_index_cost_adj',20) */ col1, col2 . .
 
 

Wednesday 2 October 2013

SQL Top wait

col c1 heading 'Average Waits for|Full scan Read I/O' format 9999.99
col c2 heading 'Average Waits for|Index Read I/O' format 9999.99
col c3 heading 'Percent of| I/O Waits |for Full scan Read I/O' format 9999.99
col c4 heading 'Average Waits |for Full scan Read I/O' format 9999.99
col c4 heading 'starting values for optimize_index_cost_adj' format 9999.99

select 
a.AVERAGE_WAIT,
b.AVERAGE_WAIT,
a.TOTAL_WAITS/(a.TOTAL_WAITS+b.TOTAL_WAITS),
b.TOTAL_WAITS/(a.TOTAL_WAITS+b.TOTAL_WAITS),
(a.AVERAGE_WAIT/b.AVERAGE_WAIT)*100
from v$system_event a,
     v$system_event b
where a.EVENT = 'db file scattered read'    
and a.EVENT = 'db file sequential read'

-- space
/* SQL Analyze(110,1) */ select profile, USERNAME,  count(*)NO_OF_SEGS ,sum(b.bytes)/1024/1024 MB,
DEFAULT_TABLESPACE,TEMPORARY_TABLESPACE TEMP_TBS ,CREATED
,ACCOUNT_STATUS from dba_users a, dba_segments b
where a.username=b.owner(+)
group by USERNAME,DEFAULT_TABLESPACE,TEMPORARY_TABLESPACE,CREATED,ACCOUNT_STATUS,profile
order by created,NO_OF_SEGS desc,mb desc


--- Sql cost with sql_id--------------
col c1 heading SQL|ID
col c2 heading Cost format 9,999,999
col c3 heading 'SQL Text' format a200
select p.sql_id c1,
       p.cost   c2,
       /*to_char(*/s.sql_text/*) c3*/
 from
    dba_hist_sql_plan p,
    dba_hist_sqltext s
where p.id=0
and p.sql_id = s.sql_id
and p.cost is not null
order by p.cost desc;   

   
sql history with r. to. date

select
to_char(s.begin_interval_time,'mm-dd hh24') c1,
p.sql_id ,
p.executions_delta ,
p.buffer_gets_delta ,
p.disk_reads_delta ,
p.iowait_delta,
p.apwait_delta,
p.ccwait_delta
 from
dba_hist_sqlstat p,
dba_hist_snapshot s
where p.snap_id = s.snap_id;


Cumulative Usage od database indexes

select
trunc(s.begin_interval_time),
p.object_name,
sum(t.disk_reads_total),
sum(t.rows_processed_total)
 from
dba_hist_sql_plan p,
dba_hist_sqlstat t,
dba_hist_snapshot s
where p.sql_id = t.sql_id
and t.snap_id = s.snap_id
and p.object_type like '%INDEX%'
group by trunc(s.begin_interval_time),
p.object_name;


-- top sql in last 10 min
-------------------------
select
sql_id,
count(*),
round(count(*)/sum(count(*)) over (),2) "% load" from
v$active_session_history a
where a.SAMPLE_TIME > sysdate - 10/24/60
and a.SESSION_TYPE <> 'BACKGROUND'
group by sql_id

--- USER wose CPU usage----
SELECT se.username, ss.sid, ROUND (value/100) "CPU Usage"
FROM v$session se, v$sesstat ss, v$statname st
WHERE ss.statistic# = st.statistic#
   AND name LIKE  '%CPU used by this session%'
   AND se.sid = ss.SID
   AND se.username IS NOT NULL
  ORDER BY value DESC;

--- same sql in library cache
select
b.sql_id,
count(*),
round(count(*)/sum(count(*)) over (),2) from
v$sql a,
dba_hist_active_sess_history b
where a.SQL_ID = b.sql_id
and a.SQL_FULLTEXT like '%orders%'
group by b.sql_id
order by 2 desc


--- wait on data file useful for striping object
select
f.file_name,
count(*),
sum(h.TIME_WAITED)
from
v$active_session_history h,
dba_data_files f
where  h.CURRENT_FILE#=f.file_id
group by f.file_name
order by 3 desc


--- list of resource on high demand in last 1 hour
select
e.NAME,
sum(h.WAIT_TIME + h.TIME_WAITED)
from
v$active_session_history h,
v$event_name e
where  h.EVENT_ID=e.EVENT_ID
and e.WAIT_CLASS<>'Idle'
group by e.NAME
order by 2 desc
;
SELECT  C.SQL_TEXT,
        B.NAME,
        COUNT(*),
        SUM(TIME_WAITED)
FROM    v$ACTIVE_SESSION_HISTORY A,
        v$EVENT_NAME B,
        v$SQLAREA C
WHERE   A.SAMPLE_TIME BETWEEN '10-JUL-04 09:57:00 PM' AND
                              '10-JUL-04 09:59:00 PM' AND
        A.EVENT# = B.EVENT# AND
        A.SESSION_ID= 123 AND
        A.SQL_ID = C.SQL_ID

GROUP BY C.SQL_TEXT, B.NAME
-- object and there waits
select
o.owner,
o.object_name,
o.object_type,
sum(h.WAIT_TIME + h.TIME_WAITED)
from
v$active_session_history h,
v$event_name e,
dba_objects o
where  h.EVENT_ID=e.EVENT_ID
and h.CURRENT_OBJ#=o.object_id
and e.WAIT_CLASS<>'Idle'
group by o.owner,
o.object_name,
o.object_type
order by 4 desc


-- space managements object adviced for shrink orginaiosed

create or replace function bfsfun (so  in varchar2,
 stype in varchar2 default null )
  return bfsset pipelined
  is
   o bfs := bfs(null,null,null,null,null,null,null,null);
fs1_b number;
fs2_b number;
fs3_b number;
fs4_b number;

fs1_b1 number;
fs2_b1 number;
fs3_b1 number;
fs4_b1 number;
fulb number;
fulb1 number;           
u_b number;
u_b1 number; 
begin
  /*create type bfs as object
(
so varchar2(30),
st varchar2(30),
sn varchar2(100),
fs1 number,
fs2 number,
fs3 number,
fs4 number,
fb number
);

create type bfsset as table of bfs;
*/

for rec in (
select  s.owner,s.segment_name,s.segment_type from dba_segments s
where owner = so and s.segment_type = nvl(stype,s.segment_type ))
loop
 dbms_space.space_usage
 (segment_owner => rec.owner,
 segment_name => rec.segment_name,
 segment_type => rec.segment_type,
 fs1_bytes => fs1_b,
 fs1_blocks => fs1_b1,
 fs2_bytes => fs2_b,
 fs2_blocks => fs2_b1,
 fs3_bytes => fs3_b,
 fs3_blocks => fs3_b1,
 fs4_bytes => fs4_b,
 fs4_blocks => fs4_b1,
 full_bytes => fulb,
 full_blocks => fulb1,
 unformatted_blocks => u_b1,
 unformatted_bytes => u_b
 );
 o.so:=rec.owner;
 o.st:= rec.segment_type;
 o.sn:=rec.segment_name;

 o.fs1:=fs1_b1;
 o.fs2:=fs2_b1;
 o.fs3:=fs3_b1;
 o.fs4:=fs4_b1;
 o.fb:=fulb1;

 pipe row(o);

 end loop;


  return;
end ;

0-25 25-50 50-75 75-100% free space Full Blocks
select * from
table(bfsfun('&un','TABLE'))
order by fs4 desc

--
alter table t enable row movement;
alter table t shrink space compact;
cascade if index also used

Saturday 28 September 2013

GGS_Heartbit enable trigger on replicator when using DBOPTIONS SUPPRESSTRIGGER

Below procedure on DB level on replication side can make trigger fires on below two table which is important  for Golden Gate monitoring when we have disable trigger on DB side


DBMS_DDL.SET_TRIGGER_FIRING_PROPERTY('GGS_ADMIN','GGS_HEARTBEAT','FALSE')
DBMS_DDL.SET_TRIGGER_FIRING_PROPERTY('GGS_ADMIN','GGS_HEARTBEAT_HISTORY','FALSE')

Saturday 21 September 2013

Useful one liner awk, perl, sed

Very useful handy 1 liners needed to be successful, good Administrator. Its very handy to troubleshoot issue Smartly.


Sed (stream editor): Sed id a stream editor is used to perform basic text transformations on an input stream (a file or input from a pipeline).

AWK (Aho, Weinberger, and Kernighan):  is a programming language designed for text processing and typically used as a data extraction and reporting tool. It is a standard feature of most Unix-like operating system

Perl (Practical Extraction and Report Language):

Perl is a powerful text processing facility provider that makes report processing easier. 


Below few one liner we can use in day to day Linux shell programming and some string management:


1) find and Replace in file
perl -pi -e 's/first/second/g' file1 file2 file3

2) find and replace only on line matching word "single"
perl -pi -e 's/first/second/g if /single/' file

perl -ne 'print if $a{$_}++' file

3) Print the N records after some pattern:

awk 'c&&c--;/pattern/{c=N}' file


4) Kill all process matching pattern

kill -9 $(ps -ef|grep search_string|grep -v grep|awk -F " " '{ print $2 }')

kill -9 `ps -ef |awk ' /search_string/ {print $2} '`

5) delete ALL blank lines from a file (same as "grep '.' ")
 awk NF
 awk '/./'

6) remove duplicate, nonconsecutive lines
 awk '!a[$0]++'                     # most concise script
 awk '!($0 in a){a[$0];print}'      # fast

7) print the line immediately after a regexp, but not the line
 # containing the regexp
 sed -n '/regexp/{n;p;}'

7) print 1 line of context before and after regexp, with line number
 # indicating where the regexp occurred (similar to "grep -A1 -B1")
 sed -n -e '/regexp/{=;x;1!p;g;$!N;p;D;}' -e h


8) print section of file based on line numbers 

 sed -n '18,121p'               # way 1
 sed '18,121!d'                 # way 2


9) awk condition Check
YESTERDAY=$(date --date "1 days ago" +%d%m%y)
DATAFILE=visitor_log_$YESTERDAY.txt
#!/usr/bin/expect --
#cat $DATAFILE > VISITOR_LOG_BIG
cat DILIP_log.bad >> ALL_DILIP_log.bad

awk -F '|' '{if((length($1)==12)&&($1>910000000000)&&($1<=919999999999)) print $1,"|",$2,"|",$9}' $DATAFILE | sort | uniq >  VISITOR_LOG_BIG



Sunday 15 September 2013

SQL Wise CPU Usage

SQL Wise CPU Usage

select
   ss.username,
   se.SID,
   VALUE/100 cpu_usage_seconds
from v$session ss, v$sesstat se, v$statname sn
where   se.STATISTIC# = sn.STATISTIC#
and   NAME like '%CPU used by this session%'
and   se.SID = ss.SID
and   ss.status='ACTIVE'
and   ss.username is not null
order by VALUE desc;

col type for a10
select * from (
select
ash.SQL_ID , ash.SQL_PLAN_HASH_VALUE Plan_hash, aud.name type,
sum(decode(ash.session_state,'ON CPU',1,0)) "CPU",
sum(decode(ash.session_state,'WAITING',1,0)) -
sum(decode(ash.session_state,'WAITING', decode(wait_class, 'User I/O',1,0),0)) "WAIT" ,
sum(decode(ash.session_state,'WAITING', decode(wait_class, 'User I/O',1,0),0)) "IO" ,
sum(decode(ash.session_state,'ON CPU',1,1)) "TOTAL"
from v$active_session_history ash,
audit_actions aud
where SQL_ID is not NULL
and ash.sql_opcode=aud.action
and ash.sample_time > sysdate - &minutes /( 60*24)
group by sql_id, SQL_PLAN_HASH_VALUE , aud.name
order by sum(decode(session_state,'ON CPU',1,1)) desc
) where rownum < 10
/

AWR Reports

Oracle support Various AWR report based on requirement one can use


1)  AWR various Report.

SQL> @?/rdbms/admin/awrsqrpt.sql --> awr report for only single sql_id
SQL> @?/rdbms/admin/awrrpt.sql   --> Traditional awr report for instance.

2) RAC Related awr Report


In 11gR2 there are two new scripts awrgrpt.sql AND awrgdrpt.sql for RAC

SQL> @?/rdbms/admin/awrgrpt.sql -- AWR Global Report (RAC) (global report)
SQL> @?/rdbms/admin/awrgdrpt.sql -- AWR Global Diff Report (RAC)
 

Other important scripts under $ORACLE_HOME/rdbms/admin

SQL> @?/rdbms/admin/spawrrac.sql -- Server Performance RAC report
SQL> @?/rdbms/admin/awrsqrpt.sql -- Standard SQL statement Report
SQL> @?/rdbms/admin/awrddrpt.sql -- Period diff on current instance
SQL> @?/rdbms/admin/awrrpti.sql -- Workload Repository Report Instance (RAC)


Advance level SQL tuning one need to look into tkprof etc.

Saturday 14 September 2013

Tuning SQL not running as expected.

Scenario: 

Many times we see complains from application team, as till yesterday query execution was perfectly right and suddenly they started observing lag in execution or and some cases not even giving any result. When DBA check, mostly he see plan change and that is due many reasons. Below are few checks DBA can try to find why sql plan got changed. 

Though to Oracle Optimizer, you can just give hint if its a small query and you understand the execution flow as well as you know the data. But for many large queries its not possible to fix with hint, you have to live with Oracle Optimizer to decide the SQL plan. 

You can also forcefully map any plan to particular SQL ID, but that option need to consider carefully.

Why Plan got changed? or Why SQL is slow check few option mentioned below.

1) Check for any stale statistics.
col TABLE_NAME for a30
col PARTITION_NAME for a20
col SUBPARTITION_NAME for a20
select OWNER,TABLE_NAME,PARTITION_NAME,SUBPARTITION_NAME,NUM_ROWS,LAST_ANALYZED from dba_TAB_STATISTICS where STALE_STATS='YES';

2) Check any invalid index/Partition

col TABLE_NAME for a30
SYS@orcl>  select owner,index_name,TABLE_NAME,NUM_ROWS,LAST_ANALYZED,STATUS from dba_indexes where status not in ('VALID','N/A');

no rows selected

SYS@orcl>  select INDEX_OWNER,INDEX_NAME,PARTITION_NAME,SUBPARTITION_COUNT,LAST_ANALYZED,STATUS from dba_ind_partitions where status <> 'USABLE';

no rows selected

3) Check free memory shared pool area. Check too much hard parsing.

SYS@orcl> select * from (select SQL_ID,PARSING_SCHEMA_NAME, count(1) from v$sql group by SQL_ID,PARSING_SCHEMA_NAME order by  3 desc,2) where rownum<=10;

SQL_ID        PARSING_SCHEMA_NAME                     COUNT(1)
------------- ------------------------------ -----------------
9p6bq1v54k13j SYS                                            4
f8pavn1bvsj7t SYS                                            3
bgjhtnqhr5u9h SYS                                            3
ga9j9xk5cy9s0 SYS                                            3
05sghzkq6r6yv SYS                                            3
53saa2zkr6wc3 SYS                                            3
32bhha21dkv0v SYS                                            3
87gaftwrm2h68 SYS                                            3
b1wc53ddd6h3p SYS                                            3
asvzxj61dc5vs SYS                                            3

10 rows selected.

5) Wait/ Blocking analysis.

-- Display blocked session and their blocking session details.
SELECT sid, serial#, blocking_session_status, blocking_session
FROM   v$session
WHERE  blocking_session IS NOT NULL;


-- Display the resource or event the session is waiting for more than 1 minutes
SELECT sid, serial#, event, (seconds_in_wait/1000000) seconds_in_wait
FROM   v$session
where (seconds_in_wait/1000000) > 60
ORDER BY sid;

select sid,seq#,event,state,SECONDS_IN_WAIT from v$session_wait where SECONDS_IN_WAIT > 60;


--Monitor Top Waiting Event Using Active Session History (ASH)
SELECT h.event,
SUM(h.wait_time + h.time_waited) "Total Wait Time (ms)"
FROM v$active_session_history h, v$sqlarea SQL, dba_users u, v$event_name e
WHERE h.sample_time BETWEEN sysdate - 1/24 AND sysdate --event in the last hour
AND h.sql_id = SQL.sql_id
AND h.user_id = u.user_id
AND h.event# = e.event#
GROUP BY h.event
ORDER BY SUM(h.wait_time + h.time_waited) DESC;

6) Tablespace Usage


--Monitor Overall Oracle Tablespace
SELECT d.STATUS "Status",
d.tablespace_name "Name",
d.contents "Type",
d.extent_management "Extent Management",
d.initial_extent "Initial Extent",
TO_CHAR(NVL(a.bytes / 1024 / 1024, 0),'99,999,990.900') "Size (M)",
TO_CHAR(NVL(a.bytes - NVL(f.bytes, 0), 0)/1024/1024,'99,999,999.999') "Used (M)",
TO_CHAR(NVL((a.bytes - NVL(f.bytes, 0)) / a.bytes * 100, 0), '990.00') "Used %",
TO_CHAR(NVL(a.maxbytes / 1024 / 1024, 0),'99,999,990.900') "MaxSize (M)",
TO_CHAR(NVL((a.bytes - NVL(f.bytes, 0)) / a.maxbytes * 100, 0), '990.00') "Used % of Max"
FROM sys.dba_tablespaces d,
(SELECT tablespace_name,
SUM(bytes) bytes,
SUM(decode(autoextensible,'NO',bytes,'YES',maxbytes))
maxbytes FROM dba_data_files GROUP BY tablespace_name) a,
(SELECT tablespace_name, SUM(bytes) bytes FROM dba_free_space
GROUP BY tablespace_name) f
WHERE d.tablespace_name = a.tablespace_name(+)
AND d.tablespace_name = f.tablespace_name(+)
ORDER BY 10 DESC;

7) Check for Memory parameters usage




--Estimation Of Shared Memory Pool Size vs. Time Saved
SELECT shared_pool_size_for_estimate "Pool Size (MB)",
estd_lc_size "Lib Cache Size (MB)",
estd_lc_time_saved/1000000 "Lib Cache Time Saved (Sec)"
FROM v$shared_pool_advice;


--Estimation Of Buffer Cache Size vs. Physical Reads
SELECT size_for_estimate "Cache Size (MB)",
buffers_for_estimate "Buffers",
estd_physical_read_factor "Estd Phys|Read Factor",
estd_physical_reads "Estd Phys| Reads"
FROM v$db_cache_advice
WHERE name = 'DEFAULT'
AND block_size = (SELECT VALUE FROM V$PARAMETER WHERE name = 'db_block_size')
AND advice_status = 'ON';

---10g onwards
SYS@orcl> ---10g onwards
SYS@orcl> --SGA Advisor
SYS@orcl> select sga_size,sga_size_factor,estd_db_time from v$sga_target_advice;

         SGA_SIZE   SGA_SIZE_FACTOR      ESTD_DB_TIME
----------------- ----------------- -----------------
              512                 1              3405
              256                 1              3724
              384                 1              3489
              640                 1              3385
              768                 2              2583
              896                 2              2547
             1024                 2              2547

7 rows selected.

SYS@orcl> ---10g onwards
SYS@orcl> --SGA Advisor
SYS@orcl> select sga_size,sga_size_factor,estd_db_time from v$sga_target_advice;

         SGA_SIZE   SGA_SIZE_FACTOR      ESTD_DB_TIME
----------------- ----------------- -----------------
              512                 1              3405
              256                 1              3724
              384                 1              3489
              640                 1              3385
              768                 2              2583
              896                 2              2547
             1024                 2              2547

7 rows selected.

SYS@orcl>
SYS@orcl> ---PGA Advisor
SYS@orcl> select pga_target_for_estimate,pga_target_factor,estd_extra_bytes_rw from v$pga_target_advice;

PGA_TARGET_FOR_ESTIMATE PGA_TARGET_FACTOR ESTD_EXTRA_BYTES_RW
----------------------- ----------------- -------------------
               40370176                 0           157892608
               80740352                 0           157892608
              161480704                 1           157892608
              242221056                 1                   0
              322961408                 1                   0
              387553280                 1                   0
              452145152                 1                   0
              516738048                 2                   0
              581329920                 2                   0
              645922816                 2                   0
              968884224                 3                   0
             1291845632                 4                   0
             1937768448                 6                   0
             2583691264                 8                   0

14 rows selected.


8) Tuning SQL using 

SQL> @?/rdbms/admin/sqltrpt

9)  AWR various Report.

SQL> @?/rdbms/admin/awrsqrpt.sql --> awr report for only single sql_id
SQL> @?/rdbms/admin/awrrpt.sql   --> Traditional awr report for instance.

10) RAC Related awr Report


In 11gR2 there are two new scripts awrgrpt.sql AND awrgdrpt.sql for RAC

SQL> @?/rdbms/admin/awrgrpt.sql -- AWR Global Report (RAC) (global report)
SQL> @?/rdbms/admin/awrgdrpt.sql -- AWR Global Diff Report (RAC)
 

Other important scripts under $ORACLE_HOME/rdbms/admin

SQL> @?/rdbms/admin/spawrrac.sql -- Server Performance RAC report
SQL> @?/rdbms/admin/awrsqrpt.sql -- Standard SQL statement Report
SQL> @?/rdbms/admin/awrddrpt.sql -- Period diff on current instance
SQL> @?/rdbms/admin/awrrpti.sql -- Workload Repository Report Instance (RAC)


9) To more analysis why plan has changed one can check support.oracle.com


SQL to check Stale statistics

Many times Oracle SQL plan get change which will cause abnormal behavior, most probable reason could be stale statistics marked by Oracle. Below is useful queries to identify the Objects and gather stats for those Objects. Oracle do have auto jobs which takes care stats gather requirement in defined Windows. One need to calibrate window based on requirement as stats gather as this is resource Intensive process, and should not conflict Business transaction. Oracle include this nightly maintenance Jobs by default scheduled in night time. Stats gather is vast topic, one need carefully touch stats gather on PROD environment.


---Using Anonymous PL/SQL block
SQL> SET SERVEROUTPUT ON
  DECLARE
ObjList dbms_stats.ObjectTab;
BEGIN
dbms_stats.gather_database_stats(objlist=>ObjList, options=>’LIST STALE’);
FOR i in ObjList.FIRST..ObjList.LAST
LOOP
dbms_output.put_line(ObjList(i).ownname || ‘.’ || ObjList(i).ObjName || ‘ ‘ || ObjList(i).ObjType || ‘ ‘ || ObjList(i).partname);
END LOOP;
END;
/

---Oracle 10g Onwards.

col TABLE_NAME for a30
col PARTITION_NAME for a20
col SUBPARTITION_NAME for a20
select OWNER,TABLE_NAME,PARTITION_NAME,SUBPARTITION_NAME,NUM_ROWS,LAST_ANALYZED from dba_TAB_STATISTICS where STALE_STATS='YES';


Saturday 7 September 2013

Perl Oracle DB connectivity and send mail

Perl Script Create report using mentioned Script and send mail to Team.
This need Sendmail to configured on servers along with SMTP details.


#!/usr/bin/perl

#

#use EXCEL;

#use MAIL;

#use CGI;

use DBI;

#use Spreadsheet::WriteExcel::Big;

$attachement=$ARGV[0];

#$subject_line=$ARGV[1];

#$ENV{ORACLE_HOME} = '/u01/app/oracle/product/9.2.0';  ##Linux

$ENV{ORACLE_HOME} = 'C:\app\Dilip\product\11.1.0\db_1';

$stsql = "select * from dba_segments where rownum<=10";

my $ORACLE_SID="orcl";

my $user = 'xxxx';

my $password = 'xxxxx';

#my $data_source = 'dbi:Oracle:ORCL';

my $data_source = 'dbi:Oracle:orcl';

my $dbh =  DBI->connect($data_source, $user, $password)

  || die "Can't connect to $data_source: $DBI::errstr";

$sth = $dbh->prepare("select * from  dba_segments where rownum<=10 and segment_type='TABLE'")

       || die $dbh->errstr;

$sth->execute() || die $sth->errstr;

# Hash Table for column wise return

#while (my $r = $sth->fetchrow_hashref) {

   #print "$r->{OWNER}, $r->{SEGMENT_NAME}\n";

#

#}

open my $fh, '>>', $attachement or die "Could not open file Output.csv: $!";

### Retrieve the returned rows of data All Rows

  while ( @row = $sth->fetchrow_array(  ) ) {

     # print "Row: @row\n";

     #push(@row);

     #print $fh qq{$res->{'Name'}\t$res->{'CompanyName'}\n};

     print $fh qq{@row\n};

  }

$sth->finish;

$dbh->disconnect();

close $fh;

send_mail($attachement,$subject_line,"","xyz\@gmail.com","","");

sub get_data

{

  undef $/;

  open (my $QFH, "< summary.sql") or die "error can't open this file $!";

  my $sth= $dbh_oracle->prepare(<$QFH>) or

      die ("Cannot connect to the database: ".$DBI::errstr."\n");

  $sth->execute;

  close $QFH;

  my $row = $sth->fetchrow_hashref;

  $sth->finish;

  return @$row{'MYTABLE','FLAG'};

}

sub send_mail {

#my ($dbh,$SEP_COL,$querystr_v,$SHEET_NAME)=@_;

#my ($SEP_COL,$querystr)=@_;

my ($attachement,$subject_line,$message,$to,$cc,$bcc)=@_;

my %mime =('GZ',"application/x-gzip", );

                   my $mailprog = '/usr/sbin/sendmail -t';

                   #my $subject_line = "Mail Demo gip file";

                   open(MAIL, "|$mailprog -t ") || &Abort ("Error sending mail Error is $!");

                   print MAIL "To: $to\n";

                   #print MAIL "Bcc:  xxxx@xyz.com, xxxx\@xyz.com\n";

                   #print MAIL "From: mobile\@xyz.com\n";

                   print MAIL "Subject: $subject_line\n";

                   print MAIL "MIME-Version: 1.0\n";

                   #Part 2 starts

                   print MAIL "Content-Type: multipart/mixed; boundary=------------$boundary\n";

                   print MAIL "\n";

                   print MAIL "This is a multi-part message in MIME format.\n";

                   print MAIL "--------------$boundary\n";

                   print MAIL "Content-Type: text/html; charset=us-ascii\n";

                   print MAIL "Content-Transfer-Encoding: 7bit\n\n";

                   print MAIL "$message\r\n";

                   print MAIL "--------------$boundary\n";

                   print MAIL "Content-Type: name='$attachement'\n";

                   print MAIL "Content-Transfer-Encoding: base64\n";

                   print MAIL "Content-Disposition: attachment; filename=$attachement\n\n";

                   my $buf; $/=0;

                   open INPUT, "$attachement"; # should be readable, we checked above [-r]

                   binmode INPUT if ($^O eq 'NT' or $^O eq 'MSWin32');

                   while(read(INPUT, $buf, 60*57))

                   {

                   print MAIL &encode_base64($buf);

                   }

                   close INPUT;

                   print MAIL "\n--------------$boundary--\n";

                   print MAIL "\n";

                   close MAIL;

                   sub Abort {

                   my ($msg) = @_;

                   } # end of sub Abort...

                   sub encode_base64 #($)

                   {

                                   my ($res, $eol, $padding) = ("", "\n", undef);

                                   while (($_[0] =~ /(.{1,45})/gs))

                                   {

                                   $res .= substr(pack('u', $1), 1);

                                   chop $res;

                                   }

                                   $res =~ tr#` -_#AA-Za-z0-9+/#;                  # ` help emacs

                                   $padding = (3 - length($_[0]) % 3) % 3;         # fix padding at the end

                                   $res =~ s#.{$padding}$#'=' x $padding#e if $padding; # pad eoedv data with =s

                                   $res =~ s#(.{1,76})#$1$eol#g if (length $eol); # lines of at least 76 characters

                                   return $res;

                   }

#exit;

}

exit;

###For DBI refer http://oreilly.com/catalog/perldbi/chapter/ch04.html

Saturday 31 August 2013

Point in Time Recovery cross restore RMAN

Steps:


1) restore Controlfile

rman target / catalog /@CATALOG
set dbid=xxxxxxxxxxxxxxxxxx
set 

 run
{
allocate channel dev1 type tape PARMS='...';
set until time "to_date('2011-12-30:00:00:00', 'yyyy-mm-dd:hh24:mi:ss')";
restore controlfile from autobackup;
}

SQL> alter database mount;

Recover database
run
{
allocate channel dev1 type tape PARMS='...';
set newname for datafile 1 to '';
.
.
.
set newname for datafile 99 to '';

set until time "to_date('2011-12-30:00:00:00', 'yyyy-mm-dd:hh24:mi:ss')";

restore tablespace system, sysaux, undotbs, users;
recover database;
}

SQL> alter database open resetlogs;



Can explore more Crash recovery option at http://www.akadia.com/services/ora_crash_recovery.html

Friday 9 August 2013

Recover Tablespace Only RMAN


How to recover from a DROP or TRUNCATE table by using RMAN.
-----------------------------------------------------------
There are three options available:
1. Restore and recover the primary database to a point in time before the drop.
This is an extreme measure for one table as the entire database goes back in
time.
2. Restore and recover the tablespace to a point in time before the drop.
This is a better option, but again, it takes the entire tablespace back in
time.
3. Restore and recover a subset of the database as a DUMMY database to export
the table data and import it into the primary database. This is the best
option
as only the dropped table goes back in time to before the drop.
For the first, see information on performing incomplete recovery on a database.
The second and third options are virtually the same, it depends if the entire
tablespace data should be taken back in time, or just recovering the one table.
The tablespace point in time recovery (TSPITR) may be useful if there are
dependencies between the dropped/truncated table and other tables in the
database. For the second option, see RMAN documentation on TSPITR and/or
Note 180436.1
RMAN Tablespace Point in Time Recovery Example. Both procedures
for the second and third options are very much the same. The differences are
that the TABLE PITR has to be exported/imported manually while the TABLESPACE
PITR is fully automated by RMAN.
This article will deal with the third option.
General overview of procedure to recover from a DROP or TRUNCATE table by using
RMAN.
--------------------------------------------------------------------------------
----
To recover from a dropped or truncated table, a dummy database (copy of primary)
will be restored and recovered to point in time so the table can be exported.
Once the table export is complete, the table can be imported into the primary
database. This dummy database can be a subset of the primary database.
However,
the 'dummy' database must include the SYSTEM, UNDO (or ROLLBACK), and the
tablespace(s) where the dropped/truncated table resides.
The simpliest method to create this 'dummy' database is to use the RMAN
duplicate command. See:
Note 228257.1
RMAN Duplicate Database in Oracle9i
Note 73912.1
RMAN Creating a Duplicate Database -- Oracle8i
The difference between the two versions is that Oracle9i duplicate command
allows for a 'SKIP TABLESPACE' option. Thus Oracle9i allows for a duplication
of a subset of the database.
In Oracle8i, you cannot 'skip' tablespaces when using duplicate, so you must
duplicate the entire database. If this is not a desired option, or you must
restore the original database and thus cannot use the rman DUPLICATE.

NOTE: The remainder of this information is for users who cannot use the DUPLICATE command in Oracle8i.
I.e., you want to restore only a subset of the Oracle8i database.Requirements :
--------------
a) RMAN backup of the primary database should be available to the
host where it has to be restored on.
b) Use of an RMAN-catalog database
c) Auxiliary instance created and started in NOMOUNT
(See
Note 180436.1
step I. Create the auxiliary initSID.ora.)
Create this from the init.ora of the primary database and:
!!!!! IMPORTANT !!!!!!!!
If using the same host as the primary, be VERY careful as you do not want to
restore on top of existing files being used by the primary (production
database).
Doing so can corrupt and crash the production database!!!!!!
- be sure all paths for this AUX instance are different than primary.
- be sure CONTROL_FILES parameter has different location but more
importantly DIFFERENT NAME.
- add LOCK_NAME_SPACE to any value other than the primary database name.
- change/add SERVICE_NAME=AUX1.
- use the SAME DB_NAME as for the production database
- BE SURE you include the 'alter database rename file' command at the end
of the script. This changes the location and/or name of the online
redo log files.
d) Set ORACLE_HOME and ORACLE_SID set to the auxiliary instance
e.q set ORACLE_SID=AUX1
set ORACLE_HOME=<....>
!!!!! IMPORTANT !!!!!!!!
1: Restore and recover the tablespace
-------------------------------------
The restore and recovery is done by RMAN. Create a file
of the script below (e.q table_pitr.cmd) and execute the following
command :
$ rman cmdfile=table_pitr.cmd
NOTE: ORACLE_HOME and ORACLE_SID set to the auxiliary instance
NOTE: The Auxiliary instance IS the target to rman at this point.
The RMAN-script :
-----------------
connect catalog rman/rman@catalog
connect target /
run
{
allocate channel t1 type sbt_tape
parms='SBT_LIBRARY=/home/usupport/liblsm.so';
set until time "to_date( '09-10-2005 06:00', 'DD-MM-RRRR HH24:MI')";
restore controlfile;
sql "alter database mount clone database";
set newname for datafile 1 to '/fs01/oradata/tspitr/system01.dbf';
set newname for datafile 2 to '/fs01/oradata/tspitr/undotbs01.dbf';
set newname for datafile 4 to '/fs01/oradata/tspitr/tools01.dbf';
restore tablespace system, undotbs1, tools;
switch datafile all;
sql "alter database datafile 1,2,4 online";
recover database skip forever tablespace TEMP,INDX,USERS,OLTS_ATTRSTORE,
OLTS_CT_DN,OLTS_CT_CN, OLTS_CT_OBJCL,OLTS_CT_STORE,OLTS_DEFAULT,
OLTS_TEMP,OLTS_IND_ATTRSTORE,
OLTS_IND_CT_DN,OLTS_IND_CT_CN,OLTS_IND_CT_OBJCL,OLTS_IND_CT_STORE,
P1TS_ATTRSTORE,P1TS_IND_STORE;
sql "alter database rename file ''/fs01/oradata/primary/REDO01.LOG'' to
''/fs01/oradata/tspitr/REDO01.LOG''";
sql "alter database rename file ''/fs01/oradata/primary/REDO02.LOG'' to
''/fs01/oradata/tspitr/REDO02.LOG''";
sql "alter database rename file ''/fs01/oradata/primary/REDO03.LOG'' to
''/fs01/oradata/tspitr/REDO03.LOG''";
/* NOTE: Syntax within rman is two single quotes around each name, this may be
operating system specific. */
release channel t1;
}
Explanation :
-------------
- Tape channel allocated, but could also be a disk channel, depending
on where the backups are.
- SET UNTIL TIME
User specified time, just before the DROP/TRUNACTE table
- MOUNT CLONE DATABASE.
This forces all datafiles to be put OFFLINE. Just for safety reasons.
- SET NEWNAME
New path for the datafile to be restored. Keep in mind that this is
done on the auxiliary instance and should NOT interfere/overwrite the
prodution database.
- ALTER DATABASE RENAME FILE
This is required to change the location of the online log files. When the
'resetlogs' is issued, Oracle will create online logs based on
specification
in the controlfile. This command changes the location and/or name. If
this is being performed on the SAME server, not issuing a rename will
cause Oracle to reset the production online log files. This will corrupt
and crash the production database!!!!!!
- RESTORE TABLESPACE ...;
Restore the tablespaces which need to be recoverd.
This includes always the SYSTEM, rollback/undo tablespace and
the tablespace(s)where the dropped/truncated table resides.
The SYSTEM tablespace is always included as it containts most / all of
the objects owned by SYS and SYSTEM. Some other tablespaces might be
included as well when they contain objects owned by SYS and SYSTEM.
SQL> select distinct tablespace_name
from dba_segments where owner in ('SYS', 'SYSTEM');
- SWITCH DATAFILE ALL;
Make the changes in the pathname (set by SET NEWNAME) active in the
controlfile.
- ALTER DATABASE DATAFILE ... ONLINE
Online the datafiles which are restored and have to be recovered.
- RECOVER DATABASE SKIP FOREVER TABLESPACE ......;
You need to specify the complete list of tablespaces which will not be
recovered. Else the recovery fails, that it cannot dentify/file the
tablespace datafile. The SKIP FOREVER clause causes RMAN to take the
datafiles offline using the DROP option. Only use skip forever when the
specified tablespaces will be dropped after opening the database. I.e.,
all tablespaces except the one which contains your data.
The recovery of RMAN, checks the datafileheaders, to get a starting point
for the recovery.
2: Open auxiliary database with RESETLOGS
-----------------------------------------
$ sqlplus /
SQL> alter database open resetlogs;
This step should ALWAYS be executed outside RMAN via SQL*Plus. If the open
is executed in RMAN it may effect the primary database's entry in the RMAN
catalog. Backups will fail with messages like:
RMAN-20011 "target database incarnation is not current in recovery catalog"
3: Export the table
-------------------
The database is recovered and open, so it can be used by export.
Example:
$ exp userid=system/<password> file=table.dmp
tables=(<owner>.<tablename>, ...) rows=Y
4: Import the export-dump
-------------------------
Import the data of the dropped table back into the primary/production
database.
Example:
$ imp userid=system/<password> file=table.dmp ignore=Y
5: Remove this AUX/DUMMY database
---------------------------------
Shutdown and remove all files associated with this database. It has
satisfied
your purpose. I.e., to give you an export of this table.


RELATED DOCUMENTS
-----------------
Note 62385.1
: Oracle7 Recovery Scenarios and Equivalent RMAN Techniques
Note 180436.1
: RMAN Tablespace Point in Time Recovery Windows Example
Note 109979.1
: RMAN Tablespace Point In Time Recovery (TSPITR) Procedure on
Unix.
Note 228257.1
: RMAN Duplicate Database in Oracle9i
Note 73912.1
: RMAN Creating a Duplicate Database -- Oracle8i
.

Sunday 7 July 2013

Golden Gate Commands

select * from table(logmnr$always_suplog_columns('SCHEMAUSER','T'));

GRANT CREATE SESSION,ALTER SESSION to gguser identified by oracle;
GRANT CONNECT,SELECT ANY DICTIONARY,FLASHBACK ANY TABLE,SELECT ANY TABLE to gguser;
GRANT SELECT on dba_clusters to gguser;
GRANT EXECUTE on DBMS_FLASHBACK to gguser;
GRANT SELECT ANY TRANSACTION to gguser;
GRANT EXECUTE on utl_file TO gguser;
EXEC dbms_goldengate_auth.grant_admin_privilege('gguser');

--Command Used to check status.
info all
SEND replicat rha02, STATUS
STATS replicat rha02, totalsonly *, reportrate min/hr/sec
SEND extract xha01, SHOWTRANS
SEND extract xha01, STATUS
STATS extract xha01, totalsonly *, reportrate sec
[oracle@dbserver1 ggs]$ ./logdump
Logdump 2 >open dirdat/OUT/ex000004
Logdump 3 >fileheader detail
Logdump 4 >ghdr on
Logdump 6 >detail on
Logdump 7 >usertoken detail
Logdump 8 >reclen 128

Reclen set to 128
Logdump 9 >pos 0

Reading forward from RBA 0

Logdump 19 >n
Logdump 34 >count
Logdump 52 >filter include iotype delete

Logdump 53 >n
Logdump  >detail on
Logdump  >ggstoken detail
Logdump  >filter csn = 9889898
Logdump  >n

shell ls -l


ggsci> list tables cust*

ggsci> add extract s_extr, tranlog, begin now
ggsci> add extract finext, tranlog, begin now, threads 4
ggsci> add extract fin, tranlog, begin now, passive
ggsci> add extract ext_ms, extseqno 111, begin now
ggsci> add extract hr_ext, extrba 77878, begin 2013-03-03 12:00:00

ggsci> add trandata hr.*
ggsci> add trandata emp.employees
ggsci> add trandata fin.acct, cols (name, address)
ggsci> add trandata fin.acct, nokey, cols (name, pid)
ggsci> add trandata fin.acct, lobsalwaysnoindex

ggsci> add checkpointtable
ggsci> add checkpointtable gg_owner.checkpoint

ggsci> start replicat rep
ggsci> start replicat fin, atcsn 5435435       -- commit sequence number (CSN)
ggsci> start replicat fin, aftercsn 0X0000071:0002334D:0

-- To start multiple Extract and Replicat groups as a unit
ggsci> start er *rep*
ggsci> stop er *ext*

ggsci> status extract extr_hr
ggsci> status extract ext*, tasks
ggsci> status extract *ext*, allprocesses

ggsci> alter extract fin, begin 2013-02-16
ggsci> alter extract fin, etrollover
ggsci> alter extract fin, extseqno 26, extrba 338
ggsci> alter extract accounts, thread 4, begin 2012-03-09
ggsci> alter extract sales, lsn 12342:1234:1


ggsci> send extract exthr status
ggsci> send extract extr, getlag
ggsci> send extract group_name tltrace file file_name ddlinclude
ggsci> send extract fin, rollover
ggsci> send extract fin  stop
ggsci> send extract fin, vammessage control:suspend
ggsci> send extract fin, tranlogoptions transcleanupfrequency 15
ggsci> send extract fin, showtrans count 10
ggsci> send extract fin, skiptrans 5.17.27634 thread 2

ggsci> VERSIONS   -- To display operating system and database version information

ggsci> FC [n | -n | string]   -- To display edit a previously issued GGSCI command and then execute it again
ggsci> fc
ggsci> fc 9
ggsci> fc -3
ggsci> fc sta