Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 34

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

New Brokerage Platform Partitions Document


Index: Page

Purpose: 2

Business date Function 2

Daily Partitions 6

Weekly Partitions 13

Monthly Partitions 21

Advance compression 30

Overview of partition indexes 32

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

New Balance Platform (NBP) Routines for Partitions

Purpose: To Create Partitions regularly on the tables with daily, weekly and monthly using the routines.

To create the partitions we need to pass the following arguments to the procedures
1) Schema name: Owner of the Table
2) Table name: Will give the table name
3) Start date: The beginning date of the partition we will give.***
4) End date: The end date to which we will have the partitions.***
5) Compress type: Here we specify the compress type we want to have. Please refer to the End of doc for more Info.
Two options for compress type are i) CFAO (compress for all operations) ( ii) CO (compress)iii)None
6) Tablespace_type (SML) : We will be having 3 Types of Tablespaces on which we will create the partitions
(S for small,M for medium , L for Large). ***
7) V_error
Purpose of the function: Business Date

This function will be skipping the Saturdays and Sundays. It is used by the procedures.
If the date parmeters are passed for weekend they will be ignored

The main points that need to be noted are as follows:


1) Compress option (Please refer to the end of the doc to have a broad idea)
2) Local indexes (Please refer to the end of the doc to have a broad idea)
How to create daily partitions:

Daily Table partitions with the compress Option and the Tablespace selection (SML).
How to execute:
Pre requisites: 1) Date Function should be created.

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

2) User who is executing should have the required permissions.


Example:
Create Function: business_date

CREATE OR REPLACE FUNCTION TDF1496.business_date (start_date DATE,


days2add NUMBER) RETURN DATE IS
Counter NATURAL := 0;
CurDate DATE := start_date;
DayNum POSITIVE;
SkipCntr NATURAL := 0;
Direction INTEGER := 1; -- days after start_date
BusinessDays NUMBER := Days2Add;
BEGIN
IF Days2Add < 0 THEN
Direction := - 1; -- days before start_date
BusinessDays := (-1) * BusinessDays;
END IF;

WHILE Counter < BusinessDays LOOP


CurDate := CurDate + Direction;
DayNum := TO_CHAR( CurDate, 'D');

IF DayNum BETWEEN 2 AND 6 THEN


Counter := Counter + 1;
ELSE
SkipCntr := SkipCntr + 1;
END IF;
END LOOP;

RETURN start_date + (Direction * (Counter + SkipCntr));


END business_date;
/

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

**** Best practices revison


Now Create the Procedure: For the Daily Table Partitions : Gen_Part_Dly :
What we should know before executing this procedure : Arguments that we pass into the Procedure
P_schema_name -> Owner of the Table
P_Table_name -> Table Name on which we want to create the partitions
P_start_date -> from when we should have the partitions created. Ex: 02-MAR-2010
P_end_date -> End date of the partitions ex: 12-MAR-2010
P_compress_type -> Two types of compress features are used we use abbreviations 1) CFAO(compress for all operations) 2) CO(Compress).
P_SML -> Type of the tablespace on which the partitions are created. Types Are S-Small,M- Medium, L- Large

CREATE OR REPLACE PROCEDURE Gen_Part_DLY (


p_schema_name IN VARCHAR2,
p_table_name IN VARCHAR2,
p_start_date IN DATE:= TO_DATE ('mm-dd-yyyy'),
p_end_date IN DATE:= TO_DATE ('mm-dd-yyyy'),
p_compress_type IN VARCHAR2,
p_sml IN VARCHAR2,
p_message OUT VARCHAR2
)
AS
v_partition_date DATE;
v_last_partition DATE;
v_actual_start_date DATE;
v_partition_name VARCHAR2 (50);
v_tblspc_name VARCHAR2 (50);
v_record_count NUMBER;
v_compress_type VARCHAR2 (50);
v_sql VARCHAR2 (2000);
v_invalid_data EXCEPTION;
v_no_table_found EXCEPTION;
v_table_count NUMBER;
BEGIN
SELECT COUNT ( * )
INTO v_table_count
FROM dba_tabLES
WHERE UPPER(table_name) = UPPER (p_table_name) AND owner = UPPER (p_schema_name);

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

