Professional Documents
Culture Documents
Author JP Vijaykumar
COMPLEX PROBLEM 1
DATE
10-08-2013
---------------------------------------------------------------------------------REQUIREMENTS:
01) For a set of range partitioned tables - partitioned by month, having
a specific partition_name(in the range), add 5 partitions by splitting the
specific partition.
02) Insert data from the table's latest partition into the newly added partiti
on.
All the column values are to be added, excepting the 1st column,range
partition_key on date value which should be below the current partition's
high value.
03) Write a pl/sql script to automate the above task and populate these range
partitioned tables.
set serverout on size 1000000 timing on linesize 120
declare
j number;
v_str date;
v_qry varchar2(4000);
v_pr1 varchar2(30);
v_pr2 varchar2(30);
begin
execute immediate 'alter session set nls_date_format='''||'yyyymmdd'||''' ';
for c1 in (select table_name,partition_name from user_tab_partitions
where partition_name='P_20130426' )loop
v_qry:='';
v_str:='';
execute immediate 'select '''||'from '||table_name||''' partition ('''||partitio
n_name||
'''||') a '||''' from user_tab_partitions where table_name='''||c1.table
_name||''' and partition_position in (
select partition_count from user_part_tables where table_name='''||c1.table_
name||''')' into v_qry;
for c0 in (select column_name from user_tab_columns where table_name=c1.table_na
me
and column_id > 1 order by column_id desc) loop
if (substr(v_qry,1,4) = 'from') then
v_qry:=c0.column_name||' '||v_qry;
else
v_qry:=c0.column_name||', '||v_qry;
end if;
end loop;
--dbms_output.put_line(v_qry);
v_str:=to_date(substr(c1.partition_name,3,length(c1.partition_name)),'yyyymmdd')
;
for i in 0..4 loop
j:=i + 1;
v_pr1:=to_date(to_date(v_str,'yyyymmdd') - i,'yyyymmdd');
v_pr2:=to_date(to_date(v_str,'yyyymmdd') - j,'yyyymmdd');
begin
execute immediate 'alter table '||c1.table_name||' drop partition P_'||v_pr2;
exception
when others then
dbms_output.put_line(c1.table_name||' p_'||v_pr2||' '||sqlerrm);
end;
--dbms_output.put_line('insert /*+ append*/ into '||c1.table_name||' select /*+
parallel(a 32)*/ to_date('||v_pr2||','||chr(39)||
--'yyyymmdd'||chr(39)||'),'||v_qry);
execute immediate 'alter table '||c1.table_name||' split partition p_'||v_pr1||'
at (to_date('||chr(39)||
to_char(to_date(v_pr1,'yyyymmdd'),'syyyy-mm-dd')||chr(39)||','||chr(39)||'syyyymm-dd'||chr(39)||')) '||
' into (partition p_'||v_pr2||',partition p_'||v_pr1||')';
execute immediate 'alter table '||c1.table_name||' truncate partition P_'||v_pr2
;
--dbms_output.put_line('insert /*+ append*/ into '||c1.table_name||' select to_c
har('||v_pr2||','||chr(39)||
-- 'yyyymmdd'||chr(39)||'),'||v_qry);
execute immediate 'insert /*+ append*/ into '||c1.table_name||' select /*+ paral
lel(a 32)*/ to_date('||v_pr2||','||chr(39)||
'yyyymmdd'||chr(39)||'),'||v_qry;
commit;
--dbms_output.put_line(v_str||' '||v_pr1||' '||v_pr2);
end loop;
end loop;
end;
/
REFERENCES:
--------------------------------------------------------------------------------------http://www.rocket99.com/techref/oracle8614.html
http://www.dbasupport.com/oracle/ora10g/multi_table_loop.shtml
http://www.morganslibrary.org/hci/hci015.html
http://www.toadworld.com/platforms/oracle/b/weblog/archive/2013/08/15/procedureto-loop-on-cursor-variables-inside-a-loop.aspx
-------------------------------------------------------------------------------COMPLEX PROBLEM 2
DATE
11-08-2013
-------------------------------------------------------------------------------REQUIREMENTS:
--In a schema there are two sets of identical tables. One set of tables end with
"_T"
and another set of tables end with "_P". Find the missing tables with "_P" ext
ension
and missing indexes on these tables with "_P" extension.
select a.table_name,a.index_name,b.table_name,b.index_name
from
(select table_name , substr(table_name,1,length(table_name) -2) a_table,
index_name from dba_indexes where owner='SCOTT'
and substr(table_name,length(table_name) -1,length(table_name)) = '_T' ) a
,
(select t.table_name ,substr(t.table_name,1,length(t.table_name) -2) b_table,
i.index_name from dba_tables t,dba_indexes i
where t.owner
='SCOTT'
and t.owner
= i.owner
and substr(t.table_name,length(t.table_name) -1,length(t.table_name)) = '_P'
and t.table_name = i.table_name(+)) b
where a.a_table = b.b_table(+)
and replace(a.index_name,'_T_','_P_') = b.index_name(+) order by 1,2
-------------------------------------------------------------------------------COMPLEX PROBLEM 3
DATE
02-08-2014
---------------------------------------------------------------------------------To disable/verify default resource_manager_plan in a 11g databases
Run the following pl/sql script connecting to the db as sysdba.
set serverout on size 1000000 linesize 120 timing on
declare
v_st1 varchar2(200);
v_st2 varchar2(200);
begin
execute immediate '
select name||'''||' - '||'''||
(select name||'''||': '||'''||value from v$parameter where name like '''||'resou
rce_manager_plan'||''')||
'''||' - '||'''||
(select name||'''||':'||'''||cpu_managed from v$rsrc_plan where is_top_plan = ''
'||'TRUE'||''')
from v$database' into v_st1;
dbms_output.put_line(v_st1);
execute immediate 'alter system set resource_manager_plan='''||''||''' scope=bot
h';
for c1 in ( select window_name from dba_scheduler_windows order by 1) loop
begin
dbms_scheduler.get_attribute(name=>c1.window_name,attribute=>'RESOURCE_PLAN',val
ue=>v_st1,value2=>v_st2);
dbms_output.put_line(c1.window_name||' '||v_st1||' '||v_st2);
dbms_scheduler.set_attribute(c1.window_name,'RESOURCE_PLAN','');
dbms_scheduler.get_attribute(name=>c1.window_name,attribute=>'RESOURCE_PLAN',val
ue=>v_st1,value2=>v_st2);
dbms_output.put_line(c1.window_name||' '||v_st1||' '||v_st2);
exception
when others then
dbms_output.put_line(sqlerrm);
end;
end loop;
execute immediate '
select name||'''||' - '||'''||
(select name||'''||': '||'''||value from v$parameter where name like '''||'resou
rce_manager_plan'||''')||
'''||' - '||'''||
(select name||'''||':'||'''||cpu_managed from v$rsrc_plan where is_top_plan = ''
'||'TRUE'||''')
from v$database' into v_st1;
dbms_output.put_line(v_st1);
end;
/
-------------------------------------------------------------------------------COMPLEX PROBLEM 4
DATE
03-08-2014
-------------------------------------------------------------------------------The following pl/sql script to create database link, dynamically was failing,
when double quotes were used in the dynamic sql. I replaced the double quotes wi
th
chr(34) to make the script work.
connect as the schema owner of the db_link to the database.
connect veeksha/saketh
set serverout on size 1000000
declare
begin
for c1 in (select name from v$database)
loop
if (c1.name = 'PROD01') then
begin
execute immediate 'create database link prod02 connect to veeksha identified by
'
||chr(34)||'tiger'||chr(34)||' using '''||'PROD02'||''' ';
exception
when others then
dbms_output.put_line(c1.name||' '||sqlerrm);
end;
else
begin
execute immediate 'drop database link prod01';
exception
when others then
dbms_output.put_line(c1.name||' '||sqlerrm);
end;
begin
execute immediate 'create database link prod01 connect to saketh identified by '
||chr(34)||'tiger'||chr(34)||' using '''||'PROD01'||''' ';
exception
when others then
dbms_output.put_line(c1.name||' '||sqlerrm);
end;
end if;
end loop;
end;
/
-------------------------------------------------------------------------------COMPLEX PROBLEM 5
DATE
05-11-2014
---------------------------------------------------------------------------------DATA INSIDE RUN_DATE TABLE IS ORGANIZED IN THE FOLLOWING MANNER:
create table run_date(run_date date,value number);
alter table run_date rename column run_date to run_time;
alter session set nls_date_format='yyyy-mm-dd hh24:mi:ss';
insert into run_date values(to_date('2014-05-13 22:10:00','yyyy-mm-dd hh24:mi:ss
'),22);
insert into
'),35);
insert into
'),38);
insert into
'),41);
insert into
'),50);
insert into
'),53);
insert into
'),69);
insert into
'),81);
insert into
'),99);
commit;
select * from run_date;
RUN_TIME
VALUE
------------------- ---------2014-05-13 22:10:00
22
2014-05-14 10:02:30
35
2014-05-14 12:22:30
38
2014-05-15 21:09:11
41
2014-05-16 02:11:08
50
2014-05-16 02:11:22
53
2014-05-17 17:43:57
69
2014-05-18 23:18:11
81
2014-05-19 14:49:16
99
9 rows selected.
--FROM THE ABOVE DATA, I WANT THE DELTA VALUES FOR EACH DAY.
alter session set nls_date_format='yyyy-mm-dd';
with t as (select to_char(run_time,'yyyy-mm-dd') run_time,max(value) value
from run_date group by to_char(run_time,'yyyy-mm-dd') order by 1)
select b.run_time,b.value - a.value diff
from t a, t b
where to_date(a.run_time) +1 = b.run_time order by 1;
RUN_TIME
DIFF
------------------------------ ---------2014-05-14
16
2014-05-15
3
2014-05-16
12
2014-05-17
16
2014-05-18
12
2014-05-19
18
6 rows selected.
--FROM THE ABOVE DATA, I WANT THE DELTA VALUES FOR EACH CONSECUTIVE RUN_TIME,
begin
execute immediate 'alter session set nls_date_format='''||'yyyymmdd'||''' ';
for c1 in (select table_name from user_tables
where partitioned='YES' --and table_name='NORTH_P'
and table_name in (select table_name from user_part_tables where pa
rtitioning_type='RANGE')
and table_name in (select k.name from user_part_key_columns k, user
_tab_columns c
where k.name = c.table_name
and k.column_name = c.column_name and c.data_type='DATE') orde
r by 1 ) loop
/******************************************************************
'P_20130505'
,'P_20130504'
,'P_20130503'
,'P_20130502'
,'P_20130405'
,'P_20130404'
,'P_20130403'
,'P_20130402'
,'P_20130307'
,'P_20130306'
******************************************************************/
for c2 in (
select min(to_date(substr(partition_name,3,length(partition_name)),'yyyymmdd'))
prt
from user_tab_partitions where table_name=c1.table_name
and to_date(substr(partition_name,3,length(partition_name)),'yyyymmdd')
> to_date(v_str,'yyyymmdd')) loop
begin
v_pt1:='P_'||c2.prt;
v_pt2:='P_'||v_str;
v_dt1:=to_date(substr(v_pt1,3,length(v_pt1)),'yyyymmdd')+1;
v_dt2:=to_date(v_str,'yyyymmdd')+1;
begin
--execute immediate 'alter table '||c1.table_name||' drop partition '||v_pt2;
dbms_output.put_line('alter table '||c1.table_name||' drop partition '||v_pt2);
exception
when others then
dbms_output.put_line(c1.table_name||' 1 '||v_pt2||' 1 '||sqlerrm);
end;
--execute immediate 'alter table '||c1.table_name||' split partition '||v_pt1||'
at (to_date('||chr(39)||
--to_char(to_date(v_dt2,'yyyymmdd'),'syyyy-mm-dd')||chr(39)||','||chr(39)||'syyy
y-mm-dd'||chr(39)||')) '||
--' into (partition '||v_pt2||',partition '||v_pt1||')';
dbms_output.put_line('alter table '||c1.table_name||' split partition '||v_pt1||
' at (to_date('||chr(39)||
to_char(to_date(v_dt2,'yyyymmdd'),'syyyy-mm-dd')||chr(39)||','||chr(39)||'syyyymm-dd'||chr(39)||')) '||
' into (partition '||v_pt2||',partition '||v_pt1||')');
--execute immediate 'alter table '||c1.table_name||' truncate partition '||v_pt2
;
exception
when others then
dbms_output.put_line(c1.table_name||' 2 '||v_pt2||' 2 '||sqlerrm);
end;
end loop;
end loop;
end;
-------------------------------------------------------------------------------COMPLEX PROBLEM 8
DATE
09-14-2014
-------------------------------------------------------------------------------set serverout on size 1000000 timing on
declare
/*************************************
THIS SCRIPT CREATES A VIEW, IF A DB LINK IS EXISTING IN THE DB
CONNECTING TO A SPECIFIC DATABASE USING A SPECIFIC USER.
IF NOT A NULL VALUE IS RETURNED AND NO ACTION TAKEN.
HERE THE NVL FUNCTION IS USED OVER A SQL QUERY ITSELF, NOT A COLUMN VALUE.
*************************************/
begin
for c1 in (
select nvl((select 'create or replace view emp_grade as select * from emp_grade
@'||db_link
from user_db_links where username='SCOTT' and host='mydb.world'),
'NULL') cmd from dual) loop
begin
dbms_output.put_line(c1.cmd);
if (c1.cmd <> 'NULL') then
execute immediate c1.cmd;
end if;
exception
when others then
dbms_output.put_line(sqlerrm);
end;
end loop;
for c2 in (
select nvl((select 'grant select on '||view_name||' to olf_user' from user_views
where view_name ='EMP_GRADE'),'NULL') cmd from dual) loop
begin
dbms_output.put_line(c2.cmd);
if (c2.cmd <> 'NULL') then
execute immediate c2.cmd;
end if;
exception
when others then
dbms_output.put_line(sqlerrm);
end;
end loop;
end;
/
-------------------------------------------------------------------------------COMPLEX PROBLEM 9
DATE
09-14-2014
---------------------------------------------------------------------------------AVG VALUES/MINUTE SINCE INSTANCE STARTUP
order by e.wait_class,e.snap_id,e.inst_id)
select * from t2
pivot(sum(waits) as waits,sum(timeouts) as timeouts,sum(time_wt_secs) as time_wt
_secs
for inst_id in (1,2,3))
--EDIT THE NUMBER OF INST_IDS HE
RE
/
--WAIT_CLASS ANALYSIS OVER A RANGE OF SNAP_ID
set serverout on size 1000000 timing on
declare
v_cnt number:=20;
begin
for c1 in (select distinct wait_class from v$event_name order by 1) loop
dbms_output.put_line('SN_ID WAIT_CLASS
WTS1 WTS2 WTS3 TMOUTS1 TMOUTS2 TMO
UTS3 TM_WTD_SEC1 TM_WTD_SEC2 TM_WTD_SEC3' );
for c2 in (
with t1 as (
select snap_id,instance_number inst_id,wait_class,sum(total_waits) sum_waits,sum
(total_timeouts) sum_timeouts,
round(sum(time_waited_micro)/1000000,2) time_waited_secs from dba_hist_system
_event
where snap_id >=(select max(snap_id) -v_cnt from dba_hist_system_event) --EDI
T THE REQUIRED NUM OF SNAP_IDS
group by snap_id,instance_number,wait_class),
t2 as (
select e.snap_id,e.inst_id,e.wait_class,e.sum_waits - b.sum_waits diff_waits,
e.sum_timeouts - b.sum_timeouts diff_timeouts,e.time_waited_secs - b.time_waited
_secs diff_time_waited_secs
from t1 e, t1 b
where e.snap_id
= b.snap_id +1
and e.inst_id
= b.inst_id
and e.wait_class
= c1.wait_class
and e.wait_class
= b.wait_class
order by e.wait_class,e.snap_id,e.inst_id)
select snap_id,wait_class,
sum(decode(inst_id,1,diff_waits,0)) wts1,
sum(decode(inst_id,2,diff_waits,0)) wts2,
sum(decode(inst_id,3,diff_waits,0)) wts3,
sum(decode(inst_id,1,diff_timeouts,0)) tmouts1,
sum(decode(inst_id,2,diff_timeouts,0)) tmouts2,
sum(decode(inst_id,3,diff_timeouts,0)) tmouts3,
sum(decode(inst_id,1,diff_time_waited_secs,0)) tm_wtd_sec1,
sum(decode(inst_id,2,diff_time_waited_secs,0)) tm_wtd_sec2,
sum(decode(inst_id,3,diff_time_waited_secs,0)) tm_wtd_sec3
from t2 group by snap_id,wait_class order by snap_id,wait_class
) loop
dbms_output.put_line(lpad(c2.snap_id,5)||' '||lpad(c2.wait_class,15)||' '||lpad(
c2.wts1,6)||' '||lpad(c2.wts2,6)||' '||lpad(c2.wts3,6)
||' '||lpad(c2.tmouts1,6)||' '||lpad(c2.tmouts2,6)||' '||lpad(c2.tmouts3,6)||' '
||lpad(c2.tm_wtd_sec1,9)||' '||lpad(c2.tm_wtd_sec2,9)||' '||lpad(c2.tm_wtd_sec3,
9));
end loop;
end loop;
end;
sum(decode(inst_id,3,diff_time_waited_secs,0)) tm_wtd_sec3
from t2 group by snap_id,event_name order by event_name,snap_id
) loop
dbms_output.put_line(lpad(c2.snap_id,5)||' '||lpad(c2.event_name,25)||' '||lpad(
c2.wts1,6)||' '||lpad(c2.wts2,6)||' '||lpad(c2.wts3,6)
||' '||lpad(c2.tmouts1,6)||' '||lpad(c2.tmouts2,6)||' '||lpad(c2.tmouts3,6)||' '
||lpad(c2.tm_wtd_sec1,9)||' '||lpad(c2.tm_wtd_sec2,9)||' '||lpad(c2.tm_wtd_sec3,
9));
end loop;
end loop;
end;