Beruflich Dokumente
Kultur Dokumente
STATUS
Startup time
select to_char(startup_time, 'HH24:MI DD-MON-YY') "Startup time"
from
v$instance
/How large is the database
col "Database Size" format a20
col "Free space" format a20
col "Used space" format a20
select round(sum(used.bytes) / 1024 / 1024 / 1024 ) || ' GB' "Database Size"
,
round(sum(used.bytes) / 1024 / 1024 / 1024 ) -
round(free.p / 1024 / 1024 / 1024) || ' GB' "Used space"
,
round(free.p / 1024 / 1024 / 1024) || ' GB' "Free space"
from
(selectbytes from v$datafile
union all
select bytes from v$tempfile
union all
select bytes from v$log) used,
(select sum(bytes) as p from dba_free_space) free
group by free.p
/
Distribution of objects and data
Which schemas are taking up all of the space
set pages 999
col "size MB" format 999,999,999
col "Objects" format 999,999,999
select obj.owner "Owner",obj_cnt "Objects",
decode(seg_size, NULL, 0, seg_size) "size MB"
from
(select owner, count(*) obj_cnt
from dba_objects group by owner) obj,
(select owner, ceil(sum(bytes)/1024/1024) seg_size
from dba_segments group by owner) seg
where
obj.owner = seg.owner(+)
order
by 3 desc ,2 desc, 1
/
SESSION
Show all connected users
set lines 100 pages 999
col ID format a15
select username,sid || ',' || serial# "ID",status,
last_call_et "Last Activity"
from
v$session
where username is not null
order by status desc,
last_call_et desc
/Time since last user activity
set lines 100 pages 999
select username
,
floor(last_call_et / 60) "Minutes"
,
status
from
v$session
where username is not null
order by last_call_et
/Sessions sorted by logon time
set lines 100 pages 999
col ID
format a15
col osuser
format a15 col login_time format a14 select username
,
osuser
,
sid || ',' || serial# "ID"
,
status
,
to_char(logon_time, 'hh24:mi dd/mm/yy') login_time
,
last_call_et
from
v$session
where
username is not null
order
by login_time
/Show user info including os pid
col "SID/SERIAL" format a10
col username format a15
col osuser format a15
col program format a40
select s.sid || ',' || s.serial# "SID/SERIAL"
,
s.username
,
s.osuser
,
p.spid "OS PID"
,
s.program
from
v$session s
,
v$process p
Where
s.paddr = p.addr
order
by to_number(p.spid)
/Show a users current sql
Select sql_text
from
v$sqlarea
where (address, hash_value) in
(select sql_address, sql_hash_value
from v$session
where username like '&username')
/Session status associated with the specified os process id
select s.username
,
s.sid
,
s.serial#
,
p.spid
,
last_call_et
,
status
from
V$SESSION s
,
V$PROCESS p
where
s.PADDR = p.ADDR
and
p.spid='&pid'
/All active sql
set feedback off
set serveroutput on size9999
column username format a20
column sql_text format a55 word_wrapped
begin
for x in
(select username||'('||sid||','||serial#||') ospid = '|| process ||
' program = ' || program username,
to_char(LOGON_TIME,' Day HH24:MI') logon_time,
to_char(sysdate,' Day HH24:MI') current_time,
sql_address,
sql_hash_value
from v$session
where status = 'ACTIVE'
and rawtohex(sql_address) <> '00'
and username is not null ) loop
for y in (select sql_text
from v$sqlarea
where address = x.sql_address ) loop
if ( y.sql_text not like '%listener.get_cmd%' and
y.sql_text not like '%RAWTOHEX(SQL_ADDRESS)%' ) then
dbms_output.put_line( '--------------------' );
dbms_output.put_line( x.username );
dbms_output.put_line( x.logon_time || ' ' || x.current_time || ' SQL#=' ||
x.sql_hash_value);
dbms_output.put_line( substr( y.sql_text, 1, 250 ) );
end if;
end loop;
end loop;
end;
/Display any long operations
set lines 100 pages 999
col username format a15
col message format a40
col remaining format9999
select username
,
to_char(start_time, 'hh24:mi:ss dd/mm/yy') started
,
time_remaining remaining
,
message
from
v$session_longops
where
time_remaining = 0
order by time_remaining desc
/List open cursors per user
set pages 999
select sess.username
,
sess.sid
,
sess.serial#
,
stat.value cursors
from
v$sesstat stat
,
v$statname sn
,
v$session sess
where
sess.username is not null
and
sess.sid = stat.sid
and
stat.statistic# = sn.statistic#
and
sn.name = 'opened cursors current'
order by value
/Or alternatively...
set lines 100 pages 999
select count(hash_value) cursors
,
sid
,
user_name
from
v$open_cursor
group bysid
,
user_name
order bycursors
/
INIT PARAMETERS
Show latent parameter changes
select name
,
value
from
v$parameter
where ismodified != 'FALSE'
/Show non-default parameters
set pages 999 lines 100
col name format a30
col value format a50
select name
,
value
from
v$parameter
where
isdefault = 'FALSE'
and
value is not null
order
by name
/or for name='parameter' style...
set pages 999 lines 100
select name || '=' || decode(type, 2,'''') || value
|| decode(type, 2,'''') parameter
from
v$parameter
where
isdefault = 'FALSE'
and
value is not null
order
by name
/Reset/Unset a spfile parameter
Setting a parameter to =' ' often isn't enough. Do this instead...
alter system reset <parameter> scope=spfile sid='*'
/The sid='*' bit is always necessary, even in non RAC database.
USER
List users
set pages 999 lines 100
col username
format a20
col status
format a8
col tablespace format a20
col temp_ts
format a20
select username
,
account_status status
,
created
,
default_tablespace tablespace
,
temporary_tablespace temp_ts
from
dba_users
order
by username
/Lock or unlock a user
alter user <username> account lock;
And to unlock...
alter user <username> account unlock;
ROLE
Find a role
select *
from
dba_roles
where
role like '&role'
/Show what roles are granted to a user
select grantee
,
granted_role
,
admin_option
from
dba_role_privs
where
grantee like upper('&username')
/Show what system privileges are granted to a role
select privilege
,
admin_option
from
role_sys_privs
where
role like '&role'
/Show what table privileges are granted to a role
select owner || '.' || table_name "TABLE"
,
column_name
,
privilege
,
grantable
from
role_tab_privs
where
role like '&role'
/Administration - ASM
•
Check if css is running/working
•
Display disk-groups
•
Show disk space usage
•
Create a disk group
•
Add a disk to a group
•
Drop a disk group
•
Is ASM performing a balancing operation
•
Check the internal consistency of a diskgroup
Check if css is running/working
crsctl check cssd
Display disk-groups
set lines 100
col name
format a10
col path
format a30
select name
,
group_number
,
disk_number
,
mount_status
,
state
,
path
from
v$asm_disk
order
by group_number
/note. A group number of zero his indicates that a disk is available but hasn't yet been assigned to
a
disk group.
Show disk space usage
select name
,
group_number
,
disk_number
,
total_mb
,
free_mb
from
v$asm_disk
order
by group_number
/Create a disk group
create diskgroup data1
external redundancy
disk '/dev/raw/raw1'
/or with multiple raw partitions...
multiple disks
create diskgroup data2
external redundancy
disk
'/dev/raw/raw2'
,
'/dev/raw/raw3'
/or with multiple fail groups...
create diskgroup data3
normal redundancy
failgroup controller1 disk '/dev/raw/raw4'
failgroup controller2 disk '/dev/raw/raw6'
/Add a disk to a group
alter diskgroup data1
add disk '/dev/raw/raw4'
/Wildcards can be used for raw device names (eg. raw*)
Drop a disk group
drop diskgroup '<name>'
/Is ASM performing a balancing operation
select *
from v$asm_operation
/Mount/dismount disk groups
alter diskgroup all mount
alter diskgroup data1 mount
alter diskgroup all dismount
alter diskgroup data1 dismount
Check the internal consistency of a diskgroup
alter diskgroup data1 check all
/
where
owner not in ('SYS','SYSTEM')
and
(max_extents - extents) < 10
order by 4
/To change maxextents
alter <segment_type> <segment_name> storage(maxextents 150);
List the contents of the temporary tablespace(s)
set pages 999 lines 100 col username format a15 col mb format 999,999
select su.username
,
ses.sid
,
ses.serial#
,
su.tablespace
,
ceil((su.blocks * dt.block_size) / 1048576) MB
from
v$sort_usage
su
,
dba_tablespaces dt
,
v$session ses
where
su.tablespace = dt.tablespace_name
and
su.session_addr = ses.saddr
/Structure - Objects
•
Find an object
•
Invalid objects
•
Show the size of an object
•
All objects owned by a user
•
Source code of a procedure
•
Get an objects ddl (9i onwards)
•
Display compilation errors and warnings
•
Find all tables containing the specified column
•
List all tables owned by a user sorted by size
Find an object
set pages 999
col owner format a15
col object_name format a40
col object_type format a20
select owner
,
object_name
,
object_type
from
dba_objects
where lower(object_name) like lower('%&object%')
order by owner, object_type, object_name
/
Invalid objects
List invalid objects...
set lines 200 pages 999
col "obj" format a40
select owner || '.' || object_name "obj",
object_type
from dba_objects
where status = 'INVALID'
/Recompile all invalid objects...
@?/rdbms/admin/utlrp.sql
Show the size of an object
col segment_name format a20
select segment_name
,
bytes "SIZE_BYTES"
,
ceil(bytes / 1024 / 1024) "SIZE_MB"
from
dba_segments
where segment_name like '&obj_name'
/All objects owned by a user
col object_name format a40
select object_name
,
object_type
from dba_objects
where owner = '&user'
order by object_type, object_name
/Source code of a procedure
select text
from dba_source
where owner = 'ANDY'
and name = 'FILE_TEST'
and type = 'PACKAGE BODY'
order by line
/Get an objects ddl (9i onwards)
Note. Works for 9i and newer.
Parameters: OBJECT_TYPE, OBJECT_NAME, SCHEMA
set pagesize 0
set long 90000
select dbms_metadata.get_ddl('TABLE','TABLE_A','ANDY') from dual;
Display compilation errors and warnings
show errors
show errors view <veiw_name>
show errors procedure <proc_name>
select * from dba_errors;
Find all tables containing the specified column
set pages 999 lines 100
col tab format a60
col column_name format a20
select owner || '.' || table_name as tab
,
column_name
from
dba_tab_columns
where
column_name like upper('&col')
/List all tables owned by a user sorted by size
set lines 100 pages 999
col
segment_name
format a40
col
mb
format 999,999,999
select segment_name
,
ceil(sum(bytes) / 1024 / 1024) "MB"
from
dba_segments
where
owner like '&user'
and
segment_type = 'TABLE'
group
by segment_name
order
by ceil(sum(bytes) / 1024 / 1024) desc
/Structure - Indexes
•
Index info by table
•
Show indexed columns
Index info by table
col cons_name format a30
select decode(constraint_type,
'C', 'Check',
'O', 'R/O View',
'P', 'Primary',
'R', 'Foreign',
'U', 'Unique',
'V', 'Check view') type
,
constraint_name cons_name
,
status
,
last_change
from
dba_constraints
where
owner like '&owner'
and
table_name like '&table_name'
order by 1
/List tables that are using the specified table as a foreign key
set lines 100 pages 999
select a.owner
,
a.table_name
,
a.constraint_name
from
dba_constraints a
,
dba_constraints b
where
a.constraint_type = 'R'
and
a.r_constraint_name = b.constraint_name
and
a.r_owner = b.owner
and
b.owner = '&table_owner'
and
b.table_name = '&table_name'
/Same as above, but produces 'disable constraint' statements
set lines 100 pages 999
col discon format a100
select 'alter table '||a.owner||'.'||a.table_name||' disable constraint
'||a.constraint_name||';' discon
from
dba_constraints a
,
dba_constraints b
where
a.constraint_type = 'R'
and
a.r_constraint_name = b.constraint_name
and
a.r_owner = b.owner
and
b.owner = '&table_owner'
and
b.table_name = '&table_name'
/Produce a list of disabled fk constraints
set lines 100 pages 999
col
table format a60
col
constraint_name format a30
select owner||'.'||table_name "table"
,
constraint_name
from
dba_constraints
where
status = 'DISABLED'
and
constraint_type = 'R'
and
owner not in ('SYS','SYSTEM')
order by 1,2
/Produce enable statements all disabled fk constraints
set lines 100 pages 999
select 'alter table '||owner||'.'||table_name||' enable constraint
'||constraint_name||';' "enable"
from
dba_constraints
where
status = 'DISABLED'
and
constraint_type = 'R'
and
owner not in ('SYS','SYSTEM')
order by 1
/List parent tables that may need fixing/re-importing
select distinct r.owner || '.' || r.table_name "exp"
from
dba_constraints c
,
dba_constraints r
where
c.status = 'DISABLED'
and
c.constraint_type = 'R'
and
c.r_owner = r.owner
and
c.r_constraint_name = r.constraint_name
and
c.owner not in ('SYS','SYSTEM')
order by 1
/List missing foriegn key values
Note. Useful for resolving ORA-02298
select 'select '||cc.column_name-
||' from '||c.owner||'.'||c.table_name-
||' a where not exists (select ''x'' from '-
||r.owner||'.'||r.table_name-
||' where '||rc.column_name||' = a.'||cc.column_name||')'
from
dba_constraints c,
dba_constraints r,
dba_cons_columns cc,
dba_cons_columns rc
where
c.constraint_type = 'R'
and
c.owner not in ('SYS','SYSTEM')
and
c.r_owner = r.owner
and
c.owner = cc.owner
and
r.owner = rc.owner
and
c.constraint_name = cc.constraint_name
and
r.constraint_name = rc.constraint_name
and
c.r_constraint_name = r.constraint_name
and
cc.position = rc.position
and
c.owner = '&table_owner'
and
c.table_name = '&table_name'
and
c.constraint_name = '&constraint_name'
order
by c.owner, c.table_name, c.constraint_name, cc.position
/Show all table constraints for a user
Note. This still needs some work...
set lines 100 pages 999
break on table_name
select table_name
,
decode(constraint_type,
'C', 'Check',
'O', 'R/O View',
'P', 'Primary',
'R', 'Foreign',
'U', 'Unique',
'V', 'Check view') type
,
nvl(index_name, R_CONSTRAINT_NAME) "IDX"
from
dba_constraints
where
owner like '&user'
order
by table_name
,
decode(constraint_type,
'P','0','R','1','U','2','C','3','O','4','V','5')
/Structure - Materialized view
•
Create a view log for the master table
•
List all materialized view logs
•
Create a simple materialized view
•
Show all materialized and resfresh times
•
Show materialized view tables and masters
•
Show refresh jobs in dba_jobs
•
Manually start a refresh
•
Force a complete refresh
Create a view log for the master table
This is required for fast refresh
create materialized view log on <table>
/or...
create materialized view log on <table>
tablespace <tablespace_name>
/
List all materialized view logs
select log_owner
,
log_table
from
dba_mview_logs
/Create a simple materialized view
create materialized view andy_mview
refresh [fast | complete | force]
start with sysdate
next sysdate + 1/24
with primary key
as select * from test_table
/Fast = update changes only
Complete = wipe and repopulate the mview
Force = fast if possible, complete if not.
Show all materialized and resfresh times
set lines 100 pages 999
col last_refresh format a20
select owner
,
mview_name
,
to_char(last_refresh_date, 'dd/mm/yy hh24:mi') last_refresh
from
dba_mviews
order by owner, last_refresh
/Show materialized view tables and masters
set lines 100
col mview format a40
col master format a40
select owner || '.' || name mview
,
master_owner || '.' || master master
from
dba_mview_refresh_times
/Show refresh jobs in dba_jobs
This is useful for spotting failures
set lines 100
col job format 9999
col log_user format a15
col last format a15
col next format a15
col fail format9999
col what format a20
select job
,
log_user
,
to_char(last_date, 'dd/mm/yy hh24:mi') last
,
to_char(next_date, 'dd/mm/yy hh24:mi') next
,
failures fail
,
replace(what, '"') what
from
dba_jobs
where
what like '%dbms_refresh.refresh%'
/Manually start a refresh
execute dbms_mview.refresh ('<owner.mv_table>');
Force a complete refresh
execute dbms_mview.refresh ('<owner.mv_table>','C');
Structure - Partitions
•
List partitioned tables
•
List a tables partitions
•
Show partition sizes for the specified table
•
Move a partition to a new tablespace
•
Add a partition
•
Split a partition
•
Drop a partition
•
Truncate a partition
List partitioned tables
set pages 999 lines 100
col table_name format a40
select table_name
,
partitioning_type type
,
partition_count partitions
from
dba_part_tables
where
owner = '&owner'
order by 1
/
List a tables partitions
set pages 999 lines 100
col high_value format a20
col tablespace_name format a20
select partition_name
,
tablespace_name
,
high_value
from
dba_tab_partitions
where
table_owner = '&owner'
and
table_name = '&table_name'
order by partition_position
/Show partition sizes for the specified table
set pages 999 lines 100
col tablespace_name format a20
col num_rows format 999,999,999
select p.partition_name
,
p.tablespace_name
,
p.num_rows
,
ceil(s.bytes / 1024 / 1204) mb
from
dba_tab_partitions p
,
dba_segments s
where
p.table_owner = s.owner
and
p.partition_name = s.partition_name
and
p.table_name = s.segment_name
and
p.table_owner = '&owner'
and
p.table_name = '&table_name'
order by partition_position
/Move a partition to a new tablespace
alter table <table_name>
move partition <partition_name>
tablespace <tablespace_name>
nologging
/Add a partition
alter table <table_name>
add partition <partition_name> values less than (<value>)
tablespace <tablespace_name>
/or...
alter table <table_name>
add partition <partition_name> values (<value>)
where
st.STATISTIC# = sn.STATISTIC#
and
st.VALUE > 0
and
st.SID = &SID
order
by value desc
/Resource intensive sql
change 8192 to match block size
select sql_text
,
executions
,
to_char((((disk_reads+buffer_gets)/executions) * 8192)/1048576,
'9,999,999,990.00')
as total_gets_per_exec_mb
,
to_char((( disk_reads
/executions) * 8192)/1048576,
'9,999,999,990.00')
as disk_reads_per_exec_mb
,
to_char((( buffer_gets
/executions) * 8192)/1048576,
'9,999,999,990.00')
as buffer_gets_per_exec_mb
,
parsing_user_id
from
v$sqlarea
where executions > 10
order by 6 desc
/File io stats
Requires timed_statistics=true
set lines 80 pages 999
col fname heading "File Name" format a60
col sizemb heading "Size(Mb)" format 99,999
col phyrds heading "Reads" format 999,999,999
col readtim heading "Time" format 99.999
col phywrts heading "Writes" format 9,999,999
col writetim heading "Time" format 99.999
select lower(name) fname
,
(bytes / 1048576) sizemb
,
phyrds
,
readtim
,
phywrts
,
writetim
from
v$datafile df
,
v$filestat fs
where
df.file# = fs.file#
order
by 1
/In session tracing
To switch it on:
exec dbms_system.set_sql_trace_in_session (<sid>, <serial#>, true);
To switch it off:
exec dbms_system.set_sql_trace_in_session (<sid>, <serial#>, false);
switch on event 10046
To switch it on:
alter session set events '10046 trace name context forever, level 8';
To switch it off:
alter session set events '10046 trace name context off';
Note. use tkprof to interpret the results.
Rows per block
select
avg(row_count) avg
, max(row_count) max
, min(row_count) min
from
(
select count(*) row_count
from
&table_name
group
by substr(rowid, 1, 15)
)
/Show the buffer cache advisory
Note. The current setting is halfway down and has a read factor of one.
set lines 100 pages 999
col est_mb format 99,999
col estd_physical_reads format 999,999,999,999,999
select size_for_estimate est_mb
,
estd_physical_read_factor
,
estd_physical_reads
from
v$db_cache_advice
where
name = 'DEFAULT'
order by size_for_estimate
/db_cache_advice needs to be on for the above to work
alter system set db_cache_advice=on;
Performance - Locks DML
•
Show sessions that are blocking each other
•
Show locked objects
•
Show which row is locked
•
List locks
Show sessions that are blocking each other
select 'SID ' || l1.sid ||' is blocking ' || l2.sid blocking
from
v$lock l1, v$lock l2
where
l1.block =1 and l2.request > 0
and
l1.id1=l2.id1
and
l1.id2=l2.id2
/Show locked objects
set lines 100 pages 999
col username
format a20
col sess_id
format a10
col object
format a25
col mode_held
format a10
select oracle_username || ' (' || s.osuser || ')' username
,
s.sid || ',' || s.serial# sess_id
,
owner || '.' ||object_name object
,
object_type
,
decode( l.block
,
0, 'Not Blocking'
,
1, 'Blocking'
,
2, 'Global') status
,
decode(v.locked_mode
,
0, 'None'
,
1, 'Null'
,
2, 'Row-S (SS)'
,
3, 'Row-X (SX)'
,
4, 'Share'
,
5, 'S/Row-X (SSX)'
,
6, 'Exclusive', TO_CHAR(lmode)) mode_held
from
v$locked_object v
,
dba_objects d
,
v$lock l
,
v$session s
where
v.object_id = d.object_id
and
v.object_id = l.id1
and
v.session_id = s.sid
order by oracle_username
,
session_id
/Show which row is locked
select do.object_name
,
row_wait_obj#
,
row_wait_file#
,
row_wait_block#
,
row_wait_row#
,
dbms_rowid.rowid_create (1, ROW_WAIT_OBJ#, ROW_WAIT_FILE#,
ROW_WAIT_BLOCK#, ROW_WAIT_ROW#)
from
v$session s
,
dba_objects do
where
sid=&sid
and
s.ROW_WAIT_OBJ# = do.OBJECT_ID
/Then select the row with that rowid...
select * from <table> where rowid=<rowid>;
List locks
column lock_type format a12
column mode_held format a10
column mode_requested format a10
column blocking_others format a20
column username format a10
SELECT session_id
,
lock_type
,
mode_held
,
mode_requested
,
blocking_others
,
lock_id1
FROM
dba_lock l
WHERE
lock_type NOT IN ('Media Recovery', 'Redo Thread')
/Performance - Locks DDL
•
Show all ddl locks in the system
•
Slightly more simple version of the above
•
Generate kill statement for ddl locking sessions
Show all ddl locks in the system
select decode(lob.kglobtyp,
0, 'NEXT OBJECT', 1, 'INDEX', 2, 'TABLE', 3, 'CLUSTER',
4, 'VIEW', 5, 'SYNONYM', 6, 'SEQUENCE',
7, 'PROCEDURE', 8, 'FUNCTION', 9, 'PACKAGE',
11, 'PACKAGE BODY', 12, 'TRIGGER',
13, 'TYPE', 14, 'TYPE BODY',
19, 'TABLE PARTITION', 20, 'INDEX PARTITION', 21, 'LOB',
22, 'LIBRARY', 23, 'DIRECTORY', 24, 'QUEUE',
28, 'JAVA SOURCE', 29, 'JAVA CLASS', 30, 'JAVA RESOURCE',
32, 'INDEXTYPE', 33, 'OPERATOR',
34, 'TABLE SUBPARTITION', 35, 'INDEX SUBPARTITION',