IF (v_table_count > 0)
THEN
SELECT COUNT ( * )
INTO v_record_count
FROM dba_tab_partitions
WHERE table_name = UPPER (p_table_name)
AND table_owner = UPPER (p_schema_name);

IF (v_record_count > 0)
THEN
SELECT TO_DATE (SUBSTR (MAX (partition_name), -8), 'YYYYMMDD'),
RTRIM (MAX (partition_name), '0123456789'),
MAX (tablespace_name)
INTO v_last_partition, v_partition_name, v_tblspc_name
FROM dba_tab_partitions
WHERE table_name = UPPER (p_table_name)
AND table_owner = UPPER (p_schema_name)
AND SUBSTR (partition_name, -8) != '99999999';

IF UPPER (P_compress_type) = 'CFAO'


THEN
v_compress_type := ' compress for all operations';
ELSIF UPPER (P_compress_type) = 'CO'
THEN
v_compress_type := ' compress';
ELSIF Upper(p_compress_type)='NONE'
THEN
v_compress_type := ' ';
ELSE
RAISE v_invalid_data;
END IF;

IF (v_last_partition < p_end_date)


THEN
WHILE (v_last_partition < p_end_date)
LOOP

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

v_partition_date := business_date (v_last_partition, 1);


v_sql :=
'alter table '
|| p_schema_name
|| '.'
|| p_table_name
|| ' add partition '
|| v_partition_name
|| TO_CHAR (v_partition_date, 'YYYYMMDD')
|| ' values less than (to_date ('''
|| TO_CHAR (v_partition_date + 1, 'SYYYY-MM-DD HH24:MI:SS')
|| ''', ''SYYYY-MM-DD HH24:MI:SS'', ''NLS_CALENDAR=GREGORIAN'')) tablespace '
|| SUBSTR (v_tblspc_name, 1, 2)
|| p_sml
|| TO_CHAR (v_partition_date, 'YYYYMM')
|| v_compress_type
;
DBMS_OUTPUT.put_line (v_sql);

EXECUTE IMMEDIATE (v_sql);

v_last_partition := v_partition_date;
END LOOP;

p_message := 'Successful';
ELSE
p_message :=
'End date specified is less than the last available partition date: '
|| v_last_partition
;
END IF;
ELSE
p_message := 'There are currently no partitions on the table';
END IF;
ELSE
RAISE v_no_table_found;
END IF;

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

EXCEPTION
WHEN v_no_table_found
THEN
RAISE_APPLICATION_ERROR (
-20735,
'Please Check the table name you may be entered wrong table name or table does not exists'
);
WHEN v_invalid_data
THEN
RAISE_APPLICATION_ERROR (-20734, 'you can only enter CFAO,None or CO');
WHEN OTHERS
THEN
RAISE;
DBMS_OUTPUT.put_line (p_message);
END;
/

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

After the procedure is created: to check the validity create a sample table:
CREATE TABLE DLY_TEST
(
BATCH_DTE_CYMD DATE NOT NULL,
ACCT_NO number
)
TABLESPACE TSHODSTAB1
PARTITION BY RANGE (BATCH_DTE_CYMD)
(
PARTITION P_ACT_RLS_D_20100301 VALUES LESS THAN (TO_DATE(' 2010-03-02 00:00:00', 'SYYYY-MM-DD HH24:MI:SS',
'NLS_CALENDAR=GREGORIAN'))
TABLESPACE TS201003)
/

CREATE UNIQUE INDEX DLY_test_I1U ON DLY_test


(ACCT_NO, BATCH_DTE_CYMD)
COMPRESS LOCAL (
PARTITION P_ACT_RLS_D_20100301
TABLESPACE TS201003)
/

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

set serveroutput on
set define off
variable v_error varchar2(100);
exec Gen_Part_DLY ('TDF1496','DLY_TEST','02-MAR-2010','06-mar-2010','NONE','M',:v_error);

set serveroutput on
set define off
variable v_error varchar2(100);
exec Gen_Part_DLY ('TDF1496','DLY_TEST', to_date('03-24-2010','mm-dd-yyyy'),to_date( '03-31-2010','mm-dd-yyyy'),'CFAO','M', :v_error);

ex: Without Partitions


alter table TDF1496.DLY_TEST add partition P_ACT_RLS_D_20100302 values less than (to_date (' 2010-03-03 00:00:00', 'SYYYY-MM-DD HH24:MI:SS',
'NLS_CALENDAR=GREGORIAN')) tablespace TSM201003
alter table TDF1496.DLY_TEST add partition P_ACT_RLS_D_20100303 values less than (to_date (' 2010-03-04 00:00:00', 'SYYYY-MM-DD HH24:MI:SS',
'NLS_CALENDAR=GREGORIAN')) tablespace TSM201003
alter table TDF1496.DLY_TEST add partition P_ACT_RLS_D_20100304 values less than (to_date (' 2010-03-05 00:00:00', 'SYYYY-MM-DD HH24:MI:SS',
'NLS_CALENDAR=GREGORIAN')) tablespace TSM201003

ex: With Partitions:


set serveroutput on
set define off
variable v_error varchar2(100);
exec Gen_Part_DLY ('TDF1496','DLY_TEST','07-MAR-2010','10-mar-2010','CO','M',:v_error);

alter table TDF1496.DLY_TEST add partition P_ACT_RLS_D_20100309 values less than (to_date (' 2010-03-10 00:00:00', 'SYYYY-MM-DD HH24:MI:SS',
'NLS_CALENDAR=GREGORIAN')) tablespace TSM201003 compress
alter table TDF1496.DLY_TEST add partition P_ACT_RLS_D_20100310 values less than (to_date (' 2010-03-11 00:00:00', 'SYYYY-MM-DD HH24:MI:SS',
'NLS_CALENDAR=GREGORIAN')) tablespace TSM201003 compress
PL/SQL procedure successfully completed.

Hint: Make sure that the Tablespaces exist.

Weekly Table partitions with the compress Option and the Tablespace selection (SML).

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

How to execute:
Pre requisites: 1) Date Function should be created.
2) User who is executing should have the required permissions.

Create the Function: Business date


Create the procedure: Gen_Part_Wkly
CREATE OR REPLACE PROCEDURE tdf1496.gen_part_wkly (
p_schema_name IN VARCHAR2,
p_table_name IN VARCHAR2,
p_end_date IN DATE:= TO_DATE ('mm-dd-yyyy'),
p_compress_type IN VARCHAR2,
p_sml IN VARCHAR2,
p_message OUT VARCHAR2)
AS
v_partition_date DATE;
v_actual_end_date DATE;
v_last_partition DATE;
v_actual_start_date DATE;
v_partition_name VARCHAR2 (50);
v_compress_type VARCHAR2 (50);
v_tblspc_name VARCHAR2 (50);
v_record_count NUMBER;
v_sql VARCHAR2 (20000);
v_no_table_found EXCEPTION;
v_invalid_data EXCEPTION;
v_table_count NUMBER;
BEGIN
SELECT COUNT ( * )
INTO v_table_count
FROM dba_tables
WHERE UPPER (table_name) = UPPER (p_table_name)
AND owner = UPPER (p_schema_name);

IF (v_table_count > 0)
THEN

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

SELECT COUNT ( * )
INTO v_record_count
FROM dba_tab_partitions
WHERE table_name = UPPER (p_table_name)
AND table_owner = UPPER (p_schema_name);

SELECT COUNT ( * )
INTO v_record_count
FROM dba_tab_partitions
WHERE table_name = UPPER (p_table_name)
AND table_owner = UPPER (p_schema_name)
AND SUBSTR (partition_name, -8) != '99999999';

IF (v_record_count > 0)
THEN
SELECT TO_DATE (SUBSTR (MAX (partition_name), -8), 'YYYYMMDD'),
RTRIM (MAX (partition_name), '0123456789'),
MAX (tablespace_name)
INTO v_last_partition, v_partition_name, v_tblspc_name
FROM dba_tab_partitions
WHERE table_name = UPPER (p_table_name)
AND table_owner = UPPER (p_schema_name)
AND SUBSTR (partition_name, -8) != '99999999';

v_actual_end_date :=
p_end_date + (7 - TO_NUMBER (TO_CHAR (p_end_date, 'D')));

IF (v_last_partition < v_actual_end_date)


THEN
WHILE (v_last_partition < v_actual_end_date)
LOOP
v_last_partition := v_last_partition + 7;

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

IF UPPER (p_compress_type) = 'CFAO'


THEN
v_compress_type := ' compress for all operations';
ELSIF UPPER (p_compress_type) = 'CO'
THEN
v_compress_type := ' compress';
ELSIF UPPER (p_compress_type) = 'NONE'
THEN
v_compress_type := ' ';
ELSE
RAISE v_invalid_data;
END IF;

v_sql :=
'alter table '
|| p_schema_name
|| '.'
|| p_table_name
|| ' add partition '
|| v_partition_name
|| TO_CHAR (v_last_partition, 'YYYYMMDD')
|| ' values less than (to_date ('''
|| TO_CHAR (v_last_partition + 1, 'SYYYY-MM-DD HH24:MI:SS')
|| ''', ''SYYYY-MM-DD HH24:MI:SS'', ''NLS_CALENDAR=GREGORIAN'')) tablespace '
|| SUBSTR (v_tblspc_name, 1, 2)
|| p_sml
|| TO_CHAR (v_last_partition, 'YYYYMM')
|| v_compress_type;

DBMS_OUTPUT.put_line (v_sql);

EXECUTE IMMEDIATE (v_sql);


END LOOP;

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

p_message := 'Succesful';
ELSE
p_message :=
'End date specified is less than the last available partition date: '
|| v_last_partition;
END IF;
ELSE
p_message := 'There are currently no partitions on the table';
END IF;
ELSE
RAISE v_no_table_found;
END IF;
EXCEPTION
WHEN v_no_table_found
THEN
raise_application_error (
-20735,
'Please Check the table name you may be entered wrong table name or table doesnot exists');
WHEN v_invalid_data
THEN
raise_application_error (-20734, 'you can only enter CFAO or CO');
WHEN OTHERS
THEN
RAISE;
DBMS_OUTPUT.put_line (p_message);
END;

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

After the procedure is created: to check the validity create a sample table:
CREATE TABLE WKY_TEST
(
BATCH_DTE_CYMD DATE NOT NULL,
BA_RECCODE CHAR(1 BYTE) NOT NULL,
SEC_NO NUMBER(38) NOT NULL,
ACCT_NO NUMBER(38) NOT NULL,
ACCT_TYPE NUMBER(38) NOT NULL,
FIRM_NO NUMBER(38) NOT NULL,
REP CHAR(4 BYTE)

)
TABLESPACE TSHODSTAB1
PARTITION BY RANGE (BATCH_DTE_CYMD)
(
PARTITION P_RCK_W_20100306 VALUES LESS THAN (TO_DATE(' 2010-03-07 00:00:00', 'SYYYY-MM-DD HH24:MI:SS',
'NLS_CALENDAR=GREGORIAN'))
TABLESPACE TS201003)
/

CREATE UNIQUE INDEX BETA_RCK_WKY_IU ON WKY_TEST


(SEC_NO, ACCT_NO, ACCT_TYPE, FIRM_NO, BATCH_DTE_CYMD)
LOCAL (
PARTITION P_RCK_W_20100306
TABLESPACE TS201003)
/

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

Now execute the Routine:


set serveroutput on
set define off
Variable v_error varchar2(100);
exec Gen_Part_Wkly ('TDF1496','WKY_TEST','20-MAR-2010','NONE','S',:v_error);

EX:
alter table TDF1496.WKY_TEST add partition P_RCK_W_20100313 values less than (to_date (' 2010-03-14 00:00:00', 'SYYYY-MM-DD HH24:MI:SS',
'NLS_CALENDAR=GREGORIAN')) tablespace TSS201003
alter table TDF1496.WKY_TEST add partition P_RCK_W_20100320 values less than (to_date (' 2010-03-21 00:00:00', 'SYYYY-MM-DD HH24:MI:SS',
'NLS_CALENDAR=GREGORIAN')) tablespace TSS201003
PL/SQL procedure successfully completed.

set serveroutput on
set define off
Variable v_error varchar2(100);
exec Gen_Part_Wkly ('TDF1496','WKY_TEST','06-APR-2010','cfao','S',:v_error);

alter table TDF1496.WKY_TEST add partition P_RCK_W_20100327 values less than (to_date (' 2010-03-28 00:00:00', 'SYYYY-MM-DD HH24:MI:SS',
'NLS_CALENDAR=GREGORIAN')) tablespace TSS201003 compress for all operations
alter table TDF1496.WKY_TEST add partition P_RCK_W_20100403 values less than (to_date (' 2010-04-04 00:00:00', 'SYYYY-MM-DD HH24:MI:SS',
'NLS_CALENDAR=GREGORIAN')) tablespace TSS201004 compress for all operations
alter table TDF1496.WKY_TEST add partition P_RCK_W_20100410 values less than (to_date (' 2010-04-11 00:00:00', 'SYYYY-MM-DD HH24:MI:SS',
'NLS_CALENDAR=GREGORIAN')) tablespace TSS201004 compress for all operations
PL/SQL procedure successfully completed.

Hint: Make sure that the Tablespaces exist.

Monthly Table partitions with the compress Option and the Tablespace selection (SML).
How to execute:
Pre requisites: 1) Date Function should be created.

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

2) User who is executing should have the required permissions.

set serveroutput on size 100000


set define off
variable v_error varchar2(100);
exec Gen_Part_Mthly ('AFIDBAU','MTH_TEST','06','2010',' compress for all operations','S',:v_error);

Create the Function: Business date


Create the procedure: Gen_Part_Mthly
CREATE OR REPLACE PROCEDURE TDF1496.Gen_Part_Mtly (
p_schema_name IN VARCHAR2,
p_table_name IN VARCHAR2,
p_end_month IN NUMBER,
p_end_year IN NUMBER,
p_compress_type IN VARCHAR2,
p_sml IN VARCHAR2,
p_message OUT VARCHAR2)
AS
v_last_partition VARCHAR2 (10);
v_partition_name VARCHAR2 (50);
v_tblspc_name VARCHAR2 (50);
v_record_count NUMBER;
v_compress_type VARCHAR2 (50);
v_sql VARCHAR2 (4000);
v_last_partition_year NUMBER;
v_last_partition_month NUMBER;
v_no_table_found EXCEPTION;
v_table_count NUMBER;
v_invalid_data EXCEPTION;
BEGIN
SELECT COUNT ( * )
INTO v_table_count
FROM dba_tables
WHERE UPPER (table_name) = UPPER (p_table_name)

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

AND owner = UPPER (p_schema_name);

IF (v_table_count > 0)
THEN
SELECT COUNT ( * )
INTO v_record_count
FROM dba_tab_partitions
WHERE table_name = UPPER (p_table_name)
AND table_owner = UPPER (p_schema_name);

SELECT COUNT ( * )
INTO v_record_count
FROM dba_tab_partitions
WHERE table_name = UPPER (p_table_name)
AND table_owner = UPPER (p_schema_name)
AND SUBSTR (partition_name, -8) != '99999999';

IF (v_record_count > 0)
THEN
SELECT SUBSTR (MAX (partition_name), -6),
RTRIM (MAX (partition_name), '0123456789'),
MAX (tablespace_name)
INTO v_last_partition, v_partition_name, v_tblspc_name
FROM dba_tab_partitions
WHERE table_name = UPPER (p_table_name)
AND table_owner = UPPER (p_schema_name)
AND SUBSTR (partition_name, -8) != '99999999';

v_last_partition_year := TO_NUMBER (SUBSTR (v_last_partition, 1, 4));


v_last_partition_month := TO_NUMBER (SUBSTR (v_last_partition, 5, 2));

IF v_last_partition_year < p_end_year


OR v_last_partition_month < p_end_month
THEN
WHILE v_last_partition_year < p_end_year
OR v_last_partition_month < p_end_month

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

LOOP
IF v_last_partition_month = 12
THEN
v_last_partition_year := v_last_partition_year + 1;

v_last_partition_month := 1;
ELSE
v_last_partition_month := v_last_partition_month + 1;
END IF;

IF UPPER (p_compress_type) = 'CFAO'


THEN
v_compress_type := ' compress for all operations';
ELSIF UPPER (p_compress_type) = 'CO'
THEN
v_compress_type := ' compress';
ELSIF UPPER (p_compress_type) = 'NONE'
THEN
v_compress_type := ' ';
ELSE
RAISE v_invalid_data;
END IF;

IF v_last_partition_month = 12
THEN
v_sql :=
'alter table '
|| p_schema_name
|| '.'
|| p_table_name
|| ' add partition '
|| v_partition_name
|| TO_CHAR (v_last_partition_year)
|| TRIM (TO_CHAR (v_last_partition_month, '00'))
|| ' values less than (to_date ('' '
|| TO_CHAR (v_last_partition_year + 1)

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

|| '-01-01 00:00:00'
|| ''', ''SYYYY-MM-DD HH24:MI:SS'', ''NLS_CALENDAR=GREGORIAN'')) tablespace '
|| SUBSTR (v_tblspc_name, 1, 2)
|| UPPER (p_sml)
|| v_last_partition_year
|| TRIM (TO_CHAR (v_last_partition_month, '00'))
|| v_compress_type;
ELSE
v_sql :=
'alter table '
|| p_schema_name
|| '.'
|| p_table_name
|| ' add partition '
|| v_partition_name
|| v_last_partition_year
|| TRIM (TO_CHAR (v_last_partition_month, '00'))
|| ' values less than (to_date ('' '
|| v_last_partition_year
|| '-'
|| TRIM (TO_CHAR (v_last_partition_month + 1, '00'))
|| '-01 00:00:00'
|| ''', ''SYYYY-MM-DD HH24:MI:SS'', ''NLS_CALENDAR=GREGORIAN'')) tablespace '
|| SUBSTR (v_tblspc_name, 1, 2)
|| UPPER (p_sml)
|| v_last_partition_year
|| TRIM (TO_CHAR (v_last_partition_month, '00'))
|| v_compress_type;
END IF;

DBMS_OUTPUT.put_line (v_sql);

EXECUTE IMMEDIATE (v_sql);


END LOOP;

p_message := 'Successful';
ELSE

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

p_message :=
'End date specified is less than the last available partition date: '
|| v_last_partition;
END IF;
ELSE
p_message := 'There are currently no partitions on the table';
END IF;
ELSE
RAISE v_no_table_found;
END IF;
EXCEPTION
WHEN v_no_table_found
THEN
raise_application_error (
-20735,
'Please Check the table name you may be entered wrong table name or table doesnot exists');
WHEN v_invalid_data
THEN
raise_application_error (-20734, 'you can only enter CFAO,None or CO');
WHEN OTHERS
THEN
DBMS_OUTPUT.put_line (SQLCODE || ':' || SQLERRM);
END;
/

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

After the procedure is created: to check the validity create a sample table:

CREATE TABLE MTH_TEST


(
BATCH_DTE_CYMD DATE NOT NULL,
BA_RECCODE CHAR(1 BYTE) NOT NULL,
ACCT_NO NUMBER(38) NOT NULL,
CATEGORY_CODE VARCHAR2(3 BYTE) NOT NULL,
RLP_UNIQUE_ID VARCHAR2(26 BYTE) NOT NULL,
FIRM_NO NUMBER(38) NOT NULL,
LAST_HODS_UPDT_BA_REC_CDE CHAR(1 BYTE)
)
TABLESPACE TSHODSTAB1
PARTITION BY RANGE (BATCH_DTE_CYMD)
(
PARTITION P_ACTL_RLS_M_201003 VALUES LESS THAN (TO_DATE(' 2010-04-01 00:00:00', 'SYYYY-MM-DD HH24:MI:SS',
'NLS_CALENDAR=GREGORIAN'))
TABLESPACE TS201003)
/
CREATE UNIQUE INDEX BETA_ACT_RLS_MTH_IU ON MTH_TEST
(ACCT_NO, CATEGORY_CODE, RLP_UNIQUE_ID, FIRM_NO, BATCH_DTE_CYMD)
compress LOCAL (
PARTITION P_ACTL_RLS_M_201003
TABLESPACE TSHODSIDX1)
/

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

set serveroutput on size 100000


set define off
variable v_error varchar2(100);
exec Gen_Part_Mtly ('tdf1496','MTH_TEST','04','2010','CFAO','S',:v_error);

Ex:
alter table tdf1496.MTH_TEST add partition P_ACTL_RLS_M_201004 values less than (to_date (' 2010-05-01 00:00:00', 'SYYYY-MM-DD HH24:MI:SS',
'NLS_CALENDAR=GREGORIAN')) tablespace TSS201004 compress for all operations
PL/SQL procedure successfully completed.
set serveroutput on size 100000
set define off
variable v_error varchar2(100);
exec Gen_Part_Mtly ('tdf1496','MTH_TEST','08','2010','None','S',:v_error);
alter table tdf1496.MTH_TEST add partition P_ACTL_RLS_M_201005 values less than (to_date (' 2010-06-01 00:00:00', 'SYYYY-MM-DD HH24:MI:SS',
'NLS_CALENDAR=GREGORIAN')) tablespace TSS201005
alter table tdf1496.MTH_TEST add partition P_ACTL_RLS_M_201006 values less than (to_date (' 2010-07-01 00:00:00', 'SYYYY-MM-DD HH24:MI:SS',
'NLS_CALENDAR=GREGORIAN')) tablespace TSS201006
alter table tdf1496.MTH_TEST add partition P_ACTL_RLS_M_201007 values less than (to_date (' 2010-08-01 00:00:00', 'SYYYY-MM-DD HH24:MI:SS',
'NLS_CALENDAR=GREGORIAN')) tablespace TSS201007
alter table tdf1496.MTH_TEST add partition P_ACTL_RLS_M_201008 values less than (to_date (' 2010-09-01 00:00:00', 'SYYYY-MM-DD HH24:MI:SS',
'NLS_CALENDAR=GREGORIAN')) tablespace TSS201008
PL/SQL procedure successfully completed.
set serveroutput on size 100000
set define off
variable v_error varchar2(100);
exec Gen_Part_Mtly ('tdf1496','MTH_TEST','12','2010','co','S',:v_error);
alter table tdf1496.MTH_TEST add partition P_ACTL_RLS_M_201009 values less than (to_date (' 2010-10-01 00:00:00', 'SYYYY-MM-DD HH24:MI:SS',
'NLS_CALENDAR=GREGORIAN')) tablespace TSS201009 compress
alter table tdf1496.MTH_TEST add partition P_ACTL_RLS_M_201010 values less than (to_date (' 2010-11-01 00:00:00', 'SYYYY-MM-DD HH24:MI:SS',
'NLS_CALENDAR=GREGORIAN')) tablespace TSS201010 compress
alter table tdf1496.MTH_TEST add partition P_ACTL_RLS_M_201011 values less than (to_date (' 2010-12-01 00:00:00', 'SYYYY-MM-DD HH24:MI:SS',
'NLS_CALENDAR=GREGORIAN')) tablespace TSS201011 compress
alter table tdf1496.MTH_TEST add partition P_ACTL_RLS_M_201012 values less than (to_date (' 2011-01-01 00:00:00', 'SYYYY-MM-DD HH24:MI:SS',
'NLS_CALENDAR=GREGORIAN')) tablespace TSS201012 compress
PL/SQL procedure successfully completed.

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

Compress:
Table compression was introduced in Oracle 9i as a space saving feature for data warehousing projects. In 11g it is now considered a
mainstream feature that is acceptable for OLTP databases. In addition to saving storage space, compression can result in increased I/O
performance and reduced memory use in the buffer cache. These advantages do come at a cost, since compression incurs a CPU
overhead, so it won't be of benefit to everyone.

The compression clause can be specified at the tablespace, table or partition level with the following options:
 NOCOMPRESS - The table or partition is not compressed. This is the default action when no compression clause is specified.
 COMPRESS - This option is considered suitable for data warehouse systems. Compression is enabled on the table or partition
during direct-path inserts only.
 COMPRESS FOR DIRECT_LOAD OPERATIONS - This option has the same affect as the simple COMPRESS keyword.
 COMPRESS FOR ALL OPERATIONS - This option is considered suitable for OLTP systems. As the name implies, this option
enables compression for all operations, including regular DML statements. This option requires the COMPATIBLE initialization
parameter to be set to 11.1.0 or higher.
Please note that there is common misconception that 11g table compression decompresses data while reading and holds it in the
uncompressed form in the cache. That is not quite correct since one of the salient features of the database compression is that we do
not have to uncompress the data before reading it and the data stays in the compressed form even in the cache. As a result, Oracle
table compression not only helps customers save disk space, it also helps to increase cache efficiency since more blocks can now fit in
the memory.
Overhead at DML time - Whenever a SQL update, insert of delete changes a data block in RAM, Oracle must determine if the data
block should be unlinked from the freelist (this threshold is defined by the PCTFREE parameter). Compression on write - An
outbound data block must be compressed to fit into it's tertiary block size (as defined by db_block_size and the tablespace blocksize
keyword). For example, an uncompressed block in RAM might occupy up to 96k in RAM and be compressed into it's tertiary disk
blocksize of 32k upon a physical disk write. Decompress on read - At physical read time, incoming disk blocks must be expanded
once and stored in the RAM data buffer. The exact mechanism for this expansion is not published in the Oracle11g documentation,
but it's most likely a block versioning scheme similar to the one used for maintaining read consistency. Increased likelihood of disk

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

contention - Because the data is tightly compressed on the data blocks, more rows can be stored, thus increasing the possibility of
"hot" blocks on disk. Of course, using large data buffers and/or solid-state disk (RAM-SAN) will alleviate this issue.
The restrictions associated with table compression include:
 Compressed tables can only have columns added or dropped if the COMPRESS FOR ALL OPERATIONS option was used.
Compressed tables must not have more than 255 columns.

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

Overview Partitioned Indexes

Partitioned Indexes Provide the same Functionality as the Table Partitions( Improve Manageability, availability, performance,
scalability)

Types of indexes:
i) Global Indexes (Useful for the OLTP systems, Tougher to Manage)
ii) Local Indexes (Useful for the DSS, DWh systems, easier to manage Usually recommended by oracle)

From oracle documentation:


Also, whenever possible, you should try to use local indexes because they are easier to manage. When deciding what kind of
partitioned index to use, you should consider the following guidelines in order:
If the table partitioning column is a subset of the index keys, use a local index.
If the index is unique and does not include the partitioning key columns, then use a global index
If your priority is manageability, use a local index
If the application is an OLTP one and users need quick response times, use a global index.

More on Local partitioned Indexes:


Local Partitioned Indexes
Local partitioned indexes are easier to manage than other types of partitioned indexes. They also offer greater availability and
are common in DSS environments. The reason for this is equipartitioning: each partition of a local index is associated with exactly
one partition of the table. This enables Oracle to automatically keep the index partitions in sync with the table partitions, and
makes each table-index pair independent. Any actions that make one partition's data invalid or unavailable only affect a single
partition.

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

Local partitioned indexes support more availability when there are partition or subpartition maintenance operations on the table. A
type of index called a local nonprefixed index is very useful for historical databases. In this type of index, the partitioning is not
on the left prefix of the index columns.
You cannot explicitly add a partition to a local index. Instead, new partitions are added to local indexes only when you add a
partition to the underlying table. Likewise, you cannot explicitly drop a partition from a local index. Instead, local index partitions
are dropped only when you drop a partition from the underlying table.
A local index can be unique. However, in order for a local index to be unique, the partitioning key of the table must be part of the
index's key columns.

More on global partitioned indexes:


Oracle offers two types of global partitioned indexes: range partitioned and hash partitioned.

Global Range Partitioned Indexes


Global range partitioned indexes are flexible in that the degree of partitioning and the partitioning key are independent from the table's
partitioning method.
The highest partition of a global index must have a partition bound, all of whose values are MAXVALUE. This ensures that all rows
in the underlying table can be represented in the index. Global prefixed indexes can be unique or nonunique.
You cannot add a partition to a global index because the highest partition always has a partition bound of MAXVALUE. If
you wish to add a new highest partition, use the ALTER INDEX SPLIT PARTITION statement. If a global index partition is
empty, you can explicitly drop it by issuing the ALTER INDEX DROP PARTITION statement. If a global index partition contains
data, dropping the partition causes the next highest partition to be marked unusable. You cannot drop the highest partition in a global
index.

Maintenance of Global Partitioned Indexes


 By default, the following operations on partitions on a heap-organized table mark all global indexes as unusable:
 ADD (HASH) ,COALESCE (HASH) ,DROP , EXCHANGE ,MERGE ,MOVE ,SPLIT , TRUNCATE

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

 These indexes can be maintained by appending the clause UPDATE INDEXES to the SQL statements for the operation. The
two advantages to maintaining global indexes:
 The index remains available and online throughout the operation. Hence no other applications are affected by this operation.
 The index doesn't have to be rebuilt after the operation.

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

You might also like