Pages

Showing posts with label sql. Show all posts
Showing posts with label sql. Show all posts

Tuesday, October 20, 2015

Oracle Partitions

Need for partition:

Oracle tables which are very huge say(> 2GB) , which inserts data everyday and that have historical and important details for reference, requires Partitions.

Partitions are nothing but smaller segments within the tables for easier maintenance of the tables.

When the content of a table need to be distributed across different types of storage devices, then we need to create partitions.

And finally for performance issue, we need partition. We prefer losing a weekly data instead of the whole table contents :)


Partitioned Index:


In addition to partitioning not only the tables, the Primary keys of the big tables are partitioned as well:

The reasons for partitioning the index are:
  • Avoid rebuilding the entire index when data is removed.
  • Perform maintenance on parts of the data without invalidating the entire index.
  • Reduce the impact of index skew caused by an index on a column with a monotonically increasing value.


Partitions can be created weekly or monthly basis depending of the data inflow:

We can create a dbms_job for creating the weekly(or monthly) partitions. The sample statement for creating partition name is :


We create the below table to show the demonstration about partition:


This sql will create the name of the partition in the format "201545_20151107". This means, that the partition has been created for the week no 45 of the year 2015 and the week ending 07.11.2015. This way you can create a job to create 3 partition names every week. This job can be scheduled to run every Sunday.

Once the names of the partition are created,then we need to create partition in the big tables with the created names.

For example if the table name is TBL_PERSONINFORMATION then partition names can be created as "TBL_PI_201545_20151107". 




Table creation script: 


CREATE TABLE TBL_PERSONALINFORMATION
(
  PERSONID    VARCHAR2(20 CHAR)                 NOT NULL,
  FORENAME    VARCHAR2(1000 CHAR),
  LASTNAME    VARCHAR2(1000 CHAR)               NOT NULL,
  AGE         NUMBER,
  SEX         VARCHAR2(1 BYTE),
  ADDRESS     VARCHAR2(2000 CHAR),
  DATE_ADDED  DATE
)
PARTITION BY RANGE (DATE_ADDED)
( 
  PARTITION TBL_PI_201543_20151024 VALUES LESS THAN (TO_DATE(' 2015-10-25 00:00:00', 'YYYY-MM-DD HH24:MI:SS', 'NLS_CALENDAR=GREGORIAN')) , 
  PARTITION TBL_PI_201544_20151024 VALUES LESS THAN (TO_DATE(' 2015-11-01 00:00:00', 'SYYYY-MM-DD HH24:MI:SS', 'NLS_CALENDAR=GREGORIAN')) , 
  PARTITION TBL_PI_201545_20151024 VALUES LESS THAN (TO_DATE(' 2015-11-07 00:00:00', 'SYYYY-MM-DD HH24:MI:SS', 'NLS_CALENDAR=GREGORIAN'))
 );


Inserting data into the table:


insert into TBL_PERSONALINFORMATION partition (TBL_PI_201543_20151024) values ('10','AA','BB',19,'F','AAA BBB St.',sysdate);

After inserting into the table at the appropriate partition, the table looks like below:


Each partition has sufficient number of rows for our demo.

Before deleting Partition:


Few important points to consider before deleting the partitions:

  • All the FKs must be disabled before deleting
  • Delete the partitions using 'ALTER TABLE.. DROP PARTITION' syntax
  • Enable the FKs back
  • Rebuild the indexes of the partitioned table
  • Delete the data from the referential tables to maintain consistency.

Deleting the Partition:



This way, I have deleted the partition "TBL_PI_201543_20151024".


Quiz Time



Do you know why we disable Foreign Keys before deleting Partitions?

To maintain referential integrity with other tables
As per the syntax
To avoid deleting referential data
online polls

Thursday, October 15, 2015

Oracle Tablespaces

About

Tablespace is created in oracle for allocating space for holding database objects.

There are different types of tablespaces:
  •  PERMANENT TABLESPACE: This tablespace contains persistent schema objects.Objects in permanent tablespaces are stored in datafiles. 
  • An UNDO tablespace is a type of permanent tablespace used by Oracle Database to manage undo data if you are running your database in automatic undo management mode. Oracle strongly recommends that you use automatic undo management mode rather than using rollback segments for undo.
  • A TEMPORARY TABLESPACE contains schema objects only for the duration of a session. Objects in temporary tablespaces are stored in tempfiles.

  
CREATE TABLESPACE

Tablespaces are created with the syntax 'CREATE TABLESPACE'.

Project Tablespace:

In any oracle related project, tablespaces can be created as below:

  • <PROJECT>_BIG_IDX:
All the small Indexes, Index Partitions of size greater than 4 MB are grouped here. INDEXPARTITITON comes under this.

  • <PROJECT>_SMALL_IDX:
All the small Indexes, Index Partitions of size lesser than 5 MB are grouped here. INDEXPARTITITON comes under this.

  • <PROJECT>_BIG_TAB:
All the database objects like LOBINDEX, LOBSEGMENT, TABLE, TABLEPARTITON etc. of size greater than 5 MB are grouped here.

  • <PROJECT>_SMALL_TAB:
All the database objects like LOBINDEX, LOBSEGMENT, TABLE, TABLEPARTITON etc. of size lesser than 5 MB are grouped here.


Re-allocating the database objects in proper tablespaces:

There are cases where we need to reallocate database objects like Tables, Index in the proper Tablespaces on weekly or on monthly basis.

These reallocation will be done based on num_rows, avg_row_len and initial_extent of the Table objects. 


Manual Tablespace reallocation scripts:

Table:
ALTER TABLE <TABLE NAME to be moved> MOVE TABLESPACE <destination TABLESPACE NAME>;

Index:
ALTER INDEX <INDEX_NAME> REBUILD TABLESPACE <TABLESPACE_NAME>;

LOBSEGMENT:
In Order to move CLOB column to different tablespace, use the below command. This will move LOGSEGMENT and LOBINDEX to the new tablespace.

ALTER TABLE <Table_Name> MOVE LOB(<CLOB_Column>) STORE AS (TABLESPACE <Tablespace_name>);

LOBINDEX:
You cannot specify a tablespace for the LOBINDEX -- it is automatically created and moved with the LOBSEGMENT.

TABLE PARTITION:

alter table <table_name> move partition <Table_partition_name> tablespace <tablespace_name>;

INDEX PARTITION:

ALTER INDEX <Index_name> REBUILD PARITION <Partition_Name> TABLESPACE  <Tablespace_name>;


Reference: http://docs.oracle.com/cd/B19306_01/server.102/b14200/statements_7003.htm and my own project experience.