Sunday 29 May 2016

ORACLE LATCH



What is Latch ?

A mechanism to protect shared data structures in the System Global Area.
For Example: latches protect the list of users currently accessing the database and protect the data structures describing the blocks in the buffer cache.

A server or background process acquires a latch for a very short time while manipulating or looking at one of these structures.

During DB performance we will see LATCH event .

What is latch event and how many types of latch events ?

A latch is a low-level internal lock used by Oracle to protect memory structures.
The latch free event is updated when a server process attempts to get a latch, and the latch is unavailable on the first attempt.


What are the Most Popular latch wait event ??

Ø  1. Latch: library cache or Latch: shared pool

Below is Possible causes for above both latch events.

1. Underlying object structure being modified (for example:  truncate)
2. Cursors are closed explicitly after each execution
3. Insufficient size of application cursor cache
4. Statements not using any bind variables
5. Lack of statement reuse
6.Frequent logon/logoffs
7.If  Shared pool is too small

What are Possible suggestion for avoid above both latch events.. ??

1. To Increase SHARED_POOL_SIZE parameter value.
2. Modify Front-end application to use BIND VARIABLE
3. Use CURSOR_SHARING='force' parameter (for temporary basis)


Ø  2. Latch: cache buffers lru chain

Possible Causes

1. Inefficient SQL that accesses incorrect indexes iteratively (large index range scans) or many full table scans.
2. If DBWR not keeping up with the dirty workload; Hence, foreground will process spend longer holding the latch looking for a free buffer
3. Cache may be too small


Possible Suggestion

1. Increase DB_CACHE_SIZE parameter value.
2. Look for: Statements with very high logical I/O or physical I/O, using unselective indexes
 3. The cache buffers LRU - chain latches protect the lists of buffers in the cache.
When adding, moving, or removing a buffer from a list, a latch must be obtained.

-          For symmetric multiprocessor (SMP) systems, Oracle automatically sets the number of LRU latches to a value equal to one half the number of CPUs on the system.
-          For non-SMP systems, one LRU latch is sufficient.

Contention for the LRU latch can impede performance on SMP machines with a large number of CPUs. LRU latch contention is detected by querying V$LATCH, V$SESSION_EVENT, and V$SYSTEM_EVENT.

To avoid contention, consider tuning the application, bypassing the buffer cache for DSS jobs, or redesigning the application.


Latch: cache buffers chains

Possible Causes

1. Repeated access to a block (or small number of blocks), known as a hot block
2. From AskTom:

Contention for these latches can be caused by:

- Very long buffer chains.
- very very heavy access to the same blocks.

Possible Suggestion

1. From AskTom:
When I see this, I try to see what SQL the waiters are trying to execute. Many times,
what I find, is they are all running the same query for the same data (hot blocks). If
you find such a query -- typically it indicates a query that might need to be tuned (to
access less blocks hence avoiding the collisions).

If it is long buffer chains, you can use multiple buffer pools to spread things out. You
can use DB_BLOCK_LRU_LATCHES to increase the number of latches. You can use both
together.

The cache buffers chains latches are used to protect a buffer list in the buffer cache. These latches are used when searching for, adding, or removing a buffer from the buffer cache. Contention on this latch usually means that there is a block that is greatly contended for (known as a hot block).

To identify the heavily accessed buffer chain, and hence the contended for block, look at latch statistics for the cache buffers chains latches using the view V$LATCH_CHILDREN. If there is a specific cache buffers chains child latch that has many more GETS, MISSES, and SLEEPS when compared with the other child latches, then this is the contended for child latch.

This latch has a memory address, identified by the ADDR column. Use the value in the ADDR column joined with the X$BH table to identify the blocks protected by this latch. For example, given the address (V$LATCH_CHILDREN.ADDR) of a heavily contended latch, this queries the file and block numbers:

SELECT OBJ data_object_id, FILE#, DBABLK,CLASS, STATE, TCH
FROM X$BH
WHERE HLADDR = 'address of latch'
ORDER BY TCH;

X$BH.TCH is a touch count for the buffer. A high value for X$BH.TCH indicates a hot block.

Many blocks are protected by each latch. One of these buffers will probably be the hot block. Any block with a high TCH value is a potential hot block. Perform this query a number of times, and identify the block that consistently appears in the output. After you have identified the hot block, query DBA_EXTENTS using the file number and block number, to identify the segment.

