Friday, June 21, 2013

Managing Schema Objects

Partitioned Table
Types of Tables:
a) Ordinary(heap oraganized) table: Data is stored as an unordered collection(heap).
b) Partitioned table: Data is divided into smaller, more manageable pieces.
c) Index-organized table: Data is sorted and stored in a B-tree index structure.
d)Clustered table: Related data from one or more tables are stored together.

Partitioning addresses key issues in supporting very large tables and indexes by letting you decompose them into smaller and more manageable pieces called partitions.

Advantages of Partitioning:
#Partitioning enables data management operations such data loads, index creation and rebuilding, and backup/recovery at the partition level, rather than on the entire table. This results in significantly reduced times for these operations.
#Partitioning improves query performance. In many cases, the results of a query can be achieved by accessing a subset of partitions, rather than the entire table. For some queries, this technique (called partition pruning) can provide order-of-magnitude gains in performance.
#Partitioning can significantly reduce the impact of scheduled downtime for maintenance operations.
#Partition independence for partition maintenance operations lets you perform concurrent maintenance operations on different partitions of the same table or index. You can also run concurrent SELECT and DML operations against partitions that are unaffected by maintenance operations.
#Partitioning increases the availability of mission-critical databases if critical tables and indexes are divided into partitions to reduce the maintenance windows, recovery times, and impact of failures.
#Partitioning can be implemented without requiring any modifications to your applications.

Partition Key
Each row in a partitioned table is unambiguously assigned to a single partition. The partition key is a set of one or more columns that determines the partition for each row.

A partition key:
Consists of an ordered list of 1 to 16 columns
Cannot contain a LEVEL, ROWID, or MLSLABEL pseudocolumn or a column of type ROWID
Can contain columns that are NULLable

Range Partitioning
Range partitioning maps data to partitions based on ranges of partition key values that you establish for each partition. It is the most common type of partitioning and is often used with dates. For example, you might want to partition sales data into monthly partitions.
Ex-
CREATE TABLE sales_range
(salesman_id  NUMBER(5),
salesman_name VARCHAR2(30),
sales_amount  NUMBER(10),
sales_date    DATE)
PARTITION BY RANGE(sales_date)
(
PARTITION sales_jan2000 VALUES LESS THAN(TO_DATE('02/01/2000','DD/MM/YYYY')),
PARTITION sales_feb2000 VALUES LESS THAN(TO_DATE('03/01/2000','DD/MM/YYYY')),
PARTITION sales_mar2000 VALUES LESS THAN(TO_DATE('04/01/2000','DD/MM/YYYY')),
PARTITION sales_apr2000 VALUES LESS THAN(TO_DATE('05/01/2000','DD/MM/YYYY'))
);

List Partitioning:
List partitioning enables you to explicitly control how rows map to partitions. You do this by specifying a list of discrete values for the partitioning key in the description for each partition. This is different from range partitioning, where a range of values is associated with a partition and from hash partitioning, where a hash function controls the row-to-partition mapping. The advantage of list partitioning is that you can group and organize unordered and unrelated sets of data in a natural way.
Ex-
CREATE TABLE sales_list
(salesman_id  NUMBER(5),
salesman_name VARCHAR2(30),
sales_state   VARCHAR2(20),
sales_amount  NUMBER(10),
sales_date    DATE)
PARTITION BY LIST(sales_state)
(
PARTITION sales_west VALUES('California', 'Hawaii'),
PARTITION sales_east VALUES ('New York', 'Virginia', 'Florida'),
PARTITION sales_central VALUES('Texas', 'Illinois')
PARTITION sales_other VALUES(DEFAULT)
);

Hash Partitioning
Hash partitioning enables easy partitioning of data that does not lend itself to range or list partitioning. It does this with a simple syntax and is easy to implement.
Ex-
CREATE TABLE sales_hash
(salesman_id  NUMBER(5), 
salesman_name VARCHAR2(30), 
sales_amount  NUMBER(10), 
week_no       NUMBER(2)) 
PARTITION BY HASH(salesman_id) 
PARTITIONS 4 
STORE IN (data1, data2, data3, data4);
                                        
