Monday, November 28, 2016

table space administration on PDB in oracle 12c

adding  tablespace to PDB database
connect to the PDB

SQL> ALTER SESSION SET CONTAINER = pdb4;

Session altered.

SQL> SHOW CON_NAME

CON_NAME
------------------------------
PDB4
SQL> create tablespace test datafile '/media/sf_12cR1/12cDB/cdb1/pdb2/pdb4/test.dbf' size 200m;

Tablespace created.

SQL> SELECT name FROM v$datafile;

NAME
--------------------------------------------------------------------------------
/u01/app/oracle/oradata/cdb1/undotbs01.dbf
/media/sf_12cR1/12cDB/cdb1/pdb2/pdb4/system01.dbf
/media/sf_12cR1/12cDB/cdb1/pdb2/pdb4/sysaux01.dbf
/media/sf_12cR1/12cDB/cdb1/pdb2/pdb4/pdb1_users01.dbf
/media/sf_12cR1/12cDB/cdb1/pdb2/pdb4/test.dbf

SQL> DROP TABLESPACE test INCLUDING CONTENTS AND DATAFILES;

Tablespace dropped.

SQL> SELECT name FROM v$datafile;

NAME
--------------------------------------------------------------------------------
/u01/app/oracle/oradata/cdb1/undotbs01.dbf
/media/sf_12cR1/12cDB/cdb1/pdb2/pdb4/system01.dbf
/media/sf_12cR1/12cDB/cdb1/pdb2/pdb4/sysaux01.dbf
/media/sf_12cR1/12cDB/cdb1/pdb2/pdb4/pdb1_users01.dbf

SQL> SELECT tablespace_name FROM dba_tablespaces;

TABLESPACE_NAME
------------------------------
SYSTEM
SYSAUX
TEMP
USERS

scrip to check the query execution time in oracle database

You can use below query to find out the execution time of the query. Just replace the SQL_ID with your SQL_ID.

SELECT
SQL_ID,
EXECUTIONS,
ELAPSED_TIME/1000000 TOTAL_ELAPSED_TIME_SEC,
ELAPSED_TIME/1000000/EXECUTIONS ELAPSED_TIME_SEC_PER_EXEC,
CPU_TIME/1000000 TOTAL_CPU_TIME_SEC,
CPU_TIME/1000000/EXECUTIONS CPU_TIME_SEC
FROM
V$SQL WHERE SQL_ID='dx647nxstqu260';

To calculate the execution time from the AWR tables, you can use below query.

SELECT
SNAP_ID,
SQL_ID,
EXECUTIONS_DELTA EXECUTIONS,
ELAPSED_TIME_DELTA/1000000 TOTAL_ELAPSED_TIME_SEC,
ELAPSED_TIME_DELTA/1000000/EXECUTIONS_DELTA ELAPSED_TIME_SEC_PER_EXEC,
CPU_TIME_DELTA/1000000 TOTAL_CPU_TIME_SEC,
CPU_TIME_DELTA/1000000/EXECUTIONS_DELTA CPU_TIME_SEC
FROM
DBA_HIST_SQLSTAT WHERE SQL_ID='dx647nxstqu260';

Tuesday, November 22, 2016

steps to Plugin a Pluggable Database (PDB) Manually in oracle 12c

Plugin a Pluggable Database (PDB) Manually

Plugging in a PDB into the CDB is similar to creating a new PDB. First check the PBD is compatible with the CDB by calling the DBMS_PDB.CHECK_PLUG_COMPATIBILITY function, passing in the XML metadata file and the name of the PDB you want to create using it.


SET SERVEROUTPUT ON
DECLARE
  l_result BOOLEAN;
BEGIN
  l_result := DBMS_PDB.check_plug_compatibility(
                pdb_descr_file => '/media/sf_12cR1/12cDB/cdb1/pdb2/pdb2.xml',
                pdb_name       => 'pdb2');

  IF l_result THEN
    DBMS_OUTPUT.PUT_LINE('compatible');
  ELSE
    DBMS_OUTPUT.PUT_LINE('incompatible');
  END IF;
END;
/

SQL>   2    3    4    5    6    7    8    9   10   11   12   13   14  compatible -->it supposed to return compatible to plug in the database again to the container.

PL/SQL procedure successfully completed.


we want to plug the database back into the same container, so we don't need to copy the files or recreate the temp file, so we can do the following.

CREATE PLUGGABLE DATABASE pdb2 USING '/media/sf_12cR1/12cDB/cdb1/pdb2/pdb2.xml'
  NOCOPY
  TEMPFILE REUSE;