After you have identified the hot block, you can identify the segment it belongs to with the following query:

SELECT OBJECT_NAME, SUBOBJECT_NAME
FROM DBA_OBJECTS
WHERE DATA_OBJECT_ID = &obj;

In the query, &obj is the value of the OBJ column in the previous query on X$BH.


5. Latch: row cache objects

The row cache objects latches protect the data dictionary.

Suggestion: Increase SHARED_POOL_SIZE parameter to avoid this latch

Friday 27 May 2016

How to Rename a database


Today I'm going to show you, How To Rename The Database Name Orcl To Test , manually . .


Follow the procedure to Rename your database

In my Case,

OldName:       ORCL
NewName:     TEST


1.   Take a Bakup of your ControlFile, because we have to make changes in controlfile.

 SQL> Alter database backup controlfile to trace.  
 OR  
 SQL> Alter database backup controlfile to trace as ‘C:\CONTROLFILEBACKUP.SQL’;  


2. Shutdown  the database

 SQL> Shutdown immediate.  


3. Open the parameter file  ( pfile ) and change the parameter  “db_name=test “

4. Delete the existing controfiles.

5. startup your database in nomount mode using following command (using modified pfile)

 SQL> startup nomount pfile= C:\oracle\product\10.1.0\admin\orcl\pfile\init.ora._________ ';  


6. After Nomount stage, Its time to Create control file now.
Type the below statements, this creates controlfile at specified Location.

 SQL> CREATE CONTROLFILE SET DATABASE "test" RESETLOGS NOARCHIVELOG  
 MAXLOGFILES 50  
 MAXLOGMEMBERS 3  
 MAXDATAFILES 300  
 MAXINSTANCES 8  
 MAXLOGHISTORY 500  
 LOGFILE  
 GROUP 1 'C:\oracle\product\10.1.0\oradata\orcl\REDO01.LOG' SIZE 10M,  
 GROUP 2 'C:\oracle\product\10.1.0\oradata\orcl\REDO02.LOG' SIZE 10M,  
 GROUP 3 'C:\oracle\product\10.1.0\oradata\orcl\REDO03.LOG' SIZE 10M  
 -- STANDBY LOGFILE  
 DATAFILE  
 'C:\oracle\product\10.1.0\oradata\orcl\SYSTEM01.DBF',  
 'C:\oracle\product\10.1.0\oradata\orcl\SYSAUX01.DBF',  
 'C:\oracle\product\10.1.0\oradata\orcl\UNDOTBS01.DBF',  
 'C:\oracle\product\10.1.0\oradata\orcl\USERS01.DBF'  
 CHARACTER SET WE8MSWIN1252;  


7.  open your database using below command


 SQL > Alter database open resetlogs;  

8.  Check your database Name 


 SQL >Select name from v$database;  

Thursday 26 May 2016

Some Useful Scripts on Session



Find Active Sessions in Oracle Database

SELECT SID, Serial#, UserName, Status, SchemaName, Logon_Time
FROM V$Session
WHERE
Status=‘ACTIVE’ AND
UserName IS NOT NULL;


How to check the maximum number of allowed connections to an Oracle database?

The number of sessions the database was configured to allow
SELECT name, value
 FROM v$parameter
 WHERE name = 'sessions'
The number of sessions currently active
SELECT COUNT(*)
 FROM v$session


Identify the session which is locking the object
select a.sid, a.serial#
from v$session a, v$locked_object b, dba_objects c
where b.object_id = c.object_id
and a.sid = b.session_id
and OBJECT_NAME='EMP';


Oracle: Total Size of The Database

An oracle database consists of data files, redo log files, control files, temporary files.
The size of the database actually means the total size of all these files.

select
( select sum(bytes)/1024/1024/1024 data_size from dba_data_files ) +
( select nvl(sum(bytes),0)/1024/1024/1024 temp_size from dba_temp_files ) +
( select sum(bytes)/1024/1024/1024 redo_size from sys.v_$log ) +
( select sum(BLOCK_SIZE*FILE_SIZE_BLKS)/1024/1024/1024 controlfile_size from v$controlfile) "Size in GB"
from
dual ;



Interview Questions on Architecture for 1+ Experienced


