Thursday, January 30, 2014

When to rebuild the indexes

Knowing when to Rebuild Indexes:


There are two rules of thumb to help determine if the index needs to be rebuilt.

1)     If the index has height greater than four, rebuild the index.

2)     The deleted leaf rows (index nodes) should be less than 20%.


Index height


The height of the index refers to the number of levels that are spawned by the index as a result in row inserts. When a large amount of rows are added to a table, Oracle may spawn additional levels of an index to accommodate the new rows. Hence, an Oracle index may have four levels, but only in those areas of the index tree where the massive inserts have occurred. Oracle indexes can support many millions of entries in three levels, and any SAP index that has four or more levels would benefit from rebuilding.


The number of deleted leaf nodes

The term "deleted leaf node" refers to the number of index nodes that have been logically deleted as a result of row deletes. Remember that Oracle leaves "dead" index nodes in the index when rows are deleted. This is done to speed up SQL deletes, since Oracle does not have to rebalance the index tree when rows are deleted.

We must first get an idea of the current state of the index by using the ANALYZE INDEX VALIDATE STRUCTURE command.
The VALIDATE STRUCTURE command can be safely executed without affecting the optimizer. The VALIDATE STRUCTURE command populates the SYS.INDEX_STATS table only. The SYS.INDEX_STATS table can be accessed with the public synonym INDEX_STATS. The INDEX_STATS table will only hold validation information for one index at a time. You will need to query this table before validating the structure of the next index.
Below is a sample output from INDEX_STATS Table.

SQL> ANALYZE INDEX IDX_GAM_ACCT VALIDATE STRUCTURE;
 
Statement processed.
 
SQL> SELECT name, height,lf_rows,lf_blks,del_lf_rows FROM
INDEX_STATS;
NAME                      HEIGHT    LF_ROWS    LF_BLKS    DEL_LF_ROW
---------------------- -----------   ----------      ----------   ----------------
DX_GAM_ACCT           2             1                     3               6
 
1 row selected.
 

SQL> ANALYZE INDEX IDX_GAM_FID VALIDATE STRUCTURE;
 
Statement processed.
 
SQL> SELECT name, height, lf_rows, del_lf_rows, (del_lf_rows/lf_rows)
*100 as ratio FROM INDEX_STATS;
 
NAME                           HEIGHT     LF_ROWS    DEL_LF_ROW RATIO    
------------------------------ ---------- ---------- ---------- -------
IDX_GAM_FID                                  1          189         62        32.80
 
1 row selected.
 
In this example, the ratio of deleted leaf rows to total leaf rows
is clearly above 20%. This is a good candidate for rebuilding.
Let’s rebuild the index and examine the results
 
SQL> ANALYZE INDEX IDX_GAM_FID REBUILD;
 
Statement processed.
 
SQL> ANALYZE INDEX IDX_GAM_FID VALIDATE STRUCTURE;
 
Statement processed.
 
SQL> SELECT name, height, lf_rows, del_lf_rows, (del_lf_rows/lf_rows)*
100 as ratio FROM INDEX_STATS;
 
NAME                           HEIGHT     LF_ROWS    DEL_LF_ROW RATIO    
------------------------------ ---------- ---------- ---------- -------
IDX_GAM_FID                                  1          127         0        0
 
1 row selected.

Examining the INDEX_STATS table shows that the 62 deleted leaf rows were 
dropped from the index. Notice that the total number of leaf rows went from 189 
to 127, which is a difference of 62 leaf rows (189-127). This index should 
provide better performance for the application.


REBUILD INDEXES:


Using ALTER INDEX REBUILD to Rebuild Indexes.

The ALTER INDEX index_name REBUILD command is very safe way to rebuild indexes. Here is the syntax of the command:

Unlike the traditional method where we drop the index and recreate it, the REBUILD command does not require a full table scan of the table, and the subsequent sorting of the keys and rowids. Rather, the REBUILD command will perform the following steps:


  1. Walk the existing index to get the index keys.
  2. Populate temporary segments with the new tree structure.
  3. Once the operation has completed successfully, drop the old tree, and rename the temporary segments to the new index.



RAC: CRS-4640: Oracle High Availability Services is already active


Error:
/opt/grid/11.2.0.3/bin/crsctl start crs
CRS-4640: Oracle High Availability Services is already active
CRS-4000: Command Start failed, or completed with errors.
-bash-3.2#


Reason:
sometimes when CRS server reboot it try to create sockets under /tmp/.oracle or /var/tmp/.oracle and there are already previous socket files...which are protecting to create new sockets.

Solution:
login with root user and remove all files under /tmp/.oracle or /var/tmp/.oracle restart CRS on faulty node or even reboot that machine!, start the crs and it will come up 

cursor delete with commit size



declare
  i number := 0;

 cursor s1
 is

 select seq_num from Tab where <>;


begin
  for c1 in s1 loop

       delete from TAb t1 where t1.seq_num = c1.seq_num;

           i := i + 1;              -- Commit after every X records

      if i > 10000 then
        commit;
        i := 0;
      end if;
  end loop;
      commit;
end;

SQL>

