oracle Export

Exporting Data from oracle

Oracle provides a command line method for extracting data from the database: export. Use the corresponding import utility to load the data into a destination system.

Note. 11gR2. See for notes on potential problems when taking data from 11gR2

this comes in two forms

  1. exp (deprecated from oracle 10g )
  2. expdp (from Oracle 10g onwards)

these programs are located in the ORACLE_HOME\bin directory. eg \oracle\product\11.2.0\bin\ – \ – your path may be different depending on installation preference and operating system.

  • Note. Imp can only read files generated by exp. Similarly, impdp can only read files generated by expdp. They are not mutually compatible.
  • source exports generated from a higher release than that of the destination database generally will not work – see expdp Compatibility parameter for exception to this rule.

Run the following from the command line as appropriate to your operating system.

eg windows :  start > run > cmd

Depending on your local path setting, you may need to change the program source directory to run the program

eg

c:\>  cd c:\oracle\product\11.2.0\bin\

Using exp

Exp is very useful, especially when executing from a client pc. It is however deprecated and will not be available in future releases.
the command can be run interactively, or as one whole command

to export a schema to a single file:

EXPusername/password FILE=path[filename] LOG=PATH[LOGFILENAME] CONSISTENT=Y

eg exp aesuser/password file=\dumps\mydumpfile LOG=\dumps\mylogfile CONSISTENT=Y
  • this will export the logged in schema to the given filename.

Additional operators can be added onto this command. :

NO ROW DATA

If you do not require the content data and wish only the METADATA, add the operator ” ROWS=N”
eg

exp aesuser/password file=\dumps\mydumpfile  LOG=\dumps\mylogfile  ROWS=N

Good practices

  • Always take care about CHARSETS when you do export and import. Using the wrong ones can convert your data in a lossy manner. The best situation is when your source and destination database have the same character sets, so you can avoid completely any character conversion. You control this behaviour by setting NLS_LANG environment variable appropriately. When not set properly you may see ‘Exporting questionable statistics’ messages
  • You may need to patch your Oracle client (where you are running exp/imp) to the same level as the Oracle server to prevent errors

2. EXPDP

expdp is a powerful took available from Oracle 10g onwards
Requirements:

  1. expdp program
  2. [EMEAOPS:oracle_directory] pre-created. An oracle directory is a logical link from the database to a physical location on the network which is visible from the database server
    to export a schema to a single file:
EXPDP username/password DIRECTORY=mydirectory DUMPFILE=path[filename]  LOGFILE=PATH[LOGFILENAME]

eg expdp aesuser/password DIRECTORY=dumps dumpfile=mydumpfile LOGFILE=mylogfile

  • this will export the logged in schema to the given filename to the location associated with oracle directory “DUMPS”. A logfile will also be output to that same location. Filename of both as specified in the command.

Expdp comes with many operators, below is not exhaustive, but common requirements below.

NO ROW DATA

the operator to use here is
CONTENT=

choices:

CONTENT={ALL | DATA_ONLY | METADATA_ONLY}
  • ALL unloads both data and metadata. This is the default.
  • DATA_ONLY unloads only table row data; no database object definitions are unloaded.
  • METADATA_ONLY unloads only database object definitions; no table row data is unloaded.

example:

EXPDP username/password DIRECTORY=mydirectory DUMPFILE=path[filename] LOGFILE=PATH[LOGFILENAME] CONTENT=xxx

eg expdp aesuser/password DIRECTORY=dumps dumpfile=mydumpfile LOGFILE=mylogfile CONTENT=metadata_only

Exclude named objects

The exclude operator allows us to

  • EXCLUDE=SEQUENCE
    EXCLUDE=TABLE:”IN (’EMP’,’DEPT’)”
    EXCLUDE=INDEX:”= ‘MY_INDX’”

eg

EXPDP username/password DIRECTORY=mydirectory DUMPFILE=path[filename] LOGFILE=PATH[LOGFILENAME] EXCLUDE=XXXX

eg expdp aesuser/password DIRECTORY=dumps dumpfile=mydumpfile LOGFILE=mylogfile EXCLUDE=TABLE:"IN ('PRITEMDATA','PRITEMDATARCHIVE')"
  • the above will export the entire schema with the exception of table PRITEMDATA and PRITEMDATAARCHIVE. ** Note exclude/include list is enclosed in double quotes** object name need to be enclosed in single quotes.

