Wednesday, April 6, 2011

How to Comapare two Tables Data Difference

Comparing two table is a common task in day to day life of DBA . The Table which has to be compare may on same database or on different database . If the tables are in different database then we can use the database link to compare the two table . Let's have a look on the below tables . 

SQL> select *   from  (
( select * from dept1
  minus                                    //  all rows that are in T1 but not in T2
  select * from dept2 )
union all
( select * from dept2 
  minus                                  // all rows that are in T2 but not in T1
  select * from dept1 )
)




Enjoy    :-) 




Tuesday, April 5, 2011

Setting Up AUTOTRACE in SQL*Plus

AUTOTRACE is a facility  within  SQL*Plus that  shows us the explain  plan of  the  queries that we  have executed  and the resources they used. This topic makes extensive  use  of  the AUTOTRACE facility.There is more than  one way to  get AUTOTRACE configured. This is what I like to do to get AUTOTRACE working :
1.) cd  $ORACLE_HOME\rdbms\admin
2.) log into SQL*Plus as SYSTEM
3.) Run @utlxplan
4.) run the below command 


SQL> create public synonym plan_table for plan_table ; 
SQL> grant all on plan_table to public ;
We can  automatically get  a report  on the  execution path  used by the  SQL optimizer  and  the statement execution statistics. The report is  generated  after  successful  SQL DML ( i.e., select,delete,update,merge and insert ) statements. It  is  useful  for monitoring and  tuning the  performance of these  statements. We can control the  report by  setting  AUTOTRACE  system  variable.

1.) SET AUTOTRACE OFF : No AUTOTRACE report is generated. This is the default.
2.) SET AUTOTRACE ON : The AUTOTRACE report includes both the optimizer execution path and the SQL statement execution statistics. Here is an demo 
SQL> set autotrace on 
SQL>select e.last_name,e.salary,j.job_title from employees e , jobs j where e.job_id=j.job_id and e.salary> 12000 ;
LAST_NAME              SALARY               JOB_TITLE
----------------             ----------          -----------------------
King                           24000              President
Kochhar                     17000              Administration Vice President
De Haan                    17000              Administration Vice President
Russell                      14000               Sales Manager
Partners                    13500               Sales Manager
Hartstein                   13000               Marketing Manager
6 rows selected.


The statement can be automatically traced when it is run :
Execution Plan
----------------------------------------------------------
   0      SELECT STATEMENT Optimizer=CHOOSE
   1    0   TABLE ACCESS (BY INDEX ROWID) OF 'EMPLOYEES'
   2    1     NESTED LOOPS
   3    2       TABLE ACCESS (FULL) OF 'JOBS'
   4    2       INDEX (RANGE SCAN) OF 'EMP_JOB_IX' (NON-UNIQUE)

Statistics
----------------------------------------------------------
          0  recursive calls
          2  db block gets
         34  consistent gets
          0  physical reads
          0  redo size
        848  bytes sent via SQL*Net to client
        503  bytes received via SQL*Net from client
          4  SQL*Net roundtrips to/from client
          0  sorts (memory)
          0  sorts (disk)
          6  rows processed


3.) SET AUTOTRACE ON STATISTICS :  The AUTOTRACE report shows only the SQL statement execution statistics.
SQL>set autotrace on statistics
SQL>select e.last_name,e.salary,j.job_title from employees e , jobs j where e.job_id=j.job_id and e.salary> 12000 ;

LAST_NAME              SALARY              JOB_TITLE
----------------            ----------            -----------------------
King                           24000              President
Kochhar                     17000              Administration Vice President
De Haan                    17000               Administration Vice President
Russell                      14000               Sales Manager
Partners                    13500               Sales Manager
Hartstein                   13000               Marketing Manager
6 rows selected.

Statistics
----------------------------------------------------------
          0  recursive calls
          2  db block gets
         34  consistent gets
          0  physical reads
          0  redo size
        848  bytes sent via SQL*Net to client
        503  bytes received via SQL*Net from client
          4  SQL*Net roundtrips to/from client
          0  sorts (memory)
          0  sorts (disk)
          6  rows processed