Tuesday, January 28, 2014

exclude in impdp

The below example gives syntax to EXCLUDE multiple tables and multiple schemas while doing a full database export using expdp

=== BEGIN expdp_exclude.par

DIRECTORY=DATA_PUMP_DIR
DUMPFILE=abc.dmp
LOGFILE=abc.log
FULL=Y
EXCLUDE=STATISTICS
EXCLUDE=TABLE:"IN ('NAME', 'ADDRESS' , 'EMPLOYEE' , 'DEPT')"
EXCLUDE=SCHEMA:"IN ('WMSYS', 'OUTLN')"

=== END expdp_exclude.par

In the above example parameter file; tables NAME and ADDRESS are owned by SCOTT and tables EMPLOYEE and DEPT are owned by HR
EXCLUDE=TABLE => You do not have to prefix the OWNER name, in fact, if you put the OWNER.TABLE_NAME, it would not work.
It will EXCLUDE all TABLES having the name mentioned in the list, even if more than one owner has the same object name.
For example: If ADDRESS table is owned by user SCOTT and user HR, that table will be EXCLUDED from both the users.

The above commands would work only via parameter file and would not work on the command line.


COMMAND LINE SYNTAX for EXPDP

expdp system/password DIRECTORY=DATA_PUMP_DIR DUMPFILE=abc.dmp FULL=Y
EXCLUDE=TABLE:\"IN \(\'NAME\', \'ADDRESS\' , \'EMPLOYEE\' , \'DEPT\'\)\"
EXCLUDE=SCHEMA:\"IN \(\'WMSYS\', \'OUTLN\'\)\"

Monday, January 13, 2014

Configure SSH between cluster nodes in Linux


Setting up SSH between the cluster nodes:


Setting up SSH on UNIX and Linux systems involves verifying that the SSH server daemon sshd is running and, if necessary, starting this daemon. Set up SSH on the DAS host and on all hosts where instances in your cluster will reside.
On UNIX and Linux systems, SSH software is typically installed as part of the base operating system. If SSH is not installed, download and install the appropriate OpenSSH SSH package for your operating system.


Each Node should be able to ssh to itself and each other.
If .ssh directory does not exist on the hosts create it.

cd ~  ( Make sure Oracle User Home is set to 750 – chmod 750 ~ )  (Very IMP..)
chmod -R og-rx .ssh ( or chmod 700)

On Node 1
1. cd ~/.ssh

2. Generate Public & Private RSA encryption Key pair
/usr/bin/ssh-keygen -t rsa
Enter file in which to save the key: /users/apps/dba/oracle/.ssh/id_rsa
Enter passphrase: No passphrase

3. Generate Public & Private DSA encryption Key pair
/usr/bin/ssh-keygen -t dsa
Enter file in which to save the key: /users/apps/dba/oracle/.ssh/id_dsa
Enter passphrase: No passphrase

4. cat ~ /.ssh/*.pub >> authorized_keys

5. scp the  authorized_keys to the subsequent Node under /users/apps/dba/oracle/.ssh

Repeat Step 1-5 on all Nodes. Once Step 4 is completed on the Last Node copy the authorized_keys from the Last Node to all other Nodes. This will replace the existing authorized_keys on the hosts with keys from the Last Node in the cluster.
Verify SSH

Make sure you have below soft-links for ssh/scp before you start the OUI Installer.
/usr/bin/ssh -> /opt/boksm/bin/ssh
/usr/bin/scp -> /opt/boksm/bin/scp
/usr/local/bin/ssh -> /opt/boksm/bin/scp
/usr/local/bin/scp -> /opt/boksm/bin/scp

Ssh as the oracle from each host in the cluster as below. If a password is prompted then ssh setup is done incorrectly.

ssh to both short primary host name and full primary host name.

From Host1: ssh Host1 date
From Host1: ssh Host2 date
From Host 2: ssh Host2 date
From Host 2: ssh Host1 date

Verify the above for all remaining hosts in the cluster.

Wednesday, January 8, 2014

sqlplus spool commands


The "spool" command is used within SQL*Plus to direct the output of any query to a server-side flat file.


sql> spool /tmp/myfile.lst

Once spool is set, SQL*Plus will continue to spool the output until the command SPOOL OFF. Note that the file cannot be seen or used until the SPOOL OFF command.

The default file extension is .lst if you dont mention while spooling.
and the default file location is current working directory if you dont mention the location of spool file.

Becuse the spool command interfaces with the OS layer, the spool command is commonly used within Oracle shell scripts.

if you spool out a file that does not exist, SQL*Plus creates a new file.  SQL*Plus replaces the existing file if the file name already exists.  In Oracle 10g, the SPOOL command has a new APPEND option.  Now, you can append new SQL*Plus output in an existing file.

  sql> spool /tmp/myfile.lst append

We can also use environment variables in spool file names.

sql> spool $ORACLE_SID.lst

Few more commands:


sql>; spool name_of_file
sql>; spool name_of_file off
sql>; spool name_of_file out
sql>; spool name_of_file create
sql>; spool name_of_file append
sql>; spool name_of_file replace