ALTER PLUGGABLE DATABASE pdb2 OPEN READ WRITE;

Pluggable database created.

SQL> SELECT name, open_mode
FROM   v$pdbs
ORDER BY name;  2    3

NAME                           OPEN_MODE
------------------------------ ----------
PDB$SEED                       READ ONLY
PDB1                           READ WRITE
PDB2                           MOUNTED
PDB3                           READ WRITE

steps to unpug the PDB database from container in oracle 12c

Unplug a Pluggable Database (PDB) Manually

Before attempting to unplug a PDB, you must make sure it is closed. To unplug the database use the ALTER PLUGGABLE DATABASE command with the UNPLUG INTO clause to specify the location of the XML metadata file.


ALTER PLUGGABLE DATABASE pdb2 CLOSE;
SQL> ALTER PLUGGABLE DATABASE pdb2 CLOSE;

Pluggable database altered.

ALTER PLUGGABLE DATABASE pdb2 UNPLUG INTO '/media/sf_12cR1/12cDB/cdb1/pdb2/pdb2.xml';

SQL> ALTER PLUGGABLE DATABASE pdb2 UNPLUG INTO '/media/sf_12cR1/12cDB/cdb1/pdb2/pdb2.xml';

Pluggable database altered.


SQL> SELECT name, open_mode
FROM   v$pdbs
ORDER BY name;  2    3

NAME                           OPEN_MODE
------------------------------ ----------
PDB$SEED                       READ ONLY
PDB1                           READ WRITE
PDB2                           MOUNTED
PDB3                           READ WRITE



SQL> DROP PLUGGABLE DATABASE pdb2 KEEP DATAFILES;

Pluggable database dropped.

SQL> SELECT name, open_mode
FROM   v$pdbs
ORDER BY name;  2    3

NAME                           OPEN_MODE
------------------------------ ----------
PDB$SEED                       READ ONLY
PDB1                           READ WRITE
PDB3                           READ WRITE

steps to create PDB manually in 12c

Create a Pluggable Database (PDB) Manually

To create a new pluggable database from the seed database, all we have to do is tell Oracle where the file should be placed. We can do this using one of two methods. The first method uses the FILE_NAME_CONVERT clause in the CREATE PLUGGABLE DATABASE statement.

CONN / AS SYSDBA

SQL>CREATE PLUGGABLE DATABASE pdb3 ADMIN USER pdb_adm IDENTIFIED BY Password1
  FILE_NAME_CONVERT=('/u01/app/oracle/oradata/cdb1/pdbseed/','/media/sf_12cR1/12cDB/cdb1/pdb2/');

database created.

SQL> ALTER PLUGGABLE DATABASE pdb3 OPEN READ WRITE;



SQL> SELECT name, open_mode
FROM   v$pdbs
ORDER BY name;
  2    3
NAME                           OPEN_MODE
------------------------------ ----------
PDB$SEED                       READ ONLY
PDB1                           READ WRITE
PDB2                           READ WRITE
PDB3                           READ WRITE



Thursday, November 10, 2016

How to create manual profile in oracle 10g database?

issue:
oracle 10g database sql execution plan got changed and sql performing badly.
root cause:
oracle picked up the new plan which is a bad plan and same time we have good plan in AWR.
we have to force oracle to pick the good plan. but in 10g we dont have baseline features,so we cant forcefully oracle to pick the new plan.
solution:
In oracle 10g database version we don't have SPM and we have some  other  method to pick  the old plan.

we have SQLT feature is there and using it we can create manual profile by passing the SQL_ID and plan_hash_value.

coe_xfr_sql_profile.sql

SQL> START coe_xfr_sql_profile.sql [SQL_ID] [PLAN_HASH_VALUE];
it will create new SQL file to create a profile on the database and we can create the profile on the database.
check the profile status using dba_sql_profiles

Monday, October 3, 2016

what are the various methods for tracing a session in oracle database?

1)ALTER SESSION SET EVENTS ’10046 TRACE NAME CONTEXT FOREVER,LEVEL 12′;

2)DBMS_SYSTEM.SET_SQL_TRACE_IN_SESSION(SID, SERIAL#, TRUE);

DBMS_SYSTEM.SET_SQL_TRACE_IN_SESSION(SID, SERIAL#, FALSE);

3)DBMS_SUPPORT.START_TRACE_IN_SESSION(SID, SERIAL#, WAITS, BINDS);

SQL> @?/rdbms/admin/dbmssupp.sql


4)DBMS_MONITOR.SESSION_TRACE_ENABLE(SESSION_ID, SERIAL_NUM,BINDS,WAITS);

5)how to find the trace file location using sql query?