Include names objects

  • INCLUDE=PROCEDURE:”LIKE ‘MY_PROC_%'”
    INCLUDE=TABLE:”‘> ‘E'”

Compatibility

you can set a version to use with expdp for example, if you needed to load the data into an older release than that of the source.

VERSION=
{COMPATIBLE | LATEST | version_string}
  • COMPATIBLE – This is the default value. The version of the metadata corresponds to the database compatibility level. Database compatibility must be set to 9.2 or higher.
  • LATEST – The version of the metadata corresponds to the database version.
  • version_string – A specific database version (for example, 11.1.0). In Oracle Database 11g, this value cannot be lower than 9.2.
EXPDP username/password DIRECTORY=mydirectory DUMPFILE=path[filename] LOGFILE=PATH[LOGFILENAME] EXCLUDE=XXXX VERSION=xxx

eg expdp aesuser/password DIRECTORY=dumps dumpfile=mydumpfile LOGFILE=mylogfile EXCLUDE=TABLE:"IN ('PRITEMDATA','PRITEMDATARCHIVE')" VERSION=11.1

Oracle passwords

Resetting

sql> ALTER USER [username] IDENTIFIED BY [password]

To Reset sys Password

(all actions from a local command line)

Prior to starting sqlplus, first set the oracle sid (shown here as xxxx) – where the oracle_sid is the name of the database you wish to amend.

Linux

TIP : If on linux, first execute “. oraenv ” which will set this sid and other necessary environment variables.

[oracle@DBNODE3 ~]$ . oraenv
ORACLE_SID = [oracle] ? PRESTIGE
The Oracle base for ORACLE_HOME=/opt/mnt1/app/oracle/product/11.2.0/dbhome_1 is /opt/mnt1

[oracle@DBNODE3 ~]$ sqlplus / as sysdba
sql> alter user [username] identified by [password]

Windows

start > run > cmd

SET ORACLE_SID=xxxx
sqlplus / AS sysdba

sql> ALTER USER sys IDENTIFIED BY [password]

Oracle Licensing

Processor licensing

You pay per Processor you run the Oracle software on; however Oracle has a special definition of “processor” which may or may not match that of your hardware vendor.
Intel Hyperthreading technology that makes one core look like two counts as 1 processor for this purpose.

Other soft partitioning technologies are treated differently; for example, the Solaris OS has a concept of Containers, this is similar to hard partitioning on an HP machine; however Oracle does not recognise software partitioning with Solaris Containers prior to Solaris 10; and even then there are stipulations.

Hard partitioning methods such as Sun’s Domains, IBM’s Logical partitioning are recognised as legitimate methods to limit the amount of resources that can run the Oracle software. If you are a MS or VMWare virtual machine user, the following document is a must read; Oracle Partitioning Policy document
Oracle Standard Edition uses a per-socket licensing scheme v Enterprise Edition uses a per-core licensing scheme.

Oracle Standard Edition

If you use Standard Edition or Standard Edition One on a 2 processor system you simply need 2 licenses. However, if you use Enterprise Edition you need to take the number of cores into account as well.

Enterprise Edition Per-core licensing
Multi-core processors are priced as (number of cores)*(multi-core factor) processors, where the multi-core factor is:
This link describes this best: http://www.oracle.com/us/corporate/contracts/processor-core-factor-table-070634.pdf

Oracle Software Download

Sql Tools
Sql Developer is a free cross-platform cross-database SQL tool (Oracle, Mysql & MS-SQL) that is recommended. there are many others!

To see which Oracle Sessions are Blocking

ELECT DISTINCT
s1.username ||'@'|| s1.machine ||'(INST=' || s1.inst_id ||' SID_SERIAL#= '|| s1.sid ||'_'||s1.serial#||')' AS Blocker
,s2.username||'@'||s2.machine||' (INST='||s1.inst_id||' SID_SERIAL# = '||s2.sid ||'_'||s2.serial#||' Event='|| s2.event ||')' AS Blockee
, s2.program ||'@'|| s1.machine AS Program
,SUBSTR(ob.object_name,1,40) ||' SQL -> ' || s3.sql_text AS Blocked_OBJ_SQL, s4.sql_text AS Blocker_SQL
FROM gv$lock l1, gv$session s1, gv$lock l2
, gv$session s2, v$sql s3
, gv$active_session_history ash, v$sql s4, dba_objects ob, gv$locked_object lo
    WHERE s1.sid=l1.sid
    AND s2.sid=l2.sid
    AND l1.block=1
    AND l2.request >0
    AND l1.id1=l2.id1
    AND l2.id2=l2.id2
    AND s2.sql_id = s3.sql_id
    AND s1.prev_sql_id = s4.sql_id
    AND lo.object_id = ob.object_id
    AND lo.SESSION_ID = s2.sid
    AND s2.seconds_in_wait > 10;