What are 11g New Features 
1) Automatic Diagnostic Repository [ADR]
2) Database Replay
3) Automatic Memory Tuning
4) Case sensitive password
5) Virtual columns and indexes
6) Interval Partition and System Partition
7) The Result Cache
8) ADDM RAC Enhancements
9) SQL Plan Management and SQL Plan Baselines
10) SQL Access Advisor & Partition Advisor
11) SQL Query Repair Advisor
12) SQL Performance Analyzer (SPA) New
13) DBMS_STATS Enhancements
14) The Result Cache
15) Total Recall (Flashback Data Archive)
Note: The above are only top new features, there are other features as well introduced in 11g which will be included subsequently

11G Background Processes?

The following process are added in 11g as new background processes.
1 dbrm DB resource manager
2 dia0 Diagnosability process
3 fbda Flashback data archiver process
4 vktm Virtual Timekeeper
5 w000 Space Management Co-ordination process
6 smc0 Space Manager process
NOTE : The above six are mandatory processes.


DBWn writes when __ ?
• Checkpoint occurs
• Dirty buffers reach threshold
• There are no free buffers
• Timeout occurs
• RAC ping request is made
• Tablespace OFFLINE
• Tablespace READ ONLY
• Table DROP or TRUNCATE
• Tablespace BEGIN BACKUP
Log Writer (LGWR) writes:
• At commit
• When 1/3rd full
• When there is 1 MB of redo
• Every 3 seconds
• Before DBWn writes


What are the Responsibilities of System Monitor (SMON) ?
• Instance recovery
– Rolls forward changes in redo logs
– Opens database for user access
– Rolls back uncommitted transactions
• Coalesces free space
• Deallocates temporary segments.
Process Monitor (PMON) Cleans up after failed processes by:
• Rolling back the transaction
• Releasing locks
• Releasing other resources
• Restarting dead dispatchers
Checkpoint (CKPT) Responsible for:
• Signaling DBWn at checkpoints
• Updating datafile headers with checkpoint information
• Updating control files with checkpoint information
Archiver (ARCn)
• Optional background process
• Automatically archives online redo logs when ARCHIVELOG mode is set
• Preserves the record of all changes made to the database


Why should we use separate ASM home?
ASM should be installed separately from the database software in its own ORACLE_HOME directory. This will allow you the flexibility to patch and upgrade ASM and the database software independently.

How many ASM instances should one have?
Several databases can share a single ASM instance. So, although one can create multiple ASM instances on a single system, normal configurations should have one and only one ASM instance per system.

What is ASM Rebalancing?

The rebalancing speed is controlled by the ASM_POWER_LIMIT initialization parameter. Setting it to 0 will disable disk rebalancing

Monday 23 May 2016

Findiing & Removing (Deleting) Files by 'n' Dates in LINUX



Finding & Removing (Deleting) Files by 'n' Dates in LINUX

There are three times associated with a file

·         atime  - last access time
   
·         ctime  - last status change time

·         mtime  - last modify time

Remove all files from /home/dpafumi older than 5 days:
$ find /home/dpafumi -type f -mtime +5 -exec rm -f {} \;

Print out what files from /home/dpafumi older than 5 days:
$ find /home/dpafumi -type f -ctime +5 -print

Finding the top 5 largest files
$ find . -ls | sort -nrk 7 | head -5

Find files larger than 100MB
$ find . -size +100000k

Delete audit records that’s older than 30 days
$ find $ORACLE_HOME/rdbms/audit -name "*.aud" -mtime +30 -exec rm {} \;

Delete files in /tmp that’s older than 30 days
$ find /tmp -group dba -type f -mtime +5 -exec rm -f {} \; find /tmp/dba -group dba -type f -mtime +5 -exec rm -f {} \;

Delete *.trc files more than 5 days old
$ find $TRACE_DIR -name '*.trc' -type f -mtime +5 -exec rm {} \;

Display top 5 largest files in a current directory
$ ls -alS | head -5

Largest space-consuming directories (Does not include sub-directories)
$ du -Sm . | sort -nr | head -5

Report the sum of space consumed by a directory and its subdirectories
$ du . | sort -nr | head -5

List files in a directory greater than 300M
$ ls -l | awk '{if ($5 >314572800) print $0}' |sort +4