4.) SET AUTOTRACE ON EXPLAIN : The AUTOTRACE report shows only the optimizer execution Path.
SQL> set autotrace on explain 
SQL> select e.last_name,e.salary,j.job_title from employees e , jobs j where e.job_id=j.job_id and e.salary> 12000 ;
LAST_NAME              SALARY             JOB_TITLE
----------------          ----------           -----------------------
King                           24000              President
Kochhar                     17000              Administration Vice President
De Haan                    17000               Administration Vice President
Russell                      14000               Sales Manager
Partners                    13500               Sales Manager
Hartstein                   13000               Marketing Manager
6 rows selected.

Execution Plan :
----------------------------------------------------------
   0      SELECT STATEMENT Optimizer=CHOOSE
   1    0   TABLE ACCESS (BY INDEX ROWID) OF 'EMPLOYEES'
   2    1     NESTED LOOPS
   3    2       TABLE ACCESS (FULL) OF 'JOBS'
   4    2       INDEX (RANGE SCAN) OF 'EMP_JOB_IX' (NON-UNIQUE)
5.) SET AUTOTRACE TRACEONLY : This is like SET AUTOTRACE ON, but it suppresses the printing of the user’s query output, if any.
SQL >  set autotrace traceonly 
Execution Plan 
----------------------------------------------------------
   0      SELECT STATEMENT Optimizer=CHOOSE
   1    0   TABLE ACCESS (BY INDEX ROWID) OF 'EMPLOYEES'
   2    1     NESTED LOOPS
   3    2       TABLE ACCESS (FULL) OF 'JOBS' 
   4    2       INDEX (RANGE SCAN) OF 'EMP_JOB_IX' (NON-UNIQUE)
Statistics : 
---------------------------------------------------------
          0  recursive calls
          2  db block gets
         34  consistent gets
          0  physical reads
          0  redo size
        848  bytes sent via SQL*Net to client
        503  bytes received via SQL*Net from client
          4  SQL*Net roundtrips to/from client
          0  sorts (memory)
          0  sorts (disk)
6  rows processed

Enjoy       :)

What is Database Link ?

A database link is a pointer that defines a one-way communication path from an Oracle Database server to another database server. The link pointer is actually defined as an entry in a data dictionary table. A database link is a schema object in one database that enables you to access objects on another database.

To create a private database link, we must have the create database link system privilege. To create a public database link, we  must have the  create public database link  system privilege. Also, we must have the CREATE SESSION system privilege on the remote Oracle database . Before creating it, we must collect the following information:
1.)  A net service name that our local database instance can use to connect to the remote instance and
2.) A valid username and password on the remote database.


The net service name is necessary for every database link. The username and password that we specify when defining a database link are used to establish the connection to the remote instance. The Credentials of database links are : 
Primary Server   =  Noida 
Remote  Server =  Delhi 


1.) Connect to datbase as 
C:\> sqlplus sys/xxxx@delhi as sysdba 


2.) Create a user
SQL> create user abc identified by abc
  2  default tablespace users
  3  quota unlimited on users;
User created.


3.) Grant the privileges required for database link 
SQL> grant create public database link , create session, create table  to abc;
Grant succeeded.


4.) Connect with "ABC" user and create a table for testing purpose as
SQL> conn abc/abc@delhi
Connected.
SQL> create table test1 (id number);
Table created.
SQL> insert into test1 values(&T);
Enter value for t: 23
old   1: insert into test1 values(&T)
new   1: insert into test1 values(23)
1 row created.
SQL>
Enter value for t: 345
old   1: insert into test1 values(&T)
new   1: insert into test1 values(345)
1 row created.
SQL>
Enter value for t: 32
old   1: insert into test1 values(&T)
new   1: insert into test1 values(32)
1 row created.
SQL> commit ;
Commit complete.


SQL> select * from test1 ; 
         ID
----------
        23
       345
        32
SQL> exit


5.) Connect with primary database as
c:\>sqlplus sys/XXXX@noida as sysdba 


6.) Create a Public database link and access the remote table(test1) as
SQL> create public database link d_link connect to abc identified by abc  using  'DELHI' ; 
Database link created 


SQL> select * from abc.test1@d_link ; 
        ID
----------
        23
       345
        32


Hence , we access the remote table by using the database link .


Enjoy     :-) 



Clonning Through DBCA


Cloning is just about creating a copy of production system in to a test or development environment. i.e. Having an exact image of production database in testing environment . Here is one of the easiest way to clone a database . We can clone a database in many different ways .

1.) Clonning using backup of Controlfile
2.) Clonning through DBCA 
3.) Clonning using  OEM 
4.) Clonning using  RMAN Backup

