Friday, April 3, 2020

Oracle spool command


What is SPOOL ?Spool Command in ORACLE is used to print data from oracle tables into other files, meaning you can send all the sql outputs into any file you wish to.

How to SPOOL from ORACLE in CSV format ??

Login to sqlplus

Set echo off;
Set Heading off;
Set define Off;
Set feedback Off;
set verify off;
Set serveroutput On;
SET PAGESIZE 5000
SET LINESIZE 120

SQL >   Spool c:\file.csv     (Windows)

SQL >  SELECT COL1||','||COL2||','||COL3 FROM TABLE_NAME;

SQL>  Spool Off;

Set define On;
Set feedback On;
Set heading on;
Set verify on;


Ex:  Recently i written a spool command for making all the tables and indexes max extent sizes to unlimited because lot of tables and indexes max extent size have NULL value

Set echo off;
Set Heading off;
Set define Off;
Set feedback Off;
Set verify off;
Set serveroutput On;
SET PAGESIZE  5000
SET LINESIZE 120

SQL>   Spool   extent.sql

SQL>   select   'alter '||   object_type||’  ‘||object_name||’   '||’ storage (maxextents unlimited);'
            from  dba_objects   where   object_type in ('TABLE','INDEX')   and owner = 'XXX';

spool off

SQL> @extent.sql                       (for executing spool command)

If u didn’t specify anything after the file name(ex: extent  instead of extent.sql) then oracle by default generates output file as ‘.log’ extention(i.e., extent.log)

If we have very few tables in the database instead of writing spool command we can do manually one after another using

SQL >   alter  table  tab_name  move storage (maxextents unlimited);
 Table altered.
Or
SQL>   alter  index  ind_name  move  storage (maxextents unlimited);
 Index altered.

Using single command we can write dynamic sql script to do the same changes for all the objects

NOTE:
In Linux the output can be seen in the Directory from where you entered into the SQLPLUS
In Windows the output file is located where you specified in the spool

APPEND:

If you want the sql output to append into any existing file then you can do the below

login to sqlplus

SQL > spool /opt/oracle/File.log  append




Active Data Guard


Oracle Active Data Guard enhances the performance of production databases by offloading resource intensive operations to one or more standby databases, protecting interactive users and critical business tasks from the impact of long-running operations. Active Data Guard enables a physical standby database to be used for real-time reporting with minimal latency, and compared to traditional replication methods is simple to use, transparently supports all datatypes, and offers very high performance.
                Oracle Active Data Guard enables read-only access to a physical standby database for queries, sorting, reporting, web-based access, etc., while continuously applying changes received from the production database.
                Active Data Guard also enables the use of fast incremental backups when offloading backups to a standby database, and can provide additional benefits of high availability and disaster protection against planned or unplanned outages at the production site.

Features

                   Physical Standby with Real-Time Query
                  Fast Incremental Backup on Physical Standby

Benefits

·        Increase performance - Offload unpredictable workloads to an up-to-date replica of the production database
·        Simplify operations - Eliminate management complexity that accompanies traditional replication solutions
·        Eliminate compromise - The reporting replica is up to date and online at all times, which is not possible with traditional storage mirroring technology
·        Reduce cost - An Oracle Active Data Guard physical standby database can also provide disaster recovery and/or serve as a test database - no additional storage or servers required
·        Enables standby database for use of real-time reporting
·         Offloads backup operations
·        Insulates critical operations from unexpected system impacts
·         Provides high availability and disaster protection

ORA-00018, ORA-00020 maximum number of sessions exceeded

Error Description
Any operation or a new session creation is failed. Like,
ORA-00018: maximum number of sessions exceeded

ORA-00020: maximum number of processes (string) exceeded

In order to solve the problem you have to increase the value of the PROCESSES/ SESSIONS initialization parameter.

You can see the current settings of the SESSIONS and PROCESSES parameter value by querying from v$spparameter or by simply show parameter parameter_name.

SQL> select name, value from v$spparameter where name in ('processes','sessions');


NAME VALUE
------------------------------ ----------
processes 150
sessions 
                                                        (or)

To see the no. of   PROCESSES  &   SESSIONS use command:

SQL> show   parameter  session;
SQL>  show   parameter   process;

The explicitly value of the sessions parameter is not set and so it is left to blank. It's default value is derived from the processes parameter and value=1.1*process_parameter_value+5
You can calculate the default value of sessions parameter by,

SQL> select 1.1*value+5 "sessions par default value" from v$spparameter where name='processes';

sessions par default value
--------------------------
170

In order to change the value of the sessions you have to change it in spfile or pfile. Dynamically it can't be changed.

How to Solve the Problem


If your database start with spfile then,
Alter system set sessions=200 scope=spfile;
or simply you can consider to increase processes parameter by,
Alter system set processes=200 scope=spfile;
shutdown immediate;
startup;

If your database start with pfile then,
Open the pfile with an editor and edit the sessions parameter value and restart your database.

Similarly you can set the processes parameter.

Steps To Generate AWR Report Manually



Step 1: Go to $ORACLE_HOME/rdbms/admin





Step 2: Run command ls -lrt awr*












Step 3: Connect to sqlplus








Step 4:  From the sql prompt run awrrpt (or) awrrpti (for specified instance)

Step 5: it will now ask for the report as either ‘HTML’ or ‘TEXT’. (choose one)

Step 6: Select number of days you want to go back or just hit enter for listing all completed snapshots.


Step 7: Then specify Begin and End snapshot Ids.

Step 8: Here you specify the name of the report or select the default name assigned.

Step 9: The report gets generated.

Step 10: Exit SQLPLUS.

Step 11: Run command ls –ltr newname to show the new file created under the path you are in



Here are some of the scripts:

awrrpt.sql – Produces the AWR report for the current (local) database and instance.
awrrpti.sql - Produces the AWR report for a specified database and instance.
awrgrpt.sql (11g Release 2 and above) - Produces the Global AWR report for all available instances in an Oracle Real Application Clusters (RAC) environment. It does this for the current database.
awrgrpti.sql (11g Release 2 and above) - Produces the Global AWR report for available instances in an Oracle Real Application Clusters (RAC) environment. It does this for a specified database and instances (either one instance, a comma delimited list of some instances or all instances can be chosen).