Composite Partitioning
Composite partitioning partitions data using the range method, and within each partition, subpartitions it using the hash or list method. Composite range-hash partitioning provides the improved manageability of range partitioning and the data placement, striping, and parallelism advantages of hash partitioning. Composite range-list partitioning provides the manageability of range partitioning and the explicit control of list partitioning for the subpartitions.
Ex-
CREATE TABLE sales_composite 
(salesman_id  NUMBER(5), 
 salesman_name VARCHAR2(30), 
 sales_amount  NUMBER(10), 
 sales_date    DATE)
PARTITION BY RANGE(sales_date) 
SUBPARTITION BY HASH(salesman_id)
SUBPARTITION TEMPLATE(
SUBPARTITION sp1 TABLESPACE data1,
SUBPARTITION sp2 TABLESPACE data2,
SUBPARTITION sp3 TABLESPACE data3,
SUBPARTITION sp4 TABLESPACE data4)
(PARTITION sales_jan2000 VALUES LESS THAN(TO_DATE('02/01/2000','DD/MM/YYYY'))
 PARTITION sales_feb2000 VALUES LESS THAN(TO_DATE('03/01/2000','DD/MM/YYYY'))
 PARTITION sales_mar2000 VALUES LESS THAN(TO_DATE('04/01/2000','DD/MM/YYYY'))
 PARTITION sales_apr2000 VALUES LESS THAN(TO_DATE('05/01/2000','DD/MM/YYYY'))
 PARTITION sales_may2000 VALUES LESS THAN(TO_DATE('06/01/2000','DD/MM/YYYY')));

When to Partition a Table
Here are some suggestions for when to partition a table:
#Tables greater than 2GB should always be considered for partitioning.
#Tables containing historical data, in which new data is added into the newest partition.

Installation of ASM in oracle database 10g

ASM using ASMLib and Raw Devices

Both ASMLib and raw devices require the candidate disks to be partitioned before they can be accessed. Here, three 10g VMware virtual disks are to be used for the ASM storageThe following text shows the "/dev/sdb" disk being partitioned.

(I have added 3 disks of size 3G each and all on different SCSI disk controller. We usually do this to get most out of the ASM disk mirroring i.e. even if one disk is dead, Oracle will still be able to read data from mirrored copies from other disks. But think about all of your disks are on one disk controller and that whole controller is burnt, then you loose all of your data.)

[root@localhost ~]#  ls /dev/sd*
/dev/sda   /dev/sda2  /dev/sda4  /dev/sdb  
/dev/sda1  /dev/sda3  /dev/sda5  /dev/sdc   /dev/sdd
[root@localhost ~]#  cat /proc/partitions
major minor  #blocks  name
   8     0   36700160 sda
   8     1      48163 sda1
   8     2    4088542 sda2
   8     3   30515467 sda3
   8     4          1       sda4
   8     5    2040223 sda5
   8    16    3145728 sdb
   8    32    3145728 sdc
   8    48    3145728 sdd

Create one partition in all disks
[root@localhost ~]#fdisk /dev/sdb
Device contains neither a valid DOS partition table, nor Sun,
SGI or OSF disklabel
Building a new DOS disklabel. Changes will remain in memory only,
until you decide to write them. After that, of course, the previous
content won't be recoverable.
Warning: invalid flag 0x0000 of partition table 4 will be corrected
by w(rite) 
Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-3): 1
First cylinder (1-391, default 1):
Using default value 1
Last cylinder or +size or +sizeM or +sizeK (1-391, default 391):
Using default value 391
[root@localhost ~]#fdisk /dev/sdc
Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-3): 1
First cylinder (1-391, default 1):
Using default value 1
Last cylinder or +size or +sizeM or +sizeK (1-391, default 391):
Using default value 391
Command (m for help): w 
The partition table has been altered!  
[root@localhost ~]#fdisk /dev/sdd
Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-3): 1
First cylinder (1-391, default 1):
Using default value 1
Last cylinder or +size or +sizeM or +sizeK (1-391, default 391):
Using default value 391
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.

Verify that you have one partition for every disk.
[root@localhost ~]#  cat /proc/partitions
major minor  #blocks  name

   8     0   36700160 sda
   8     1      48163 sda1
   8     2    4088542 sda2
   8     3   30515467 sda3
   8     4          1 sda4
   8     5    2040223 sda5
   8    16    3145728 sdb
  8    32    3145728 sdc
   8    48    3145728 sdd
    
