Documente Academic
Documente Profesional
Documente Cultură
1) Introduction
2) Fundamentals
3) Logical and physical structure
4) Oracle Engine, Database Creation
a. tablespace
b. Dump folders SQL
5) Backups & Recovery
6) RMAN
7) Performance tuning
a. AWR
8) Refresh and Cloning:
9) Partial recovery
10) Advanced Topics (ASM, AWR, ADDM, RAC)
11) UNIX
12) Listener
13) standby database/materialized views/
14) Data Guard
15) ASM
16) OEM
17) Patch update
18) Create SQL
1
DBA job types:
1. Production : support phone call 24*7…Error find out, service request (SR)
2. Development: Database creation, Installations
3. Application support DBA: Writing PL/SQL …..Reports generating, adding columns.
UNIX flavors:
Oracle versioning:
9i 9.2.0.1, 9.2.0.8
10g 10.2.0.3
11g 11.1.07 11.2.0
Putty
SQL Developer by Oracle
TOAD
OEM (High; Monitoring)
Oracle:
2
Oracle is a Relational Database Management System (RDBMS), which uses Relational Data Model to store its database and SQL
(commonly abbreviated as Structured Query Language) to process the stored data
Physical structure:
The high water mark is the boundary between used and unused space in a segment.
Logical structure:
Data Blocks:
3
• Oracle manages the storage space in Datafiles of a database in units called data blocks.
• Data block is the smallest unit of data used by a database.
• In physical, OS level, all data is stored in bytes. Each OS has a block size. Oracle requests data in multiples of oracle data
blocks.
Logical: Table, Tablespace
tablespace's data files. Each bit corresponds to a database block or group of blocks.
SQL>select tablespace_name,extent_management from dba_tablespaces;
The DBMS_SPACE_ADMIN package allows DBAs to quickly and easily convert between LMT and DMT mode. Look at these
examples:
Oracle Server:
It contains Memory and processes. When a database is started on a database server (regardless of the type of computer), Oracle
allocates a memory area called the SGA and starts one or more Oracle processes.
4
The basic memory structures associated with Oracle instance include:
1. System Global Area (SGA), which is shared by all server and background processes.
2. Program Global Areas (PGA), which is private to each server and background process; there is one PGA for each process.
Sometimes we call it as Personal Global Area.
Before 11g, we have to allocate memory for SGA and PGA separately. But in 11g, this oracle instance will allocate memory
dynamically to its PGA, and SGA. So, we have to allocate just to complete instance.
SGA is a shared memory region allocated by Oracle that contain data and control information for the Oracle instance.
SGA will allocate memory dynamically for each part of its SGA
1. Shared pool: Used to allocate memory for SQL and PL/SQL execution
Library Cache: It will check syntax, semantics and parsing.
Data Dictionary Cache: Here it will check user privileges such as login, authorizations and table existence.
2. Keep Cache (Optional): It stores frequent queries.
3. DB buffer cache:
4. Redo Log Buffer Cache: It holds the changes in database for recovery. It will store all queries, except SELECT
statements. Default size 50M
The primary function of the redo log is to record all changes made to data. If a failure prevents modified data from
being written to the Datafiles, then the changes can be obtained from the redo log, so work is never lost.
To protect against a failure involving the redo log itself, Oracle allows a multiplexed redo log so that two or more
copies of the redo log can be maintained on different disks.
The process of applying the redo log during a recovery operation is called rolling forward
5
5. Java pool (Optional): used for java objects and other java execution memory
6. Large Pool (Optional)
7. Keep pool (Optional): it will store frequent queires.
All SGA components allocate and deallocate space in units of granules. Oracle Database tracks SGA memory use in internal numbers
of granules for each SGA component.
6
Syntax/Semantics and parsing
Main processes:
LGWR: log-writer process (Redo Log Buffer Redo Log files)
7
PMON: it is responsible for recovery if user process fails. It will rollback uncommitted transactions. It will cleanup database buffer
cache for freeing resources that were allocated to a process.
Every 3 seconds.
SMON: Systems monitor process
If Oracle crashed hard with a power failure, the SMON process is attached at startup time, and detects any uncompleted work, using
the rollback segments to recover the transactions. In addition, SMON performs periodic cleanup of temporary segments that are no
longer needed, and also perform tablespace operations, coalescing contiguous free extents into larger extents.
SMON is responsible for instance recovery and also sometimes startup.
Wakeup for every 5 minutes.
Modified or new data is not necessarily written to a datafile immediately. To reduce the amount of disk access and to increase
performance, data is pooled in memory and written to the appropriate Datafiles all at once, as determined by the database writer
process (DBWn) background process.
CKPT:
Oracle database contain Contains Datafiles, Redo files, and Control files.
Concepts:
8
In previous database releases, a database administrator (DBA) was required to manually specify different SGA component sizes by
setting a number of initialization parameters, including the SHARED_POOL_SIZE, DB_CACHE_SIZE, JAVA_POOL_SIZE, and
LARGE_POOL_SIZE parameters.
Oracle Database 10g includes the Automatic Shared Memory Management feature which simplifies the SGA memory
management significantly. In Oracle Database 10g, a DBA can simply specify the total amount of SGA memory available to an
instance using the SGA_TARGET initialization parameter and the Oracle Database will automatically (Dynamically) distribute this
memory among various subcomponents to ensure most effective memory utilization.
1. SHUTDOWN NORMAL
9
• To initiate an automated and unattended backup
• When a power shutdown is going to occur soon
• When use get this message, user must commit or rollback all his transactions within time.
• No new connections are allowed, nor are new transactions allowed to be started, after the statement is issued.
• Any uncommitted transactions are rolled back. (If long uncommitted transactions exist, this method of
shutdown might not complete quickly, despite its name.)
• Oracle does not wait for users currently connected to the database to disconnect. Oracle implicitly rolls back
active transactions and disconnects all connected users.
Shutting Down with the TRANSACTIONAL Option
When you want to perform a planned shutdown of an instance while allowing active transactions to complete first, use the
SHUTDOWN command with the TRANSACTIONAL option:
SHUTDOWN TRANSACTIONAL
Transactional database shutdown proceeds with the following conditions:
• No new connections are allowed, nor are new transactions allowed to be started, after the statement is issued.
• After all transactions have completed, any client still connected to the instance is disconnected.
• At this point, the instance shuts down just as it would when a SHUTDOWN IMMEDIATE statement is submitted.
The next startup of the database will not require any instance recovery procedures.
A transactional shutdown prevents clients from losing work, and at the same time, does not require all users to log off.
3. SHUTDOWN ABORT
You can shut down a database instantaneously by aborting the database's instance. If possible, perform this type of shutdown
only in the following situations:
o The database or one of its applications is functioning irregularly and none of the other types of shutdown works.
o You need to shut down the database instantaneously (for example, if you know a power shutdown is going to occur in
one minute).
o You experience problems when starting a database instance.
10
o When you must do a database shutdown by aborting transactions and user connections, issue the SHUTDOWN command
with the ABORT option:
o An aborted database shutdown proceeds with the following conditions:
o No new connections are allowed, nor are new transactions allowed to be started, after the statement is issued.
o Current client SQL statements being processed by Oracle are immediately terminated.
o Uncommitted transactions are not rolled back.
o Oracle does not wait for users currently connected to the database to disconnect. Oracle implicitly disconnects all
connected users.
The next startup of the database will require instance recovery procedures.
To start an instance using the standard parameter file, mount the default database, and open the database, enter
STARTUP or STARTUP OPEN database
To start an instance using the standard parameter file, mount the default database, and open the database, enter
STARTUP FORCE RESTRICT MOUNT
Force means restart
Restrict-when some maintenance is going on and
To start an instance using the parameter file TESTPARM without mounting the database, enter
STARTUP PFILE=testparm NOMOUNT
To shutdown a particular database, immediately restart and open it, allow access only to users with the RESTRICTED SESSION
privilege, and use the parameter file MYINIT.ORA.
STARTUP FORCE RESTRICT PFILE=myinit.ora OPEN database
First we will setup Oracle Home, Oracle Base, and Oracle SID.
11
Oracle Base: ORACLE_BASE is an environment variable used as base directory.
/app/Oracle
Oracle SID: The Oracle System identifier is used to uniquely identify a particular database on a system. For this reason, one cannot
have more than one database with the same SID on a computer system.
Dump folders:
Tablespace:
12
2. SYSAUX
It is an auxiliary tablespace to the SYSTEM tablespace Many database components use the SYSAUX tablespace as the default
location to store data.
It is centralized location for database metadata that does not reside in the SYSTEM tablespace.
3. UNDO
These are special tablespaces used solely for storing undo information. Undo tablespaces are used for rolling back transactions.
Each oracle instance is assigned to only one undo tablespace at a time. But we can have nu number of undo tablespaces.
4. TEMP
More than 1 tablespace can be online and active in a database. (after 10g, new concept tablespace group)
When SYSTEM tablespace is locally manages, it is must to define one temporary tablespace when creating tablespace.
If SYSTEM is dictionary managed and we do not define temp tablespace, then SYSTEM is still used for default temporary
storage. However we will receive a warning in ALERT LOG saying that tt is recommended.
5. BIGFILE
Bigilfe tablespace eases database administration because it consists of only 1 datafile with max of 128TB.
One datafile with larger size will take more time compared to number of Datafiles with same size.
1. Create folders for dump files, data files (Datafiles in Admin folder)
2. Create P file
3. Set ORACLE_HOME, and ORACLE_SID or set .oraenv
4. Set PATH=$PATH:$ORACLE_HOME/bin or create spfile
5. Login to sqlplus
sqlplus ‘/as sysdba
6. Instance up
Startup nomount pfile=’/…../pfile.ora’;
7. DB creation
@/…./createdb.sql
8. Running default oracle scripts
@/u01/oracle/rdbms/admin/catalog.sql
@/u01/oracle/rdbms/admin/catproc.sql
13
And after that login as system
SQL>connect system/manager
and run following script
SQL>@/..11.2.0/sqlplus/admin/pubbuild.sql
And again login to sysdba
Connect sys as sysdba
DBCA:
GUI..Just follow the screens…first it will ask for HOME and SID.
To login to database:
$ ORACLE_SID=LAXMAN
$ ORACLE_HOME=/u01/app/product/11.2.0
$ export ORACLE_HOME
$ export ORACLE_SID
$ PATH=$PATH:$ORACLE_HOME/bin
$ export PATH
1. $ sqlplus '/as sysdba' or Sqlplus /as sysdba
And
startup pfile=’…/pfile.ora’;
Or
We can create sp file, once you login to sqlplus it will search for sp file
To create spfile
Create spfile from pfile=’….’;
14
Select * from v$instance
2. To check Datafiles
Select file_name, tablespace_name from dba_data_files;
By default oracle will generate 3 datafiles called system, sysaux, undo.dbf
3. To add tablespace to database
CREATE TABLESPACE my_tablespace DATAFILE ’/u01/oracle/oradata/data01.dbf’ size 100M;
4. To add datafile to tablespace
Alter tablespace <name> add datafile ‘…./newdf.dbf’ size 100M;
5. Rename tablespace
Alter tablespace ts1 rename to ts1new
After installation to check components installed
6. Select comp_name,version,status,= from dba_registry
Some links psoug OnlineAppsDBA
Transaction Management:
Parsed
Found to be valid statement
Run without any error.
15
In a database, there are various types of users which have different responsibilities and rights. The main categories are: Two user accounts are
automatically created with the database and granted the DBA role. These two user accounts are:
2. Physical: We will use RMAN to take physical backups; optionally we can write scripts to take physical backups.
Cold Backup: In cold backup we will shutdown the database and we will take the backup. But it is not consistent.
When taking a cold backup of the database, ensure that database polling is disabled before shutting down the database.
Otherwise, the database will still be opened by the Oracle Fail Safe.
Must mount mode
Hot Backup: no need to shutdown database. It works only if archive log is active.
16
To check archive log status
SQL> archive log list;
If it is “No Active mode” then
Shutdown the database and again startup in mount stage;
SQL>Shutdown immediately;
SQL>startup mount;
Then run following command
SQL> alter database archivelog;
Now again bring database into open mode
SQL> alter database open;
Now list the files to do backup
SQL> select file_name, tablespace_name from dba_data_files;
SQL> alter tablespace users begin backup;
Tablespace altered.
17
It backups only control files, Datafiles, archived redo log files; but not redo log files
1. True incremental
2. Tape support: all major backup systems support inside RMAN. We need Media Management Layer (3rd party software).
3. Multiple I/o channel
4. Compression
5. Block level backup: only we can restore particular backup
6. Skip unused blocks
7. Open database backups: without using open/end backup. Must be archivelog mode
8. Block level recovery
9. Cataloging: a record of RMAN backups stored in target database control file, and optionally in recovery catalog stored in other
database.
10. Platform independent: RMAN scripts can save in recovery catalog for retrieval during a backup session.
18
SQL> CREATE TABLESPACE my_tablespace DATAFILE ’/u01/oracle/oradata/data01.dbf’ size 100M;
b. Create rman user and allocate default tablespace (also account Unlock)
c. Grant permission to user for recovery_catalog_owner
SQL>GRANT RECOVERY_CATALOG_OWNER TO rman;
SQL>alter user rman default tablespace tbsp1 quota unlimited on tbsp1
d. Then connect to RMAN (SID is RMAN database) (possible error: if no listener found then enter command $ lstnrctl
start / or check listener status by $lstnrctl status )
$rman catalog user/password@DatabaseName
Or
$rman catalog user@Database (then it will ask for password)
1. Retension policy: set RMAN to ..days to keep backup by using following command
RMAN>configure retension policy to recovery window of 4 days;
By default it is off, whenever RMAN need space, it will delete old backups.
19
2. Device type: by default it is disk, v can chance this type to tape
RMAN> Configure channel device type sbt
Params=’ENV= (<vendor specific>)’;
NOTE: sbt is device type used for tape, default is Disk
3. Control file Autobackup
RMAN> configure controlfile autobackup on;
4. Backup compression
RMAN>configure device type disk backup to compressed backupset;
We backup database level, tablespace level, datafile level. Whenever we do backup these, it will backup control file and spfile
automatically (if auto controlfile backup is ON).
Performance tuning:
b. SQL Trace: it will execute query really and gives the exact result with proofs. (real poll)
1. If we want to SQL trace for current session (and also we can set identifier to identify later)
20
Alter session set sql_trace =true;
Alter session set tracefile_identifier=myidentifier;
2. To trace other session
First find out SID and serial number of process by using command
SQL> select sid, serial# from sys.v_$session where ...
Then enable tracing for particular process
SQL> ALTER SYSTEM SET timed_statistics = true;
SQL> execute dbms_system.set_sql_trace_in_session(8, 13607, true);
Then execute
SQL>execute dbms_system.set_sql_trace_in_session(8,13607, false);
To trace an entire database
SQL>ALTER SYSTEM SET sql_trace = true SCOPE=MEMORY;
It will create one trace file in Udump with “xx.trc” binary content
To read .trc file, oracle providing tool called TKProf, and it will generate all contents in text file.
Statspack; Up to 9i; it will take snapshot of complete database for every x minutes.
Statspack won’t install by default when installing oracle, we have to create 1 user and generate scripts to install.
After 9i statspack was replaced by AWR from 10g.
21
For every snapshot, it will assign one ID.
Its better if more logical reads (nothing but reads from cache)
Weight events: where database waiting more. Is there any need of creating Indexes? Why it is not using existing indexes?
AWR is html file
AWR Reports Analysis:
ADDM: automatic database Diagnostic Monitoring: just give 2 screenshots; based on data captured on AWR, ADDM diagnose
the performance and major issues.
ADDM text format file
ADDM file contains which query consuming more time. Like 100% impact. Just send it to developers
Do we need SQL Tuning?
----------
Oracle Enterprise Manager (OEM): Maximum we will use only for production.
SQL adviser
It will suggest efficient SQL query also
22
SQL tuning analyzer, dbms analyzer
Partial Recovery:
1. If one table is dropped, but database is running good. (Table is logical; it contains 1 or more Datafiles).
2. If we want to recover one single table we must create auxiliary database (nothing but temp table) and apply archive log.
3. If we want to backup just one user tables.
It is nothing but partial recovery…
Export Username/pwd@dbName
Import uname/pwd@db for recovery
Standby database:
Data Guard
We always maintain one stand by database to production.
For any query, oracle will write necessary actions to both production database and stand by database.
It is replica of original database.
It is mainly for disaster recovery.
Whenever we want to work with production database (upgrading, pat installation, shutdown) then we will up the standby database
and shutdown the actual database. After done with standby database, again we will up the actual db and down standby.
Max Protection mode: it will update both prod DB and Standby DB, then commited/inserted. (network delay, failure)
Max Availability mode: only prod database updates and it will give commited, then in backend it will update standby DB.
23
Max Performance mode: it will give transaction committed statement to user immediately, not a matter Standby is updated or not?
Generally we will use max performance.
Materialized views:
Object
We will create materialized view at development database, with customized separate tables, and also one materialized log in the
original (production) database. 1st we will send all data to MV, after that changed data (just like incremental backup).
It is mainly to analyze production data. Performance, cost.
Create dblink (username, password)
Listener:
If remote connection wants to connect database, a database must be configure its Oracle Net listener.
We will create Listener with ip address, port number for each database.
Application will send request with ip address, port number, listener will connect to particular database
TNS entry
Home/network/admin/listener.ora target database
Home/network/admin/Tnsnames.ora client
For each host only 1 listener (on 1 host, any number of dbs)
V can create any number of TNS entries
24
Generally oracle create automatically when DB installing, or we will configure using “netca” (just like DBCA)
TNSNames.ora in client system with database ip address, port number
Listener.ora on server
Data Dictionary:
25
Data dictionary views:
V$ views are dynamic, use it in database mount or open stage. (Single)
Dba_ views are static; used when only database open mode. Only when DB is open. (Plural)
ASM
ASM lets combine disks, storage, volumes into single pool storage just like volume manager does.
It will provide automatic high performance environment.
Automatically stripes data across storage.
Balances I/O.
ASM cluster file system (ACFS)
OEM:
Mylistener
RAC:
26
• Has a minimum of 3 IP addresses: one for public network, one for private network interconnect, and a virtual IP address to
support faster failover in the event of node failure.
• at least 2 network cards, one is to connect public network for client communication, and other one is for private network
traffic between nodes in cluster
Some points:
• ORACLE_SID is unique on each node.
• Groups oinstall and dba must exist on each node with oracle user
• Both ssh and scp are configured for all nodes in cluster. (rsh and rcp?)
• ASMLib is library for Linux is a support library for ASM instances in Oracle 10g and later.
• Database and recovery files will use ASM disks configured with ASMLib. OCR and voting disks will use raw devices
(Oracle recommended).
NOTE: OCFS is alternative to raw and ASM disks. can be used to store both database files and executables on a common
shared file system.
27
Both these OCR and voting disk must be on separate raw devices. Used by CRS.
When user requested for data, first instance checks cache, if not then it will pull up from data file. (Reading data from cache is
logical read, and reading from storage/Datafiles is physical read; logical read are good for better performance.)
Patch Update:
Metalink.oracle.com
Patches & UpdatesPatch searchyou can search by patch ID or Number or Advance search
For Advance searchProduct is RDBMS Server
And select Release and OS Click Search
(For Example for Oracle 10.2.0.110.2.0.4 on Linux server; Patch ID is 4518443
1. Stand alone patch
2. patch set: combination of patches
Steps:
1. Download the Patch
2. Uninstall the patch (It will create folder with patch ID)
3. Must read ReadMe file before updating contains in directory. (it contains, what bug is fixing, steps, Oracle Version)
4. If we use “opatch apply” command to apply, no need to shutdown database.
Creating tables
SQL>create table employee (id char(9) not null,lastname varchar(10),salary number(10,2),hired date, primary key(id));
SQL> insert into employee values(1,'lucky',200, date '2007-01-01');
UNIX
1. If you want change folder owner (generally Unix admin people will do)
chown –R oracle:dba /….
28
2. For search and replace in vi editor (s-search; g-replace)
1,$ s/old…word/new…word/g
3. To remove file
rm filename
4. To remove folder that contain files
rm –R folder_name
5. List of users
cat /etc/passwd
6. To delete user
userdel username
7. To see ip addresses
cat /etc/hosts
Q: ssh –oracle db
Oracle Commands:
29
9. To findout abt sharedpool desc v$shared_pool_advice; working
13. To view datafiles select table_name from dict where table_name lke ' working
14. desc dba_data_files
15. select name,bytes/1024/1024 from v$datafile;
16. select distinct pool from v$sgastat; or select * from v$sgastat;
17. desc v$sgaas
18. to findout startup time
Select to_char(startup_time, 'HH24:MI DD-MON-YY') "Startup time" from v$instance
19. Ps –ef | grep PMON to see list of instaces running
20. To set editor
SQL>define_editor=vi
21.
Questions:
1. When we will use dictionary managed tablespace?
2. UNDO tablespace is default or we have to create manually?
3. . oraenv? .bash profile?
4. Oracle Instance MOUNT lo opens? Or only open mode.
5. When changing values, old values written to UNDO before COMMIT. And what happens to UNDO after COMMIT
6. If OS supports disk mirroring, control files can also mirrored. What mirroring means?
7. Retention
30
8. Instance recovery only in RAC configuration?
9. Tablespace repository
10. Cursors/Index
11. Do we need to start listener manually or it will start automatically when database created/startup?
If we use dbca, it will configure automatically.
12. Net service name? tnsnames.ora? Listener.ora where they exist? Service??
13. 1 datafile with auto extend? Adding more Datafiles??? Which one is better?
14. Streams (Golden gate purchased by oracle)
15. DBA_HIST tables,
16. wait event analysis; Oracle Wait Interface,
17. Stats Pack (9i) = AWR (10g)
18. We have normal database and now want to change RAC. What are the sets to take??
19. Standby Database/materialized view/DataGuard
20. Disk mirroring
21. OEM Grid
22. Crontab –Unix job scheduler
23. DBS files?
Requirements:
31
TOP
32