Search This Blog

Tuesday, March 29, 2011

Generate Random Data, Using bind variables to improve performance

create table salary_rates
(
dept char(1),
rate number(2,1)
);

alter table salary_rates cache;

create table emp
(
id number,
dept char,
salary number
);

create unique index emp_id_idx on emp(id);

create sequence emp_id;


CREATE OR REPLACE TRIGGER emp_trg
BEFORE INSERT ON emp
FOR EACH ROW
WHEN (new.id IS NULL)
BEGIN
SELECT emp_id.NEXTVAL INTO :new.id FROM dual;
END;
/

--------Salary rates Loading data------------------------

insert into salary_rates values( 'A',0.1);
insert into salary_rates values( 'B',0.2);
insert into salary_rates values( 'C',0.3);
insert into salary_rates values( 'D',0.4);
commit;
--------Salary rates Loading data ------------------------

------------------------- Generate random depts -------------------------
CREATE OR REPLACE FUNCTION GET_RANDOM_DPT RETURN CHAR
IS
X NUMBER;
RTV CHAR;
BEGIN
X:=ROUND(dbms_random.value(1, 5));

IF (X=1) THEN
RTV := 'A';
ELSIF (X=2) THEN
RTV := 'B';
ELSIF (X=3) THEN
RTV := 'C';
ELSE
RTV := 'D';
END IF;

RETURN RTV;
END;
/
------------------------- Generate random depts -------------------------

create or replace procedure gen_data ( emp_population number)
is
begin
FOR i IN 1 .. emp_population loop
insert into emp(dept,salary) values(GET_RANDOM_DPT,ROUND(dbms_random.value(1,1000000)));
commit;
END LOOP;
end;
/
-----------------------------------------------------------------------------------------------------------

/* Scenario : Update employee salary , ONLY those employees current salary is less than 3000 according the employee's department increase rates.
------------------------------------------------------------------------------------------------------
update 1
Elapsed: 00:00:01.70


CREATE OR REPLACE PROCEDURE UPDATE1
AS
rate_ number;
begin
---open c_emp;
for i IN ( select id,dept,salary from emp) loop
if (i.salary < 3000 ) then
select rate into rate_ from salary_rates where dept=i.dept;
update emp set salary=(salary * rate_/100)+salary where id=i.id;
commit;
else
null;
end if;
end loop;
END UPDATE1;



CREATE OR REPLACE PROCEDURE UPDATE2
AS
rate_ number;
type rc is ref cursor;
q rc;
begin
for i IN ( select id,dept,salary from emp) loop
if (i.salary < 3007 ) then
open q for ' select rate from salary_rates where dept= :y ' using i.dept;
fetch q into rate_;
execute immediate ( ' update emp set salary=(salary * :r / 100) + salary where id= :x') using rate_,i.id;
commit;
else
null;
end if;
end loop;
END UPDATE2;
Elapsed: 00:00:01.32

How to move a table in new tablespace with NOLOGGING option

/*
Login as sys or system
create a tablespace with NOLOGGING option
*/

create tablespace sh_rate
datafile 'E:\ORADATA\PROD\DATAFILE\RATES.DBF' size 128M autoextend on maxsize 2048M
segment space management auto
NOLOGGING;
/*
Grant user (The owner of the table[s], indexes etc) to access the new tablespace
*/
alter user parameters quota unlimited on sh_rate;

/*
Move tables in the new tablespace
*/

alter table parameters.sh_rates_temp move tablespace sh_rate;
alter table parameters.sh_rate move tablespace sh_rate;

/*
create,Recreate, or rebuild indexes

Rebuild index : alter index xyz rebuild tablespace sh_rate;
*/
create index parameters.sh_rates_temp_idx1 on parameters.sh_rates_temp(code) tablespace sh_rate;
create index parameters.sh_rate_idx1 on parameters.sh_rate(code) tablespace sh_rate;

Thursday, March 24, 2011

Find out DBID from RMAN catalog

In the case that you need to restore control file or
spfile using RMAN , you need to set the DBID first.
In order to determine the DBID login as rman in rman catalog
and issue the following sql:

sqlplus rman/***@rman

sql>select dbid from rc_database where name='DBNAME';

Another way to find out the DBID is from your RMAN logs.

Example:

RMAN> set dbid 1150787593

executing command: SET DBID
database name is "ORCL" and DBID is 1150787593

RMAN> restore spfile;

Saturday, March 19, 2011

Oracle RAC 10g Manage Server-Side Callouts

In Oracle 10g RAC, every time a node , database , instance , service and etc. goes up or down, that event can be catched and used to make user defined callouts. So every time a state change occurs, a FAN event is posted to ONS immediately. When a node receives an event through ONS, it will asynchronously execute all executables in the server side callouts directory.

Callouts directory is under $CRS_HOME/racg/usrco

The example below creates a file in /home/oracle directory when the ocfs database is down. You can modify it with further functionality i.e. send mail to administrator people etc.

create the file parseCallout.sh under the $CRS_HOME/racg/usrco
chmod +x parseCallout.sh

#!/bin/sh
NOTIFY_EVENTTYPE=$1
echo $NOTIFY_EVENTTYPE
for ARCS in $*; do
PROPERTY=`echo $ARCS | awk -F"=" '{print $1}'`
echo $PROPERTY
VALUE=`echo $ARCS | awk -F"=" '{print $2}'`
echo $VALUE
case $PROPERTY in
VERSION|version)
NOTIFY_VERSION=$VALUE ;;
SERVICE|service)
NOTIFY_SERVICE=$VALUE ;;
DATABASE|database)
NOTIFY_DATABASE=$VALUE ;;
INSTANCE|instance)
NOTIFY_INSTANCE=$VALUE ;;
HOST|host)
NOTIFY_HOST=$VALUE ;;
STATUS|status)
NOTIFY_STATUS=$VALUE ;;
REASON|reason)
NOTIFY_REASON=$VALUE ;;
CARD|card)
NOTIFY_CARDINALITY=$VALUE ;;
TIMESTAMP|timestamp)
NOTIFY_LOGDATE=$VALUE ;;
??:??:??)
NOTIFY_LOGTIME=$PROPERTY ;;
esac
done
if ([ "$NOTIFY_STATUS" == "down" ] && [ "$NOTIFY_DATABASE" == "ocfs" ]) then
echo "$NOTIFY_DATABASE is down, please investigate ... " > /home/oracle/"$NOTIFY_LOGDATE".callout
fi

Thursday, March 10, 2011

Rename file using the current date and time

( Unix Version )

mv /file/path/filename.txt /file/path/filename_$(date +"%m%d%y%H%M%S").txt

Rename file using the current date and time ( Windows Version )


ren C:\file\path\filename.txt %time:~0,2%%time:~3,2%%time:~6,2%_%date:~4,2%%date:~7,2%%date:~10,4%_filename.txt

Convert Oracle Date to Unix epoch and vice versa

From Oracle Date to Unix epoch

select ( TO_DATE('2010-06-29', 'YYYY-MM-DD')- TO_DATE('1970-01-01', 'YYYY-MM-DD')) * 86400000 from dual;

From UNIX epoch to Oracle date

select TO_DATE('1970-01-01', 'YYYY-MM-DD') + 1277769600000 / 86400000 from dual



JAVA
=====
package mypackage;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;


public class TestClear
{
public TestClear()
{
}
public String getUnixTime1()
{
java.lang.Long unixTime= new Long(System.currentTimeMillis()/1000L);
String unixTimeString = new String(""+unixTime);
//return unixTimeString.substring(0,6);
return unixTimeString;
}
public int getUnixTime2()
{
int year = 2010;
int month = 5;
int date = 29;
Calendar cal = Calendar.getInstance();
cal.clear();
cal.set(Calendar.YEAR, year);
cal.set(Calendar.MONTH, month);
cal.set(Calendar.DATE, date);
java.util.Date utilDate = cal.getTime();
java.lang.Long unixTime= new Long(cal.getTimeInMillis()/1000L);
String unixTimeString = new String(""+unixTime);
unixTimeString= unixTimeString.substring(0,5);
int unixTimeInt = Integer.parseInt(unixTimeString);
return unixTimeInt;
}


/**
*
* @param args
*/
public static void main(String[] args)
{
TestClear t= new TestClear();
System.out.println(t.getUnixTime2());
}
}