Oracle provides ASM libraries to stamp the disks as ASM disks. These libraries are distributed on the Linux kernel basis. You need to know which kernel version you are using then get the libraries from the OTN website to download libs for your kernel.

Check using the following commands
For checking kernel version:
[root@localhost ~]# uname -r
2.6.9-67.EL
For checking processor type:
[root@localhost ~]# uname -p
i686
For checking hardware platform:
[root@localhost ~]#  uname -i
i386

According to the above information I need following RPMs:
oracleasm-suppurt-2.1.7.el4.i386.rpm
oracleasmlib-2.0.4-1.el4.i386.rpm
oracleasm-2.4.9-67.EL.2.0.3-1.i686.rpm

Installation the rpms as below-
[root@localhost ~]#  rpm –Uvh  oracleasm-suppurt-2.1.7.el4.i386.rpm \
oracleasmlib-2.0.4-1.el4.i386.rpm \
oracleasm-2.4.9-67.EL.2.0.3-1.i686.rpm

With the oracleasm utility installed, configure the kernel module for ASM
[root@localhost ~]# /etc/init.d/oracleasm configure
Configuring the Oracle ASM library driver.

This will configure the on-boot properties of the Oracle ASM library
driver.  The following questions will determine whether the driver is
loaded on boot and what permissions it will have.  The current values
will be shown in brackets ('[]').  Hitting  without typing an
answer will keep that current value.  Ctrl-C will abort.

Default user to own the driver interface []: oracle
Default group to own the driver interface []: oinstall
Start Oracle ASM library driver on boot (y/n) [n]: y
Scan for Oracle ASM disks on boot (y/n) [y]:
Writing Oracle ASM library driver configuration: done
Initializing the Oracle ASMLib driver:                    [  OK  ]
Scanning the system for Oracle ASMLib disks:              [  OK  ]


After the kernel module is updated, label the disks as ASM disks and give then an ASM name.
[root@localhost ~]# /etc/init.d/oracleasm createdisk ASM1 /dev/sdb1
Marking disk "ASM1" as an ASM disk:                       [  OK  ]

[root@localhost ~]# /etc/init.d/oracleasm createdisk ASM2 /dev/sdc1
Marking disk "ASM2" as an ASM disk:                       [  OK  ]

[root@localhost ~]# /etc/init.d/oracleasm createdisk ASM3 /dev/sdd1
Marking disk "ASM3" as an ASM disk:                       [  OK  ]

Issue a scandisks on all ASM disks.
[root@localhost ~]# /etc/init.d/oracleasm scandisks
Scanning the system for Oracle ASMLib disks:              [  OK  ]

listsdisks will list all ASM disks. Make sure you see all the disks you configured.
[root@localhost ~]# /etc/init.d/oracleasm listdisks
ASM1
ASM2
ASM3
#

Configure RAW disks to use as ASM disks:
Open /etc/sysconfig/rawdevices and add following lines:
/dev/raw/raw1 /dev/sdb1
/dev/raw/raw2 /dev/sdc1
/dev/raw/raw3 /dev/sdd1

 Restart the rwdevices service to make your changes effective
Assigning devices:
           /dev/raw/raw1  -->   /dev/sdb1
/dev/raw/raw2:  bound to major 8, minor 17
           /dev/raw/raw2  -->   /dev/sdc1
/dev/raw/raw2:  bound to major 8, minor 33
           /dev/raw/raw3  -->   /dev/sdd1
/dev/raw/raw3:  bound to major 8, minor 49
done

This will bound the disks with raw devices.
Execute the following commands to set access and ownership on the raw disks for user oracle.
Also add these lines in /etc/rc.local to preserve these settings after system restart.
chown oracle:oinstall /dev/raw/raw1
chown oracle:oinstall /dev/raw/raw2
chown oracle:oinstall /dev/raw/raw3
chmod 600 /dev/raw/raw1
chmod 600 /dev/raw/raw2
chmod 600 /dev/raw/raw3

The ASM raw device disks are now configured. 


To be continued in next post