Clonning through DBCA  is one of the least used way to clone or duplicate an Oracle DB but it is the best one. To promote it I write this post. Let's have a look on the steps .

1.) Open the command prompt and type "DBCA" . On the "Welcome" screen click the "Next" button.

2.) On the "Operations" screen select the "Manage Templates" option and click the "Next" button.

3.) On the "Template Management" screen select the "Create a database template" option and select the "From and existing database (structure as well as data)" sub-option then click the "Next" button.

4.) On the "Source database" screen select the relevant database instance and click the "Next" button.

5.) On the "Template properties" screen enter a suitable name and description for the template, confirm the location for the template files and click the "Next" button.

6.) On the "Location of database related files" screen choose either to maintain the file locations or to convert to OFA structure (recommended) and click the "Finish" button.

7.) On the "Confirmation" screen click the "OK" button.

8.) Wait while the Database Configuration Assistant progress screen gathers information about the source database, backs up the database and creates the template.

9.) Depending upon the size of the database it will take some time. For my 8 Gig database, it took like 8 mins. Now we have a template created and we will use to create our new database.

10.) Click on "Next Operation".

11.) Select "Create a Database" option and click "Next".

12.) In "Select a template from the following list to create a database" - select the template name which you provided in Step 6 and click "Next".

13.) Provide the new Service Name for the new database. The SID will automatically be set to the service name entered above. Click "Next".

14.) Let the "Configure the Database with Enterprise Manager" remain checked and "Use Database Control for Database Management" remain checked. Click "Next".

15.) Provide the sys password and click "Next".

16.) Let the "File System" option remain checked unless you want to use ASM or raw for your new database.

17.) Let the "Use Database File Locations from Template remain checked. This is important. Click "Next".

18.) Let the default values for Flash Recover Area remain as they are and click "Next".

19.) Let the "No Scripts to run" remain checked an click "Next".

20.) You can keep the default values for Memory and Sizing over here or change it as per your need and Click "Next".

21.) You are now at the final screen wherein you can all your configurations and verify that they are correct. Clicking next, DBCA will do all your job and your DB should be up and running in next 15-20 mins.

22.) Finally before logging in to the new DB using EM, check the tnsnames.ora and see an entry is created for the new database else add one. You can add a new listenere too in you listener.ora if you want and the do a "lsnrctl reload" to reload the listeners.

23.) Finally do a tnsping on your new database to check all's fine.

24.) Log in using EM and you should have you DB ready in Open mode.

25.) Note all user accounts besides the system account are locked and expired so you need to unlock them to allow users to connect to the new DB.

26.) The whole process took some 30-35 mins and it was all GUI  and no scripts or errors. Seem to be the best way out to duplicate an Oracle 10g database.   


ENJOY   :-)


What is Alert Log File ?

The alert log file is a chronological log of messages and errors written out by an Oracle Database. Typical messages found in this file is: database startup, shutdown, log switches, space errors, etc. This file should constantly be monitored to detect unexpected messages and corruptions.Oracle will automatically create a new alert log file whenever the old one is deleted.

When an internal error is detected by a process, it dumps information about the error to its trace file. Some of the information written to a trace file is intended for the database administrator, while other information is for Oracle Worldwide Support. Trace file information is also used to tune applications and instances.

The alert log of a database includes the following information : 
1. ) All internal errors (ORA-00600), block corruption errors (ORA-01578), and deadlock errors (ORA-00060) that occur.
2.) Administrative operations, such as CREATE, ALTER, and DROP statements and STARTUP, SHUTDOWN, and ARCHIVELOG statements.
3.) Messages and errors relating to the functions of shared server and dispatcher processes.
4.) Errors occurring during the automatic refresh of a materialized view.
5.) The values of all initialization parameters that had non-default values at the time the database and instance startup .

which process writes to alert log file?
Not "one" but all the background processes can/do write to it. The archiver writes to it. LogWriter can write (if we have log_checkpoints_to_alert). When a background process detects that another has died, the former writes to the alert log before panicking the instance and killing it.  Similarly an ALTER SYSTEM command issued by the server process for our database session will also write to the alert.log .

To find the location of alert log file we can find by below command 
SQL > select value from v$parameter where name = 'background_dump_dest' ;   OR 
SQL> show parameter background

If the background_dump_dest parameter is not specified, Oracle will write the alert.log into the $ORACLE_HOME/RDBMS/trace directory.


Enjoy    :-)