Note
1. this is using global view of GV$SESSION_WAIT – and reports back on any session which has waited > 10 seconds. So if your case scenario is looking for sessions which are blocked for less period of time then change the final line in the above script as required.

  AND s2.seconds_in_wait > 10;

Be cautious when amending this time period, as some applications may have by design, plenty of transactions which do cause row lock transactions eg scheduling applications. Normally, the purpose of this script is to look for anomalies.

2. seconds_in_wait has been deprecated from oracle 11g+. Currently is still works in 11gR2. Future releases may not include this column
if this bothers you, then change script line :

 AND s2.seconds_in_wait > 10;
 

to read

 AND s2.WAIT_TIME_MICRO/1000000 > 10;

Test case

We can easily simulate a a blocking lock situation using a test table. We will open two sqlplus sessions to the database and then issue the following commands to demonstrate this

1. using sqlplus connect to the database, eg

C:\sqlplus [username]/[password]@database_name
SQL*Plus: RELEASE 11.2.0.1.0 Production ON Wed Oct 5 13:33:26 2011
Copyright (c) 1982, 2010, Oracle.  ALL rights reserved.
 
Connected TO:
Oracle DATABASE 11g Enterprise Edition RELEASE 11.2.0.1.0 - 64bit Production
WITH the Partitioning, OLAP, Data Mining AND REAL Application Testing options
 
SQL>

Session A.
here, we create a dummy table and insert some test data into it. Check the data via a select, and then lock all rows in the table :

  1. create table test(colA varchar2(1), colB varchar2(1));
  2. insert into test values (1,’a’);
  3. insert into test values (2,’b’);
  4. select * from test ;
  5. commit;
  6. select * from test for update;

 

SQL> CREATE TABLE test(colA VARCHAR2(1), colB VARCHAR2(1));
TABLE created.
SQL> INSERT INTO test VALUES (1,'a');
1 ROW created.
SQL> INSERT INTO test VALUES (1,'b');
1 ROW created.
SQL> SELECT * FROM test ;
C C
- -
1 a
1 b
SQL> COMMIT;
COMMIT complete.
sql> SELECT * FROM test FOR UPDATE;

now we have locked all rows in the table.

so switching to another sqlplus session, using the same connection string as before
eg

C:\sqlplus [username]/[password]@database_name
SQL>

lets first try and see if we can select any row data :

SQL> SELECT * FROM test ;
C C
- -
1 a
1 b

good – expected behaviour as a row or table lock does not normally prevent us from seeing the before image. Now lets try updating a row :

SQL> UPDATE test SET colA='Z' WHERE colA='1';

now you should see the session hangs. And you can prove this from running the script as above giving us the example result of :
example_blocked_oracle_session.JPG

so back to Session A – lets close the lock

SQL> COMMIT;
COMMIT complete.

and then checking session B, you should see that the update has now completed :

SQL> UPDATE test SET colA='Z' WHERE colA='1';
1 ROW updated.

Oracle object wait statistics

common waits are ITL, Buffer and Row Lock contention. knowing the frequency of these is critical to understanding the root cause of any contention. When these occur, the most common remedies for high buffer busy waits include database writer (DBWR) contention tuning, adding freelists (or ASSM), and adding missing indexes. 

When reviewing objects for possible tuning issues, it is handy to have statistics such as the number of internal transaction list (ITL) waits, buffer busy waits and row lock waits that the object has experienced. Combined with the number of logical and physical reads the object has experienced, the above statistics give a complete picture of the usage of the object in question.

To see a list of these per object recorded since the database/instance was last restarted, run the following sql
SELECT * FROM
(
SELECT
DECODE
(GROUPING(a.object_name), 1, 'All Objects', a.object_name)
AS "Object",
SUM(CASE WHEN
a.statistic_name = 'ITL waits'
THEN
a.VALUE ELSE NULL END) "ITL Waits",
SUM(CASE WHEN
a.statistic_name = 'buffer busy waits'
THEN
a.VALUE ELSE NULL END) "Buffer Busy Waits",
SUM(CASE WHEN
a.statistic_name = 'row lock waits'
THEN
a.VALUE ELSE NULL END) "Row Lock Waits",
SUM(CASE WHEN
a.statistic_name = 'physical reads'
THEN
a.VALUE ELSE NULL END) "Physical Reads",
SUM(CASE WHEN
a.statistic_name = 'logical reads'
THEN
a.VALUE ELSE NULL END) "Logical Reads"
FROM
gv$segment_statistics a
WHERE
a.owner LIKE UPPER('&owner')
GROUP BY
rollup(a.object_name)) b
WHERE (b."ITL Waits">0 OR b."Buffer Busy Waits">0)