select
   u_dump.value || '/' ||
   db_name.value || '_ora_' ||
   v$process.spid ||
   nvl2(v$process.traceid, '_' || v$process.traceid, null )||'.trc' "Trace File"
from
   v$parameter u_dump
cross join
   v$parameter db_name
cross join
   v$process
join
   v$session
on
   v$process.addr = v$session.paddr
where
   u_dump.name = 'user_dump_dest'
and
   db_name.name = 'db_name'
and
v$session.audsid=sys_context('userenv','sessionid');

6) convert the trace files to human readble format using tkprof
trprof source_file target_file




Tuesday, September 27, 2016

How to calculate optimal SYSAUX tablespace size ON ORACLE DATABASE?

we have oracle provided script to check optimal  sysaux tablespace size.
script located under rdbms/admin

$ORACLE_HOME/rdbms/admin/utlsyxsz.sql;


SQL> @utlsyxsz.sql;


This script estimates the space required for the SYSAUX tablespace.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Specify the Report File Name
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The default report file name is utlsyxsz.txt.  To use this name,
press <return> to continue, otherwise enter an alternative.

Enter value for report_name: awr.log


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
SYSAUX Size Estimation Report
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Estimated at
12:01:02 on Sep 27, 2016 ( Tuesday ) in Timezone -05:00


DB_NAME     HOST_PLATFORM                             INST STARTUP_TIME      PAR
----------- ---------------------------------------- ----- ----------------- ---
* TEST      servername                1 11:22:14 (07/17)  NO

~~~~~~~~~~~~~~~~~~~~
Current SYSAUX usage
~~~~~~~~~~~~~~~~~~~~
| Total SYSAUX size:                         180.9 MB
|
| Total size of SM/AWR                        76.0 MB (  42.0% of SYSAUX )
| Total size of SM/OPTSTAT                    71.8 MB (  39.7% of SYSAUX )
| Total size of SM/ADVISOR                    10.3 MB (   5.7% of SYSAUX )
| Total size of LOGMNR                         5.9 MB (   3.3% of SYSAUX )
| Total size of SM/OTHER                       4.8 MB (   2.6% of SYSAUX )
| Total size of EM_MONITORING_USER             1.6 MB (   0.9% of SYSAUX )
| Total size of LOGSTDBY                       0.9 MB (   0.5% of SYSAUX )
| Total size of XSOQHIST                       0.8 MB (   0.4% of SYSAUX )
| Total size of AO                             0.8 MB (   0.4% of SYSAUX )
| Total size of STREAMS                        0.5 MB (   0.3% of SYSAUX )
| Total size of JOB_SCHEDULER                  0.4 MB (   0.2% of SYSAUX )
| Total size of TSM                            0.3 MB (   0.1% of SYSAUX )
| Total size of Others                         7.1 MB (   3.9% of SYSAUX )
|

~~~~~~~~~~~~~~~~~~~~
AWR Space Estimation
~~~~~~~~~~~~~~~~~~~~

| To estimate the size of the Automatic Workload Repository (AWR)
| in SYSAUX, we need the following values:
|
|     - Interval Setting (minutes)
|     - Retention Setting (days)
|     - Number of Instances
|     - Average Number of Active Sessions
|     - Number of Datafiles

|
| For 'Interval Setting',
|   Press <return> to use the current value:     60.0 minutes
|   otherwise enter an alternative
|
Enter value for interval: 30

**   Value for 'Interval Setting': 30

|
| For 'Retention Setting',
|   Press <return> to use the current value:   7.00 days
|   otherwise enter an alternative
|
Enter value for retention: 7

**   Value for 'Retention Setting': 7

|
| For 'Number of Instances',
|   Press <return> to use the current value:   1.00
|   otherwise enter an alternative
|
Enter value for num_instances: 1

**   Value for 'Number of Instances': 1

|
| For 'Average Number of Active Sessions',
|   Press <return> to use the current value:   0.00
|   otherwise enter an alternative
|
Enter value for active_sessions: 10

**   Value for 'Average Number of Active Sessions': 10

| ***************************************************
| Estimated size of AWR:                     310.5 MB
|
|   The AWR estimate was computed using
|   the following values:
|
|            Interval -        30 minutes
|           Retention -      7.00 days
|       Num Instances -         1
|     Active Sessions -     10.00
|           Datafiles -         7
| ***************************************************

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Optimizer Stat History Space Estimation
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

| To estimate the size of the Optimizer Statistics History
| we need the following values:
|
|     - Number of Tables in the Database
|     - Number of Partitions in the Database
|     - Statistics Retention Period (days)
|     - DML Activity in the Database (level)

