Tuesday, March 10, 2015

Resize datafiles to minimum



column value new_val blksize
select value from v$parameter where name = 'db_block_size'
/

select file_name,
       ceil( (nvl(hwm,1)*&&blksize)/1024/1024 ) smallest,
       ceil( blocks*&&blksize/1024/1024) currsize,
       ceil( blocks*&&blksize/1024/1024) -
       ceil( (nvl(hwm,1)*&&blksize)/1024/1024 ) savings
from dba_data_files a,
     ( select file_id, max(block_id+blocks-1) hwm
         from dba_extents
        group by file_id ) b
where a.file_id = b.file_id(+)
/

column cmd format a75 word_wrapped

select 'alter database datafile '''||file_name||''' resize ' ||
       ceil( (nvl(hwm,1)*8)/1024/1024 )  || 'm;' cmd
from dba_data_files a,
     ( select file_id, max(block_id+blocks-1) hwm
         from dba_extents
        group by file_id ) b
where a.file_id = b.file_id(+)
  and ceil( blocks*&&blksize/1024/1024) -
      ceil( (nvl(hwm,1)*&&blksize)/1024/1024 ) > 0
/

Tuesday, March 3, 2015

Oracle Database Size

Oracle Database Size

SET FEEDBACK OFF
SET HEADING OFF
SET AUTOPRINT OFF
–SET NOTIFY OFF
SET VERIFY OFF
PROMPT ……………………………………………………..
SELECT    ‘** TableSpace Usage Snap For ‘
|| NAME
|| ‘ Instance Taken On ‘
|| RTRIM (TO_CHAR (SYSDATE, ‘Day’))
|| ‘ ‘
|| TO_CHAR (SYSDATE, ‘DD-MON-YYYY HH24:MI:SS’)
|| ‘ ** ‘
FROM   v$database;
COMPUTE SUM LABEL ‘Total’ OF Tot_Spc_Alloc ON report
BREAK ON REPORT
COLUMN TSname        Format A25               Heading ‘Tablespace Name’
COLUMN TSid          Format           999     Heading ‘Tablespace#’
COLUMN Tot_Spc_Alloc Format 9,999,999,990.00  Heading ‘Total Space|Allocated’
COLUMN Tot_Spc_Free  Format   999,999,990.00  Heading ‘Total Free|Space’
COLUMN Tot_Spc_Used  Format   999,999,990.00  Heading ‘Total Space|Used’
COLUMN Pct_Used      Format           990.000 Heading ‘Percent|Used’
COLUMN Pct_Free      Format           990.000 Heading ‘Percent|Free’
COLUMN FileName      Format A65               Heading ‘File Name’
COLUMN fileid        Format 99999             Heading ‘File|ID’
SET LINESIZE 10000
SET HEADING ON
SELECT tsid, tsname, pct_used, pct_free, tot_spc_alloc, tot_spc_used, tot_spc_free
FROM   (SELECT   c.ts# tsid,
a.tsname,
(1 – (b.tot_spc_free / a.tot_spc_alloc)) * 100 pct_used,
(b.tot_spc_free / a.tot_spc_alloc) * 100 pct_free,
a.tot_spc_alloc / 1024 / 1024 tot_spc_alloc,
(a.tot_spc_alloc – b.tot_spc_free) / 1024 / 1024 tot_spc_used,
b.tot_spc_free / 1024 / 1024 tot_spc_free
FROM     (SELECT   tablespace_name tsname, SUM (BYTES) tot_spc_alloc
FROM     DBA_DATA_FILES
GROUP BY tablespace_name) a,
(SELECT   tablespace_name tsname, SUM (BYTES) tot_spc_free, MAX (BYTES) max_b2
FROM     DBA_FREE_SPACE
GROUP BY tablespace_name) b,
v$tablespace c
WHERE    a.tsname = b.tsname AND a.tsname = c.NAME
ORDER BY c.ts#);

Oracle 11g Active Sessions


Oracle 11g Sessions currently active


select   A.inst_id “Instance”,A.sid “Sid”,A.serial# “Serial No.”,p.spid “ospid”,a.schemaname “schemaname”,
        A.machine “Machine”,A.terminal “Terminal”,  A.program “Program”,
      a.event “event”,a.seconds_in_wait “seconds_in_wait”,
       a.blocking_session “blocking_session”, A.osuser ,    
       sw.p1, sw.p2, sw.p3,sw.event,
       B.sql_id “SQL ID”,
       B.sql_text “Query”,
       B.users_executing “Users” ,C.opname “Operation”,C.target “Target”,
       C.sofar “Till Now”,C.totalwork “Total Work”,C.Time_Remaining “Remaining Time”,
       C.elapsed_seconds “Time Taken”,
       C.Message “Message”,A.username “User”,A.status “Status” , sysdate “Time”,
       su.tablespace, su.contents, su.extents, su.blocks
       from   gv$session A,gv$sql B,gv$session_longops C,gv$process P ,gv$session_wait sw,v$sort_usage su
where  A.sql_address = B.address
and    a.PADDR = p.addr
and    B.users_executing > 0
and    C.inst_id(+) = A.inst_id
and    C.sid(+) = A.sid
and    C.serial#(+) = A.serial#
and    C.sql_address(+) = A.sql_address
and     sw.sid(+)= a.sid
and    a.saddr=su.session_addr
–and C.Time_Remaining >0 —-
–and a.schemaname = ‘XXX’
–and a.sid=3835;


select * from dba_hist_active_sess_history
where sample_time >sysdate-5/24;


SELECT * FROM TABLE(dbms_xplan.display_cursor(‘0txzuvqj2ujux’));
select * from table(dbms_xplan.display_awr(‘3dd7k4gars656′));
SELECT * FROM table(DBMS_XPLAN.DISPLAY_AWR(‘3dd7k4gars656′,null,null,’ADVANCED’));
select * from table( DBMS_XPLAN.display_cursor(‘3dd7k4gars656′, NULL,’ADVANCED ROWS ALLSTATS’));
 select
count(1),to_char(sample_time, ‘YYYY-MM-DD HH24′) sample_time,
sql_id, sql_plan_hash_value
from dba_hist_active_sess_history
where
sql_id = ‘0txzuvqj2ujux’
group by
to_char(sample_time, ‘YYYY-MM-DD HH24′),
sql_id, sql_plan_hash_value
order by sample_time;

SELECT sql_id,plan_hash_value,
SUM(elapsed_time_total)/SUM(executions_total)/1000000 avg_elapsedt_secs
FROM dba_hist_sqlstat
WHERE sql_id = ‘0txzuvqj2ujux’
GROUP BY sql_id,plan_hash_value ;

——-
——- TOP SQL from dba_hist_active_sess_history no v$active_session_history   filter by DBID
——-
select * from (select     ash.SQL_ID , ash.SQL_PLAN_HASH_VALUE Plan_hash, aud.name type,    
 sum(decode(ash.session_state,’ON CPU’,1,0))     “CPU”,   
   sum(decode(ash.session_state,’WAITING’,1,0))    –     sum(decode(ash.session_state,’WAITING’, 
   decode(wait_class, ‘User I/O’,1,0),0))    “WAIT” ,     sum(decode(ash.session_state,’WAITING’, 
   decode(wait_class, ‘User I/O’,1,0),0))    “IO” ,     sum(decode(ash.session_state,’ON CPU’,1,1))     “TOTAL”
   from dba_hist_active_sess_history ash,     audit_actions aud where SQL_ID is not NULL   — and ash.dbid=&DBID   
and ash.sql_opcode=aud.action   — and ash.sample_time > sysdate – &minutes /( 60*24)
group by sql_id, SQL_PLAN_HASH_VALUE   , aud.name order by sum(decode(session_state,’ON CPU’,1,1))   desc
) where  rownum < 10

Lockers, Blockers

Lockers, Blockers


select s1.username || '@' || s1.machine || ' ( SID=' || s1.sid ||
' )  is blocking ' || s2.username || '@' || s2.machine || ' ( SID=' ||
s2.sid || ' ) ' AS blocking_status
from v$lock l1, v$session s1, v$lock l2, v$session s2
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;
select decode(request, 0, 'Holder: ', 'Waiter: ') || sid sess,
id1,
id2,
lmode,
request,
type
from v$lock
where (id1, id2, type) in
(select id1, id2, type from v$lock where request > 0)
order by id1, request;
select blocking_session, sid, wait_class, sql_id, seconds_in_wait
from v$session
where blocking_session is not NULL
order by blocking_session;
--To find the most important wait events in the last 15 minutes, issue the following query:
select session_id,
sql_id,
event,
sum(wait_time + time_waited) total_wait_time
from v$active_session_history
where sample_time between sysdate - 30 / 2880 and sysdate
group by event, session_id, sql_id
order by total_wait_time desc;
--You can identify the SQL statements that have been waiting the most during the last 15 minutes with this query.
select a.user_id,
u.username,
s.sql_text,
s.sql_id,
sum(a.wait_time + a.time_waited) total_wait_time
from v$active_session_history a, v$sqlarea s, dba_users u
where a.sample_time between sysdate - 30 / 2880 and sysdate
and a.sql_id = s.sql_id
and a.user_id = u.user_id
group by a.user_id, s.sql_text, u.username, s.sql_id
order by total_wait_time desc;



set echo off
col sid form 9999
col id1 form 9999999999
col id2 form 999999999
col lmode    head "Lock Held" form a14
col request1 head "Lock Request" form a16
col type     head "Lock Type" form a15
col ctime    head "Time|Held" form 999999
col block head "No Of |Sessions|Waiting|For This|Lock" form 99999
---Check for Lock type, mode etc.
select sid,
DECODE(TYPE,
'BL','Buffer hash table',
'CF','Control File Transaction',
'CI','Cross Instance Call',
'CS','Control File Schema',
'CU','Bind Enqueue',
'DF','Data File',
'DL','Direct-loader index-creation',
'DM','Mount/startup db primary/secondary instance',
'DR','Distributed Recovery Process',
'DX','Distributed Transaction Entry',
'FI','SGA Open-File Information',
'FS','File Set',
'IN','Instance Number',
'IR','Instance Recovery Serialization',
'IS','Instance State',
'IV','Library Cache InValidation',
'JQ','Job Queue',
'KK','Redo Log "Kick"',
'LS','Log Start/Log Switch',
'MB','Master Buffer hash table',
'MM','Mount Definition',
'MR','Media Recovery',
'PF','Password File',
'PI','Parallel Slaves',
'PR','Process Startup',
'PS','Parallel Slaves Synchronization',
'RE','USE_ROW_ENQUEUE Enforcement',
'RT','Redo Thread',
'RW','Row Wait',
'SC','System Commit Number',
'SH','System Commit Number HWM',
'SM','SMON',
'SQ','Sequence Number',
'SR','Synchronized Replication',
'SS','Sort Segment',
'ST','Space Transaction',
'SV','Sequence Number Value',
'TA','Transaction Recovery',
'TD','DDL enqueue',
'TE','Extend-segment enqueue',
'TM','DML enqueue',
'TS','Temporary Segment',
'TT','Temporary Table',
'TX','Transaction',
'UL','User-defined Lock',
'UN','User Name',
'US','Undo Segment Serialization',
'WL','Being-written redo log instance',
'WS','Write-atomic-log-switch global enqueue',
'XA','Instance Attribute',
'XI','Instance Registration',
decode(substr(TYPE,1,1),
'L','Library Cache ('||substr(TYPE,2,1)||')',
'N','Library Cache Pin ('||substr(TYPE,2,1)||')',
'Q','Row Cache ('||substr(TYPE,2,1)||')',
'????')) TYPE,
id1,id2,
decode(lmode,
0,'None(0)',
1,'Null(1)',
2,'Row Share(2)',
3,'Row Exclu(3)',
4,'Share(4)',
5,'Share Row Ex(5)',
6,'Exclusive(6)') lmode,
decode(request,
0,'None(0)',
1,'Null(1)',
2,'Row Share(2)',
3,'Row Exclu(3)',
4,'Share(4)',
5,'Share Row Ex(5)',
6,'Exclusive(6)') request1,
ctime, block
from
v$lock
where sid>5
and type not in ('MR','RT')
order by decode(request,0,0,2),block,5
/
/

Renaming ASM Disk Groups




Oracle RDBMS-11.2.0.3
Task: rename the ASM disk created by oracleasm utility
Status: ASM is installed, Disks are mounted and no database created that uses that disk
1. Login to Grid user, and connect to asmcmd console.
ASMCMD> umount DATA -f
ASMCMD> umount FRA -f
2. exit from asmcmd console and run command renamedg with grid user:
$ renamedg dgname=DATA newdgname=DATA_DG1 verbose=true
$ renamedg dgname=FRA newdgname=FRA_DG1 verbose=true
3. login to asmcmd and mount new DG
ASMCMD> mount DATA_DG1
ASMCMD> mount FRA_DG1
ASMCMD> lsdg <– run this to confirm the changes
Oracle Doc Reference:

Oracle Datapump import (impdp) via network_link


Oracle Data Pump (IMPDP) via Network link
We commonly use Oracle’s datapump with approach of take expdp dump and do impdp of that dump file.
If there is requirement to run expdp and impdp at same time, we can use impdp feature with option of network_link. The Data pump via network link method is much simpler.
This option uses network link methodology and uses DBLINK and TNS service to fetch the data rows.
Limitations: tables with LONG columns not support this feature.

Step 1: Create user on SOURCE db to use for network link in TARGET database.
CREATE USER TMPDBA IDENTIFIED BY “Password123$”;
GRANT DBA TO TMPDBA;

Step 2: Create Database link on target database:
CREATE DATABASE LINK SERVICE_NAME CONNECT TO TMPDBA  IDENTIFIED BY “Password123$” USING ‘SERVICE_NAME';

Step 3: Copy TNSNAMES entry for source  database to target’s tnsnames.ora file.

Step 4: Create directory in TARGET to use for your logfiles.
CREATE DIRECTORY dumpdir AS ‘/u01/app/oracle/admin/datapump/';
GRANT READ,WRITE ON DIRECTORY dumpdir to PUBLIC;

Step 5: Run import job on TARGET.  You parallelize the job to create multiple datapump slaves..
impdp parfile=impdp.par
 PARFILE contents:
 DIRECTORY=dumpdir
NETWORK_LINK=SERVICE_NAME
SCHEMAS=’TEST’
REMAP_SCHEMA=TEST:TEST1
PARALLEL=4
LOGFILE=impdp_from_service_name.log


Configuring Kernel Parameters for Oracle Installation on Linux x86-64



Configuring Kernel Parameters
Verify that the kernel parameters shown in the following table are set to values greater than or equal to the minimum value shown. The procedure following the table describes how to verify and set the values.
Note:
The kernel parameter and shell limit values in this section are minimum values only. For production database systems, Oracle recommends that you tune these values to optimize the performance of the system. Refer to your operating system documentation for more information about tuning kernel parameters.
Parameter
Minimum Value
File
semmsl
semmns
semopm
semmni
250
32000
100
128
/proc/sys/kernel/sem
shmall
2097152
/proc/sys/kernel/shmall
shmmax
Minimum: 536870912
Maximum: A value that is 1 byte less than the physical memory
Recommended: More than half the physical memory
See My Oracle Support Note 567506.1 for additional information about configuring shmmax.
/proc/sys/kernel/shmmax
shmmni
4096
/proc/sys/kernel/shmmni
file-max
6815744
/proc/sys/fs/file-max
ip_local_port_range
Minimum: 9000
Maximum: 65500
/proc/sys/net/ipv4/ip_local_port_range
rmem_default
262144
/proc/sys/net/core/rmem_default
rmem_max
4194304
/proc/sys/net/core/rmem_max
wmem_default
262144
/proc/sys/net/core/wmem_default
wmem_max
1048576
/proc/sys/net/core/wmem_max
aio-max-nr
Maximum: 1048576
Note: This value limits concurrent outstanding requests and should be set to avoid I/O subsystem failures.
/proc/sys/fs/aio-max-nr
Note:
If the current value of any parameter is higher than the value listed in this table, then do not change the value of that parameter.
To view the current value specified for these kernel parameters, and to change them if necessary:
  • Enter commands similar to the following to view the current values of the kernel parameters:
Note:
Make a note of the current values and identify any values that you must change.
Parameter
Command
semmsl, semmns, semopm, and semmni
# /sbin/sysctl -a | grep sem
This command displays the value of the semaphore parameters in the order listed.
shmall, shmmax, and shmmni
# /sbin/sysctl -a | grep shm
file-max
# /sbin/sysctl -a | grep file-max
ip_local_port_range
# /sbin/sysctl -a | grep ip_local_port_range
rmem_default
# /sbin/sysctl -a | grep rmem_default
rmem_max
# /sbin/sysctl -a | grep rmem_max
wmem_default
# /sbin/sysctl -a | grep wmem_default
wmem_max
# /sbin/sysctl -a | grep wmem_max
  • If the value of any kernel parameter is different from the recommended value, then complete the following steps:
Using any text editor, create or edit the /etc/sysctl.conf file, and add or edit lines similar to the following:
Note:
Include lines only for the kernel parameter values to change. For the semaphore parameters (kernel.sem), you must specify all four values. However, if any of the current values are larger than the minimum value, then specify the larger value.
fs.aio-max-nr = 1048576
fs.file-max = 6815744
kernel.shmall = 2097152
kernel.shmmax = 536870912
kernel.shmmni = 4096
kernel.sem = 250 32000 100 128
net.ipv4.ip_local_port_range = 9000 65500
net.core.rmem_default = 262144
net.core.rmem_max = 4194304
net.core.wmem_default = 262144
net.core.wmem_max = 1048586

By specifying the values in the /etc/sysctl.conf file, they persist when you restart the system. However, on SUSE Linux Enterprise Server systems, enter the following command to ensure that the system reads the /etc/sysctl.conf file when it restarts:
# /sbin/chkconfig boot.sysctl on
Enter the following command to change the current values of the kernel parameters:
# /sbin/sysctl -p
Review the output from this command to verify that the values are correct. If the values are incorrect, edit the /etc/sysctl.conf file, then enter this command again.
Enter the command /sbin/sysctl -a to confirm that the values are set correctly.
On SUSE systems only, enter the following command to cause the system to read the /etc/sysctl.conffile when it restarts:
# /sbin/chkconfig boot.sysctl on
On SUSE systems only, you must enter the GID of the oinstall group as the value for the parameter /proc/sys/vm/hugetlb_shm_group. Doing this grants members of oinstall a group permission to create shared memory segments.
For example, where the oinstall group GID is 501:
# echo 501 > /proc/sys/vm/hugetlb_shm_group
After running this command, use vi to add the following text to /etc/sysctl.conf, and enable the boot.sysctl script to run on system restart:
vm.hugetlb_shm_group=501
Note:
Only one group can be defined as the vm.hugetlb_shm_group.
After updating the values of kernel parameters in the /etc/sysctl.conf file, either restart the computer, or run the command sysctl -p to make the changes in the /etc/sysctl.conf file available in the active kernel memory.
Check Resource Limits for the Oracle Software Installation Users
For each installation software owner, check the resource limits for installation, using the following recommended ranges:
Table 1 Installation Owner Resource Limit Recommended Ranges
Resource Shell Limit
Resource
Soft Limit
Hard Limit
Open file descriptors
nofile
at least 1024
at least 65536
Number of processes available to a single user
nproc
at least 2047
at least 16384
Size of the stack segment of the process
stack
at least 10240 KB
at least 10240 KB, and at most 32768 KB
To check resource limits:
Log in as an installation owner.
Check the soft and hard limits for the file descriptor setting. Ensure that the result is in the recommended range. For example:
$ ulimit -Sn
4096
$ ulimit -Hn
65536
Check the soft and hard limits for the number of processes available to a user. Ensure that the result is in the recommended range. For example:
$ ulimit -Su
2047
$ ulimit -Hu
16384

Check the soft limit for the stack setting. Ensure that the result is in the recommended range. For example:
$ ulimit -Ss
10240
$ ulimit -Hs
32768
Repeat this procedure for each Oracle software installation owner.
If necessary, update the resource limits in the /etc/security/limits.conf configuration file for the installation owner. For example, add the following lines to the /etc/security/limits.conf file:
oracle              soft    nproc   2047
oracle              hard    nproc   16384
oracle              soft    nofile  1024
oracle              hard    nofile  65536
oracle              soft    stack   10240
Note:
  • The values mentioned in this example are illustrative and not actual values that must be added.
  • When the limits.conf file is changed, these changes take effect immediately. However, if the grid or oracle users are logged in, then these changes do not take effect until you log these users out and log them back in. You must do this before you attempt to use these accounts to install.