Oracle Hanging System

When there is an issue with an instance or session hang run this sql

sqlplus ‘/ as sysdba’
oradebug setmypid
oradebug unlimit
oradebug setinst all
oradebug -g all hanganalyze 4 <=== Issue command 3 times one minute apart
oradebug -g all dump systemstate 267 <=== Issue command 3 times one minute apart
quit

help is available :

sql> oradebug help

Trace files will be produced in the user_dump_dest directory. They are named according to the SPID of the process where the oradebug command is executed.

SQL> sho parameter user_dump_dest

NAME                                 TYPE                             VALUE
------------------------------------ -------------------------------- ------------------------------
user_dump_dest                       string                           C:\oracle\diag\rdbms\xxx\xxx1\trace
C:\Temp>set ORACLE_SID=xxxa1

C:\Temp>sqlplus / as sysdba

SQL*Plus: Release 11.1.0.7.0 - Production on Thu Aug 26 08:43:27 2010

Copyright (c) 1982, 2008, Oracle.  All rights reserved.

Connected to:
Oracle Database 11g Release 11.1.0.7.0 - 64bit Production
With the Real Application Clusters option

SQL> set timing on
SQL> set time on
08:43:38 SQL> spool oradebug_xxxx1
08:43:40 SQL> oradebug setmypid

oracle AWR

since 10g we can now use use AWR(automatic workload repository) report to analyse the Oracle Database health.

to run this, use script awrrpt.sql – available from $ORACLE_HOME\rdbms\admin

the script will prompt for input, and is self explanatory in the questions

SQL> @awrrpt
CURRENT Instance
[[USER:bignosekates|1289904198]][[USER:bignosekates|1289904198]][[USER:bignosekates|1289904198]][[USER:bignosekates|1289904198]]
   DB Id    DB Name      Inst Num Instance
----------- ------------ -------- ------------
  255075344 PRESTIGE            1 prestige

Specify the Report TYPE
[[USER:bignosekates|1289904198]][[USER:bignosekates|1289904198]][[USER:bignosekates|1289904198]][[USER:bignosekates|1289904198]][[USER:bignosekates|1289904198]][[USER:bignosekates]]
Would you LIKE an HTML report, OR a plain text report?
Enter 'html' FOR an HTML report, OR 'text' FOR plain text
Defaults TO 'html'
Enter VALUE FOR report_type:

TYPE Specified:                  html

EXT
--------------------------------
.html

Instances IN this Workload Repository SCHEMA
[[USER:bignosekates|1289904198]][[USER:bignosekates|1289904198]][[USER:bignosekates|1289904198]][[USER:bignosekates|1289904198]][[USER:bignosekates|1289904198]][[USER:bignosekates|1289904198]][[USER:bignosekates|1289904198]][[USER:bignosekates|1289904198]][[USER:bignosekates|1289904198]][[USER:bignosekates|1289904198]][[USER:bignosekates|1289904198]]

   DB Id     Inst Num DB Name      Instance     Host
------------ -------- ------------ ------------ ------------
* 255075344         1 PRESTIGE     prestige     Indy-Prestige-DB.inm-uk.net
* 255075344         1 PRESTIGE     prestige     Indy-Prestige-DB.dns1.co.uk
* 255075344         1 PRESTIGE     prestige     vIndy-Prestige

USING  255075344 FOR DATABASE Id
USING          1 FOR instance NUMBER

Specify the NUMBER OF days OF snapshots TO choose FROM
[[USER:bignosekates|1289904198]][[USER:bignosekates|1289904198]][[USER:bignosekates|1289904198]][[USER:bignosekates|1289904198]][[USER:bignosekates|1289904198]][[USER:bignosekates|1289904198]][[USER:bignosekates|1289904198]][[USER:bignosekates|1289904198]][[USER:bignosekates|1289904198]][[USER:bignosekates|1289904198]][[USER:bignosekates|1289904198]][[USER:bignosekates|1289904198]][[USER:bignosekates|1289904198]]~~
Entering the NUMBER OF days (n) will result IN the most recent (n) days OF snapshots being listed.  Pressing <return> without
specifying a NUMBER lists ALL completed snapshots.