|
| For 'Number of Tables',
|   Press <return> to use the current value:    663.0
|   otherwise enter an alternative <a positive integer>
|
Enter value for number_of_tables:

**   Value for 'Number of Tables': 663

|
| For 'Number of Partitions',
|   Press <return> to use the current value:   0.00
|   otherwise enter an alternative <a positive integer>
|
Enter value for number_of_partitions:

**   Value for 'Number of Partitions': 0

|
| For 'Statistics Retention',
|   Press <return> to use the current value:     31.0 days
|   otherwise enter an alternative <a positive integer>
|
Enter value for stats_retention:

**   Value for 'Statistics Retention': 31

|
| For 'DML Activity',
|   Press <return> to use the current value:        2 <medium>
|   otherwise enter an alternative <1=low, 2=medium, 3=high>
|
Enter value for dml_activity:

**   Value for 'DML Activity': 2

| ***************************************************
| Estimated size of Stats history             23.8 MB
|
|   The space for Optimizer Statistics history was
|   estimated using the following values:
|
|                         Tables -     663
|                        Indexes -   1,313
|                        Columns -   4,282
|                     Partitions -       0
|          Indexes on Partitions -       0
|          Columns in Partitions -       0
|        Stats Retention in Days -      31
|          Level of DML Activity -  Medium
| ***************************************************

~~~~~~~~~~~~~~~~~~~~~~
Estimated SYSAUX usage
~~~~~~~~~~~~~~~~~~~~~~

| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| Estimated size of AWR:                     310.5 MB
|
|   The AWR estimate was computed using
|   the following values:
|
|            Interval -        30 minutes
|           Retention -      7.00 days
|       Num Instances -         1
|     Active Sessions -     10.00
|           Datafiles -         7
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| Estimated size of Stats history             23.8 MB
|
|   The space for Optimizer Statistics history was
|   estimated using the following values:
|
|                         Tables -     663
|                        Indexes -   1,313
|                        Columns -   4,282
|                     Partitions -       0
|          Indexes on Partitions -       0
|          Columns in Partitions -       0
|        Stats Retention in Days -      31
|          Level of DML Activity -  Medium
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|   For all the other components, the estimate
|   is equal to the current space usage of
|   the component.
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
| ***************************************************
| Summary of SYSAUX Space Estimation
| ***************************************************

| Est size of SM/ADVISOR                      10.3 MB
| Est size of LOGMNR                           5.9 MB
| Est size of SM/OTHER                         4.8 MB
| Est size of EM_MONITORING_USER               1.6 MB
| Est size of LOGSTDBY                         0.9 MB
| Est size of XSOQHIST                         0.8 MB
| Est size of AO                               0.8 MB
| Est size of STREAMS                          0.5 MB
| Est size of JOB_SCHEDULER                    0.4 MB
| Est size of TSM                              0.3 MB
| Est size of Others                           7.1 MB

| Est size of SM/AWR                         310.5 MB
| Est size of SM/OPTSTAT                      23.8 MB
|
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| Total Estimated SYSAUX size:               367.4 MB
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| ***************************************************

End of Report

Tuesday, September 20, 2016

MS SQL server mirroring concepts

All about SQL server mirroring
pre-request for SQL server mirroring


1.    mirroring available after 2005
2.     Primary Db should be in full recovery mode
3.     mirrored Db in recovery mode
4.     Primary & Mirrored Db server has same version & same Sservic pack same version (either Standard or Enterprise)
5.     witness server has been  anything sql express also
6.     Server should be on same domain name. becoz it communicates with AD
7.     you cannot use this as a secondary read-only database to achieve scalability
8.     Database name same on both servers
9.     Operating mode




High-performance mode
Transaction safety
Witness state
OFF
NULL (no witness)2
High-safety mode without automatic failover
FULL
NULL (no witness)
High-safety mode with automatic failover1
FULL
CONNECTED

10) to check the mirror state

SELECT mirroring_safety_level_desc, mirroring_witness_name, mirroring_witness_state_desc FROM sys.database_mirroring
11) protection modes
high performance
high safety
high safety with automatic fail-over


12)Restrictions for DB mirroring:
Only user databases can be mirrored. You cannot mirror the master, msdb, tempdb, or model databases.
A mirrored database cannot be renamed during a database mirroring session.
Database mirroring does not support FILESTREAM. A FILESTREAM filegroup cannot be created on the principal server. Database mirroring cannot be configured for a database that contains FILESTREAM filegroups.
Database mirroring is not supported with either cross-database transactions or distributed transaction
Maximum 10 databases per instance can support on a 32-bit system.
b) Database mirroring is not supported with either cross-database transactions or distributed transactions.


