Oracle DBA Queries
Oracle DBA Queries
TABLES INFORMATION
Data Dictionary Tables and Views
All the table and column information are stored in SYS.TAB$ and SYS.COL$ tables.
Oracle has provided data dictionary views to get the information about table and
columns
desc <table_name>
How to determine Table SIZE?
select
owner as "Schema"
, segment_name as "Object Name"
, segment_type as "Object Type"
, round(bytes/1024/1024,2) as "Object Size (Mb)"
, tablespace_name as "Tablespace"
from dba_segments
where segment_name=’<table_name>’;
INDEX INFORMATION
Data dictionary views on Indexes
VIEW INFORMATION
Dictionary Views for seeing the View data
View details can be queried from the dictionary by querying either USER_VIEWS,
ALL_VIEWS or DBA_VIEWS. Views are useful for security and information hiding,
but can cause problems if nested too deeply. Some of the advantages of using
views:
desc <view_name>
Syntax:
SQL> set long 10000
How to extract the view definition (DDL statements) from an Oracle database
without having to go through a stack of dictionary views
Syntax:
SQL> set long 1000
SQL> set pagesize 0
SEQUENCE INFORMATION
select sequence_name,min_value,max_value,increment_by,last_number
FROM DBA_SEQUENCES
where OWNER = '<owner_name>'
and sequence_NAME = '<sequence_name>';
The last_number column display the next available sequence number if no cache is
specified
Syntax:
SQL> set long 1000
SQL> set pagesize 0
Sequence are cached with the purpose of improving the fetch performance. In
RAC,each instance stores the cache values
We can have gaps in sequence when using cache due to following reasons
a) Rollback occurs
b) System crash or instance crash
c) Sequence is used in another table
PRIVILEGES INFORMATION
Oracle has provided data dictionary views to get the information about privileges
Checking which column level privileges are granted by you to other users.
Checking which column level privileges are granted to you by other users
SELECT * FROM USER_COL_PRIVS_RECD;
TABLESPACE INFORMATION
View Description
To list the names and various other of all tablespaces in a database, use the
following query on the DBA_TABLESPACES view:
To list the names, sizes, and associated tablespaces of a database, enter the
following query on the DBA_DATA_FILES view
To produce statistics about free extents and coalescing activity for each tablespace
in the database, enter the following query:
select a.tablespace_name
,a.file_name
,(b.maximum+c.blocks-1)*d.db_block_size highwater
from dba_data_files a
,(select file_id,max(block_id) maximum
from dba_extents
group by file_id) b
,dba_extents c
,(select value db_block_size
from v$parameter
where name='db_block_size') d
where a.file_id = b.file_id
and c.file_id = b.file_id
and c.block_id = b.maximum
order by a.tablespace_name,a.file_name
/
To check the free SPACE, largest free chunck and no of free chunck in tablespace.
select tbs.tablespace_name
, tbs.initial_extent
, tbs.next_extent
--, tbs.min_extents
, decode(tbs.max_extents,2147483645,'UL',tbs.max_extents) max_extents
, tbs.pct_increase
, tbs.extent_management
, tbs.allocation_type
, tbs.status
from dba_tablespaces tbs
order by 1
/
We can use below query to obtain all the enqueue in the system
Select * from v$session_wait where event like ‘enq%’;
i) V$lock list all the lock structure currently held in the system
ii) The column type ,id1 and id2 represent the resource type ,id1
and id2 of the resource structure.so it can be joined with
V$resource which contains the list of all the resource structure
>0 =0 Owner
=0 >0 Waiter
In case of RAC
d) There are several views which can be used to find the locks
information. These views are created by catblock.sql
select
waiting_session,
holding_session,
lock_type,
mode_held,
mode_requested,
lock_id1,
lock_id2
from
dba_waiters
/
To find the holder of the CF enqueue, the following query can be used :
To find the session waiting to get the CF enqueue, the following query can be used
:
SESSION QUERIES
How to kill multiple user session using some condition?
where <condition>;
Example
This SQL script creates sql files for begin and end tablespace backup
cat ts_back.sql
set pagesize 0 feed off echo off termout off
spool /home/oracle/create/begin_backup.sql
select 'set echo on verify on feed on termout on trimspool on' from dual
/
select 'column NAME format A31' from dual
/
select 'alter system switch logfile;' from dual
/
select 'select SEQUENCE#,ARCHIVED,STATUS from v$log where STATUS =
''ACTIVE'';' from dual
/
select 'alter tablespace '||tablespace_name||' begin backup;' from
dba_tablespaces
/
select 'exit' from dual
/
spool off
spool /home/oracle/create/end_backup.sql
select 'set echo on verify on feed on termout on' from dual
/
select 'alter tablespace '||tablespace_name||' end backup;' from dba_tablespaces
/
select 'alter system switch logfile;' from dual
/
select 'select NAME,RECID from v$archived_log where RECID=(select
SEQUENCE#-1 from v$log where STATUS = ''ACTIVE'');' from dual
/
select 'exit' from dual
/
spool off
set termout on lines 180
select 'Output files are in /home/oracle/create/begin_backup.sql and
/home/oracle/create/end_backup.sql' from dual
/
select ' ' from dual
/
exit
/
select (
(1- (sum(decode(name,'physical reads',value,0))/
(sum(decode(name,'db block gets',value,0))
+ (sum(decode(name,'consistent gets',value,0))
))))*100) "Buffer Hit Ratio"
from v$sysstat
/
select s.sid,s.username,s.osuser,t.UBABLK,t.USED_UBLK,
s.terminal,s.status,t.start_time,
s.type,s.program
from v$session s,v$transaction t
where s.taddr=t.addr
order by t.start_time
/
select do.OWNER,do.OBJECT_NAME,OBJECT_TYPE,
decode (OBJECT_TYPE,'TABLE' , (Select LAST_ANALYZED from dba_tables where
owner=do.owner and TABLE_NAME=do.object_name) ,
'INDEX' , (Select LAST_ANALYZED from dba_indexes where
owner=do.owner and INDEX_NAME=do.object_name) ,
'UNKNOWN') "LAST ANALYZED",STATUS
from DBA_OBJECTS do
where OBJECT_TYPE in ('TABLE','INDEX')
and (OWNER,OBJECT_NAME) in (select OBJECT_OWNER,OBJECT_NAME from
V$SQL_PLAN where HASH_VALUE=&1)
/
select EXECUTIONS,DISK_READS,BUFFER_GETS,
CPU_TIME,ELAPSED_TIME,ROWS_PROCESSED,INVALIDATIONS,PARSE_CALLS
from v$sql
where HASH_VALUE= &1
/