Enter VALUE FOR num_days: 1
Listing the last DAY's Completed Snapshots
                                                       Snap
Instance     DB Name        Snap Id    Snap Started    Level
------------ ------------ --------- ------------------ -----
prestige     PRESTIGE         32245 16 Nov 2010 00:00      1
                              32246 16 Nov 2010 01:00      1
                              32247 16 Nov 2010 02:00      1
                              32248 16 Nov 2010 03:00      1
                              32249 16 Nov 2010 04:00      1
                              32250 16 Nov 2010 05:00      1
                              32251 16 Nov 2010 06:01      1
                              32252 16 Nov 2010 07:00      1
                              32253 16 Nov 2010 08:00      1
                              32254 16 Nov 2010 09:00      1
                              32255 16 Nov 2010 10:00      1

Specify the Begin and End Snapshot Ids
[[user:bignosekates|1289904198]][[user:bignosekates|1289904198]][[user:bignosekates|1289904198]][[user:bignosekates|1289904198]][[user:bignosekates|1289904198]][[user:bignosekates|1289904198]][[user:bignosekates|1289904198]][[user:bignosekates|1289904198]][[user:bignosekates|1289904198]]~~
Enter value for begin_snap: 32253
Begin Snapshot Id specified: 32253

Enter value for end_snap: 32255
End   Snapshot Id specified: 32255

Specify the Report Name
[[user:bignosekates|1289904198]][[user:bignosekates|1289904198]][[user:bignosekates|1289904198]][[user:bignosekates|1289904198]][[user:bignosekates|1289904198]][[user:bignosekates]]
The default report file name is awrrpt_1_32253_32255.html.  To use this name, press <return> to continue, otherwise enter an alternative.

Enter value for report_name:
Using the report name awrrpt_1_32253_32255.html

Oracle Directory

 

An oracle directory is a db resident object which points to a location on the network which is visible to the database server.

Create a database directory:

syntax : CREATE OR REPLACE DIRECTORY [logical directory_name] AS [physical DIRECTORY path];

Windows
SQL> CREATE OR REPLACE DIRECTORY dumps AS 'c:\dumps';

Linux
SQL> CREATE OR REPLACE DIRECTORY dumps AS '/dumps';

remember if the directory is to be used by other users outside of the creating user, read and/or write access need to be granted

SQL> GRANT read, WRITE ON DIRECTORY dumps TO scott;

Note – it doesn’t allow sub-directory traversal. So a specific directory needs to be created for each physical directory you wish to use

How to change a password in Oracle

How to change a password in Oracle

Resetting

sql> ALTER USER [username] IDENTIFIED BY [password]

To Reset sys Password

(all actions from a local command line)

Prrior to starting sqlplus, first set the oracle sid (shown here as xxxx) – where the oracle_sid is the name of the database you wish to amend.

Linux

TIP : If on linux, first execute “. oraenv ” which will set this sid and other necessary environment variables.

[oracle@DBNODE3 ~]$ . oraenv
ORACLE_SID = [oracle] ? PRESTIGE
The Oracle base for ORACLE_HOME=/opt/mnt1/app/oracle/product/11.2.0/dbhome_1 is /opt/mnt1

[oracle@DBNODE3 ~]$ sqlplus / as sysdba
sql> alter user [username] identified by [password]

Windows

start > run > cmd

SET ORACLE_SID=xxxx
sqlplus / AS sysdba

sql> ALTER USER sys IDENTIFIED BY [password]

Oracle client tracing

needed to trace a client session today as we have some transactions occuring from the application which intermittently fail for an unknown reason.

so I used these parameters

TRACE_LEVEL_CLIENT= 16
TRACE_DIRECTORY_CLIENT = c:\temp\
TRACE_FILE_CLIENT= client.trc
TRACE_TIMESTAMP_CLIENT = ON
TRACE_UNIQUE_CLIENT = ON

however i became stumped – this was an Oracle 11g system and nothing was being created.

wasn’t until  remembering this parameter that my local trace was being created:
DIAG_ADR_ENABLED=off

 

the reason? I forgot about ADR- Automatic Diagnostic Repository

ref  : http://docs.oracle.com/cd/B28359_01/server.111/b28310/diag001.htm