Showing posts with label Scripts. Show all posts
Showing posts with label Scripts. Show all posts

Tuesday, January 29, 2013

SQL Trick - Always Return a Row



This SQL will always return a pre-defined row if none exist.  "no rows selected" was getting really old.

prompt
prompt ###############
prompt INVALID OBJECTS
prompt ###############
col owner       heading 'Owner'  format a15
col object_type heading 'Type'   format a20
col object_name heading 'Object' format A30
with t as (
select owner,
       object_type,
       object_name
from   dba_objects
where  status = 'INVALID'
order by owner,
         object_type,
         object_name)
select owner, object_type, object_name from (
   select rownum rn, owner, object_type, object_name
     from t
   union
   select 0, '~~All VALID~~', '~~All VALID~~', '~~All VALID~~' from dual
   order by 1 desc
   )
  where (rn > 0 or (rownum = 1 and rn = 0))
/

Sample Output when no rows are returned:
Owner           Type                 Object
--------------- -------------------- ------------------------------
~~All VALID~~   ~~All VALID~~        ~~All VALID~~

Redo Generated by Instance / Day


SQL:
prompt
prompt ##############
prompt REDO GENERATED
prompt ##############
col thrd      heading 'Instance'  format '9'
col trans_day heading 'Day'       format a6
col logs_mb   heading 'Redo (MB)' format '9,999,999'
break on thrd skip 1
compute sum label 'Week Total' of logs_mb on thrd
select thrd,
       to_char(transaction_day,'DY-DD') trans_day,
       sum(log_size) logs_mb
  from ( select distinct
                thread# thrd,
                sequence# sequence,
                trunc(first_time) transaction_day,
                round((blocks*block_size)/1048576) log_size
           from v$archived_log
          where first_time > sysdate - 7)
 group by thrd,
          transaction_day
 order by thrd​
/

Sample Output:
##############
REDO GENERATED
##############

Instance Day     Redo (MB)
-------- ------ ----------
       1 WED-08     38,913
         THU-09     50,678
         FRI-10     68,406
         SAT-11     59,472
         SUN-12     73,550
         MON-13     48,961
         TUE-14     81,264
         WED-15     41,908
********        ----------
Week Tot           463,152

       2 WED-08        130
         THU-09      1,206
         FRI-10      1,569
         SAT-11        384
         SUN-12        429
         MON-13        295
         TUE-14        457
         WED-15        264

Undo Status


Undo Status:
prompt
prompt #########
prompt UNDO Size
prompt #########
set head off
select to_char(sum(a.bytes)/1024/1024,'999,999')||' mb' undo_size
  from v$datafile a,
       v$tablespace b,
       dba_tablespaces c
 where c.contents = 'UNDO'
   and c.status = 'ONLINE'
   and b.name = c.tablespace_name
   and a.ts# = b.ts#
/
set head on
column block_size heading 'Block Size' new_value block_size
select to_number(value) block_size
  from v$parameter
 where name = 'db_block_size'
/
set verify off
prompt
prompt ################
prompt UNDO UTILIZATION
prompt ################
col tablespace_name heading 'Undo Tablespace' format a15
col status heading 'Status' format a15
col mb heading 'Size MB' format 999,999
select tablespace_name,
       status,
       round(sum(blocks) * &block_size/1024/1024,2) MB
  from dba_undo_extents
  group by tablespace_name,
           status
  order by tablespace_name,
           status
/

col undo_retention heading 'undo_retention' format a30
select to_char(value,'99,999')||' seconds or '||to_char(value/60,'99')||' minutes' undo_retention from v$parameter where name = 'undo_retention'
/
col undo_tablespace heading 'undo_tablespace' format a30
select value undo_tablespace from v$parameter where name = 'undo_tablespace'
/
col undo_management heading 'undo_management' format a30
select value undo_management from v$parameter where name = 'undo_management'
/
 
Notes:
In Undo Segments there are three types of extents,
Unexpired – Undo data whose age is less than the undo retention time.
Expired – Undo data whose age is greater than the undo retention time.
Active – Undo data that is part of an active transaction.
The sequence for using UNDO extents:
1. A new extent will be allocated from undo when the requirement arises. As undo is written to an undo segment, if the undo reaches the end of the current extent and the next extent contains expired undo then the new undo (generated by the current transaction) wraps into that expired extent, in preference to grabbing a free extent from the undo tablespace free extent pool.
2. If this fails because there are no available free extents and we cannot autoextend the datafile, then Oracle attempts to steal an expired extent from another undo segment.
3. If that fails then it tries to reuse an unexpired extent from the current undo segment.
4. If that fails, then it tries to steal an unexpired extent from another undo segment.
5. If all else fails, an Out-Of-Space error will be reported.

Oracle Changed Objects


set pages 50
set lines 140
col owner       heading 'Owner'   format a15
col object_name heading 'Object'  format a30
col object_type heading 'Type'    format a15
col last_ddl    heading 'Changed' format a15
select owner,
       object_name,
       object_type,
       to_char(last_ddl_time,'dd-mon-yy hh24:mi') last_ddl
  from dba_objects
 where owner not in ('SYS','SYSTEM')
   and trunc(last_ddl_time) >= trunc(sysdate-1) and trunc(last_ddl_time) < trunc(sysdate)
/

Database Size and Growth



col allocated_size heading 'Allocated' format 999,999
col used_size      heading 'Used'      format 999,999
col growth_size    heading 'Growth*'    format 999,999
select (a.data_size+b.temp_size+c.redo_size)/1024/1024 allocated_size,
       d.used_size/1024/1024 used_size,
       e.growth_size
  from ( select sum(bytes) data_size
           from dba_data_files ) a,
       ( select nvl(sum(bytes),0) temp_size
           from dba_temp_files ) b,
       ( select sum(bytes) redo_size
           from sys.v_$log ) c,
       ( select sum(bytes) used_size
           from dba_segments ) d,
       ( select round(sum(a.space_used_delta)/1024/1024) growth_size
           from  dba_hist_snapshot sn,
                 dba_hist_seg_stat a,
                 dba_objects b,
                 dba_segments c
           where (begin_interval_time >= trunc(sysdate-1) and begin_interval_time < trunc(sysdate))
             and sn.snap_id = a.snap_id
             and b.object_id = a.obj#
             and b.owner = c.owner
             and b.object_name = c.segment_name ) e
/
prompt *Growth during last calendar day
prompt

Sample Output:

Allocated     Used  Growth*
--------- -------- --------
  994,466  783,455   42,396
*Growth during last calendar day