Advantages of mirroring include automatic fixing of corrupted pages and storage is not a SPOF


13)What is End Point? How u create end point?
An endpoint is a network protocol which is used to communicate Principal, Mirror and Witness servers over the network.
Creation of an end point:-
Create endpoint <endpoint name> State=started/stopped/disabled
as tcp (listener port=5022/5023) for database_mirroring (role=partner/witness)

14)What is the default of end points (port numbers) of principal, mirror and witness servers? How to find the Port numbers?
The default port numbers of principal, mirror and Witness servers are 5022, 5023 and 5024.
To Find Port Number:- SELECT name, port FROM sys.tcp_endpoints

15)In which Recovery model we can use in Mirroring?
In mirroring the principal and mirror databases are used only full recovery model

16)What is the syntax to stop the Database Mirroring?
Alter database <database name> set partner off

17)How to monitoring Mirroring?

There are six methods are available for monitoring the Database Mirroring
a) Database Mirroring Monitor:- Database Mirroring Monitor is a GUI tool that shows update status and to configure warning thresholds.
To open DM Monitor:- Right click on Principal Database > Tasks > Select Launch Database Mirroring Monitor.
b) SQL Server Management Studio:- A green arrow on the mirror server is indicates running well. A red arrow indicates problems that need to investigate.
c) SQL Server Log:- It provides information of Mirroring establishment and status. If any errors occurs it will be logged to SQL Server log and Windows event log.
d) Performance Monitor:- It can provides real-time information about Database mirroring. We can use performance counters to get status of the database mirroring such as Bytes received/sec, Bytes sent/sec, Transaction delay etc.
e) Profiler:- Profiler many events are providing the status of the Database mirroring
f) System Stored Procedures:-
? sp_dbmmonitoraddmonitoring
? sp_dbmmonitorchangemonitoring
? sp_dbmmonitorhelpmonitoring
? sp_dbmmonitordropmonitoring


18)What are the Database Mirroring states?

1) SYNCHRONIZING:-
The contents of the mirror database are lagging behind the contents of the principal database. The principal server is sending log records to the mirror server, which is applying the changes to the mirror database to roll it forward.
At the start of a database mirroring session, the database is in the SYNCHRONIZING state. The principal server is serving the database, and the mirror is trying to catch up.
2) SYNCHRONIZED:-
When the mirror server becomes sufficiently caught up to the principal server, the mirroring state changes to SYNCHRONIZED. The database remains in this state as long as the principal server continues to send changes to the mirror server and the mirror server continues to apply changes to the mirror database.
If transaction safety is set to FULL, automatic failover and manual failover are both supported in the SYNCHRONIZED state, there is no data loss after a failover.
If transaction safety is off, some data loss is always possible, even in the SYNCHRONIZED state.
3) SUSPENDED:-
The mirror copy of the database is not available. The principal database is running without sending any logs to the mirror server, a condition known as running exposed. This is the state after a failover.
  A session can also become SUSPENDED as a result of redo errors or if the administrator pauses the session
  SUSPENDED is a persistent state that survives partner shutdowns and startups.
4) PENDING_FAILOVER:-
  This state is found only on the principal server after a failover has begun, but the server has not transitioned into the mirror role.
  When the failover is initiated, the principal database goes into the PENDING_FAILOVER state, quickly terminates any user connections, and takes over the mirror role soon thereafter.
5) DISCONNECTED:-
  The partner has lost communication with the other partner

Database mirroring provides protection at the database level, whereas a cluster solution provides protection at the SQL Server instance level

19)What are the Disadvantages of Database Mirroring?

Potential data lost is possible in asynchronous operation mode. RTO will vary and depend on several factors, such as propagation interval time and bandwidth speed.
It only works at database level and not at server level. It only propagates changes at database level, no server level objects, such as logins and fixed server role membership, can be propagated.
Automatic server failover may not be suitable for application using multiple databases.

20)query to check mirroring status.



select  mirroring_state,mirroring_state_desc,mirroring_role,mirroring_role_desc,mirroring_safety_level_desc,mirroring_partner_name,mirroring_witness_name  from sys.database_mirroring;

ORA-19588: archived log RECID 2986343 STAMP 4532923 is no longer valid

ORA-19588: archived log RECID 2986343 STAMP 4532923 is no longer valid

check if the SEQ log backed up

LIST BACKUP OF ARCHIVELOG FROM SEQUENCE <SEQ Number>,if its backed up you can ignore it

cause:
Root cause could be due to 2 simultaneous backup sessions where one refers to and the other had already backed up and not needed to be backed up.