Total Pageviews

Monday, 28 December 2015

Compiling Forms

-bash-4.1$ cat c
frmcmp_batch module=$1 userid=$2
c FORMNAME apps/password

Grant with grant option
=================
 grant select on scott.emp to apps with grant option;

Forms Compilation failed.
=====================



frmcmp_batch: fatal: /u01/DEV/R12/apps/tech_st/10.1.2/jdk/jre/lib/sparc/libjvm.so: unknown file type
./c: line 1: 26372 Killed                  frmcmp_batch module=$1 userid=$2 module_type=LIBRARY
This happens because libjvm.so gets corrupted. Replace this file from a working environment.
cd  /u01/DEV/R12/apps/tech_st/10.1.2/jdk/jre/lib/sparc/client/
mv libjvm.so libjvm.so-old
once moved copy file from another environment source environment profile and retest issue.

















References:
=======
http://mahamadsulthanoracleapplications.blogspot.com/2012/07/shell-script-example.html

http://onlineappsdba.com/index.php/2012/05/08/compiling-forms-in-oracle-apps-r12-app-wsh-231210-ora-04062-wsh_delivery_details_inv/

Monday, 3 August 2015

Dba Segments

DBA_SEGMENTS
===============
Storage allocated for all database segments

Columns
   ___________________________

   OWNER
      Username of the segment owner
   SEGMENT_NAME
      Name,if any,of the segment
   PARTITION_NAME
      Partition/Subpartition Name,if any,of the segment
   SEGMENT_TYPE
      "Type of segment: "TABLE","CLUSTER","INDEX","ROLLBACK","DEFERRED
   TABLESPACE_NAME
      Name of the tablespace containing the segment
   HEADER_FILE
      ID of the file containing the segment header
   HEADER_BLOCK
      ID of the block containing the segment header
   BYTES
      Size,in bytes of the segment
   BLOCKS
      Size,in Oracle blocks of the segment
   EXTENTS
      Number of extents allocated to the segment
   INITIAL_EXTENT
      Size,in bytes,of the initial extent of the segment
   NEXT_EXTENT
      Size,in bytes,of the next extent to be allocated to the segment
   MIN_EXTENTS
      Minimum number of extents allowed in the segment
   MAX_EXTENTS
      Maximum number of extents allowed in the segment
   PCT_INCREASE
      Percent by which to increase the size of the next extent to be allocated
   FREELISTS
      Number of process freelists allocated in this segment
   FREELIST_GROUPS
      Number of freelist groups allocated in this segment
   RELATIVE_FNO
      Relative number of the file containing the segment header
   BUFFER_POOL
      The default buffer pool to be used for segments blocks
Example sql:

-- Total size allocated for each tablespace:

COLUMN tablespace_name FORMAT A21
COLUMN Sum(bytes/(1024*1024)) FORMAT 999,999,999,999

Select tablespace_name,
Sum(bytes/(1024*1024)) TotalMB
From dba_segments
Group By tablespace_name;

-- Total space allocated by Owner:

Select owner,
sum(blocks) Totalblocks,
sum(bytes/(1024*1024)) TotalMB
From dba_segments
Group By owner
-- Total space allocated by Tablespace:

Select tablespace_name,
sum(blocks) Totalblocks,
sum(bytes/(1024*1024)) TotalMB
From dba_segments
Group By tablespace_name
-- Space used in each Segment:

SET LINESIZE 150
COLUMN tablespace_name FORMAT A15
COLUMN owner FORMAT A10
COLUMN segment_name FORMAT A35
COLUMN segment_type FORMAT A10
COLUMN extents FORMAT 9,999
COLUMN blocks FORMAT 999,999
COLUMN bytes FORMAT 999,999,999,999

Select tablespace_name,
owner,
segment_name,
segment_type, /* TABLE,INDEX */
extents, /* No. of extents in the segment*/
blocks, /* No. of db blocks in the segment*/
bytes /* No. of bytes in the segment*/
From dba_segments
Where owner NOT IN('SYSTEM','DBSNMP', 'ORDSYS', 'OUTLN','SYS')
Order By bytes ;

DBA_FREE_SPACE

Free extents in all tablespaces

Columns
   ___________________________

   TABLESPACE_NAME
      Name of the tablespace containing the extent
   FILE_ID
      ID number of the file containing the extent
   BLOCK_ID
      Starting block number of the extent
   BYTES
      Size of the extent in bytes
   BLOCKS
      Size of the extent in ORACLE blocks
   RELATIVE_FNO
      Relative number of the file containing the extent
Example

--Free space in each tablespace:

COLUMN tablespace_name FORMAT A15
COLUMN "Total Free (MB) " FORMAT 999,999,999,999
COLUMN "Largest Free Extent (MB) " FORMAT 999,999,999,999

Select tablespace_name,
Sum(bytes/(1024*1024)) "Total Free (MB) ",
Max(bytes/(1024*1024)) "Largest Free Extent (MB) "
From dba_free_space
Group By tablespace_name;


DBA_DATA_FILES

Information about database data files

Columns
   ___________________________

   FILE_NAME
      Name of the database data file
   FILE_ID
      ID of the database data file
   TABLESPACE_NAME
      Name of the tablespace to which the file belongs
   BYTES
      Size of the file in bytes
   BLOCKS
      Size of the file in ORACLE blocks
   STATUS
      File status: "INVALID" or "AVAILABLE"
   RELATIVE_FNO
      Tablespace-relative file number
   AUTOEXTENSIBLE
      Autoextensible indicator: "YES" or "NO"
   MAXBYTES
      Maximum size of the file in bytes
   MAXBLOCKS
      Maximum size of the file in ORACLE blocks
   INCREMENT_BY
      Default increment for autoextension
   USER_BYTES
      Size of the useful portion of file in bytes
   USER_BLOCKS
      Size of the useful portion of file in ORACLE blocks
Example SQL

-- List the Files in each Tablespace:

Select
RPAD(tablespace_name,18)||' '||RTRIM(file_name) Tablespace
From
dba_data_files
Order By tablespace_name;

To check for particular owner and table
===========================
SET LINESIZE 150
COLUMN tablespace_name FORMAT A15
COLUMN owner FORMAT A10
COLUMN segment_name FORMAT A35
COLUMN segment_type FORMAT A10
COLUMN extents FORMAT 9,999
COLUMN blocks FORMAT 999,999
COLUMN bytes FORMAT 999,999,999,999
Select tablespace_name,
owner,
segment_name,
segment_type, /* TABLE,INDEX */
extents, /* No. of extents in the segment*/
blocks, /* No. of db blocks in the segment*/
bytes /* No. of bytes in the segment*/
From dba_segments
Where owner IN('XXPRD')
--and SEGMENT_NAME like '%BKP%'
Order By bytes ;

Ref:
http://ss64.com/orad/DBA_SEGMENTS.html


Monday, 27 July 2015

SQL Query to Find Scheduled Concurrent Programs

SQL Query to Find Specific Scheduled Concurrent Programs 

=======================================================================
select * from (
SELECT fcr.request_id
     , fcpt.user_concurrent_program_name|| NVL2(fcr.description, ' (' || fcr.description || ')', NULL) conc_prog
     , fu.user_name requestor
     , fu.description requested_by
     , fu.email_address
     , frt.responsibility_name requested_by_resp
     , trim(fl.meaning) status
     , fcr.phase_code
     , fcr.status_code
     , fcr.argument_text "PARAMETERS"
      , TO_CHAR(fcr.request_date, 'DD-MON-YYYY HH24:MI:SS') requested
     , TO_CHAR(fcr.requested_start_date, 'DD-MON-YYYY HH24:MI:SS') requested_start   
     , TO_CHAR((fcr.requested_start_date), 'HH24:MI:SS') start_time
     , DECODE(fcr.hold_flag, 'Y', 'Yes', 'N', 'No') on_hold
     , CASE
          WHEN fcr.hold_flag = 'Y'
             Then Substr(
                    fu.description
                  , 0
                  , 40
                 )
       END last_update_by
     , CASE
          WHEN fcr.hold_flag = 'Y'
             THEN fcr.last_update_date
       END last_update_date
     , fcr.increment_dates
     , CASE WHEN fcrc.CLASS_INFO IS NULL THEN
        'Yes: ' || TO_CHAR(fcr.requested_start_date, 'DD-MON-YYYY HH24:MI:SS')
       ELSE
        'n/a'
       END run_once
     , CASE WHEN fcrc.class_type = 'P' THEN
        'Repeat every ' ||
        substr(fcrc.class_info, 1, instr(fcrc.class_info, ':') - 1) ||
        decode(substr(fcrc.class_info, instr(fcrc.class_info, ':', 1, 1) + 1, 1),
               'N', ' minutes',
               'M', ' months',
               'H', ' hours',
               'D', ' days') ||
        decode(substr(fcrc.class_info, instr(fcrc.class_info, ':', 1, 2) + 1, 1),
               'S', ' from the start of the prior run',
               'C', ' from the completion of the prior run')
       ELSE
         'n/a'
       END set_days_of_week
       , CASE WHEN fcrc.class_type = 'S' AND instr(substr(fcrc.class_info, 33),'1',1) > 0 THEN
          'Days of week: ' ||
                  decode(substr(fcrc.class_info, 33, 1), '1', 'Sun, ') ||
                  decode(substr(fcrc.class_info, 34, 1), '1', 'Mon, ') ||
                  decode(substr(fcrc.class_info, 35, 1), '1', 'Tue, ') ||
                  decode(substr(fcrc.class_info, 36, 1), '1', 'Wed, ') ||
                  decode(substr(fcrc.class_info, 37, 1), '1', 'Thu, ') ||
                  decode(substr(fcrc.class_info, 38, 1), '1', 'Fri, ') ||
                  decode(substr(fcrc.class_info, 39, 1), '1', 'Sat ')
         ELSE
           'n/a'
         end  days_of_week 
  FROM fnd_concurrent_requests fcr
     , fnd_user fu
     , fnd_concurrent_programs fcp
     , fnd_concurrent_programs_tl fcpt
     , fnd_printer_styles_tl fpst
     , fnd_conc_release_classes fcrc
     , fnd_responsibility_tl frt
     , fnd_lookups fl
 WHERE fcp.application_id = fcpt.application_id
   AND fcr.requested_by = fu.user_id
   AND fcr.concurrent_program_id = fcp.concurrent_program_id
   AND fcr.program_application_id = fcp.application_id
   AND fcr.concurrent_program_id = fcpt.concurrent_program_id
   AND fcr.responsibility_id = frt.responsibility_id
   AND fcr.print_style = fpst.printer_style_name(+)
   AND fcr.release_class_id = fcrc.release_class_id(+)
   AND fcr.status_code = fl.lookup_code
   AND fl.lookup_type = 'CP_STATUS_CODE'
   AND fcr.phase_code = 'P'
   AND 1=1
   )
where  1=1
and conc_prog = 'Concurrent Program Name' 
Order By conc_prog,requested_start Asc;

SQL Query to Find all Scheduled Concurrent Programs

==================================================================
By running below sql query in toad or sql develper, you can get list of scheduled concurrent requests.


SELECT fcr.request_id
     , fcpt.user_concurrent_program_name|| NVL2(fcr.description, ' (' || fcr.description || ')', NULL) conc_prog
     , fu.user_name requestor
     , fu.description requested_by
     , fu.email_address
     , frt.responsibility_name requested_by_resp
     , trim(fl.meaning) status
     , fcr.phase_code
     , fcr.status_code
     , fcr.argument_text "PARAMETERS"
      , TO_CHAR(fcr.request_date, 'DD-MON-YYYY HH24:MI:SS') requested
     , TO_CHAR(fcr.requested_start_date, 'DD-MON-YYYY HH24:MI:SS') requested_start   
     , TO_CHAR((fcr.requested_start_date), 'HH24:MI:SS') start_time
     , DECODE(fcr.hold_flag, 'Y', 'Yes', 'N', 'No') on_hold
     , CASE
          WHEN fcr.hold_flag = 'Y'
             Then Substr(
                    fu.description
                  , 0
                  , 40
                 )
       END last_update_by
     , CASE
          WHEN fcr.hold_flag = 'Y'
             THEN fcr.last_update_date
       END last_update_date
     , fcr.increment_dates
     , CASE WHEN fcrc.CLASS_INFO IS NULL THEN
        'Yes: ' || TO_CHAR(fcr.requested_start_date, 'DD-MON-YYYY HH24:MI:SS')
       ELSE
        'n/a'
       END run_once
     , CASE WHEN fcrc.class_type = 'P' THEN
        'Repeat every ' ||
        substr(fcrc.class_info, 1, instr(fcrc.class_info, ':') - 1) ||
        decode(substr(fcrc.class_info, instr(fcrc.class_info, ':', 1, 1) + 1, 1),
               'N', ' minutes',
               'M', ' months',
               'H', ' hours',
               'D', ' days') ||
        decode(substr(fcrc.class_info, instr(fcrc.class_info, ':', 1, 2) + 1, 1),
               'S', ' from the start of the prior run',
               'C', ' from the completion of the prior run')
       ELSE
         'n/a'
       END set_days_of_week
       , CASE WHEN fcrc.class_type = 'S' AND instr(substr(fcrc.class_info, 33),'1',1) > 0 THEN
          'Days of week: ' ||
                  decode(substr(fcrc.class_info, 33, 1), '1', 'Sun, ') ||
                  decode(substr(fcrc.class_info, 34, 1), '1', 'Mon, ') ||
                  decode(substr(fcrc.class_info, 35, 1), '1', 'Tue, ') ||
                  decode(substr(fcrc.class_info, 36, 1), '1', 'Wed, ') ||
                  decode(substr(fcrc.class_info, 37, 1), '1', 'Thu, ') ||
                  decode(substr(fcrc.class_info, 38, 1), '1', 'Fri, ') ||
                  decode(substr(fcrc.class_info, 39, 1), '1', 'Sat ')
         ELSE
           'n/a'
         end  days_of_week 
  FROM fnd_concurrent_requests fcr
     , fnd_user fu
     , fnd_concurrent_programs fcp
     , fnd_concurrent_programs_tl fcpt
     , fnd_printer_styles_tl fpst
     , fnd_conc_release_classes fcrc
     , fnd_responsibility_tl frt
     , fnd_lookups fl
 WHERE fcp.application_id = fcpt.application_id
   AND fcr.requested_by = fu.user_id
   AND fcr.concurrent_program_id = fcp.concurrent_program_id
   AND fcr.program_application_id = fcp.application_id
   AND fcr.concurrent_program_id = fcpt.concurrent_program_id
   AND fcr.responsibility_id = frt.responsibility_id
   AND fcr.print_style = fpst.printer_style_name(+)
   AND fcr.release_class_id = fcrc.release_class_id(+)
   AND fcr.status_code = fl.lookup_code
   AND fl.lookup_type = 'CP_STATUS_CODE'
   AND fcr.phase_code = 'P'
   AND 1=1
Order By Fu.Description, Fcr.Requested_Start_Date Asc;


Query to findout login and logout details in r12.
=================================
SELECT user_name "User Name",
to_char(start_time, 'MM-DD-YYYY HH24:MI:SS') "Login Time",
to_char(end_time, 'MM-DD-YYYY HH24:MI:SS') "Logout Time"
--,a.*
FROM apps.fnd_logins a,
apps.fnd_user b
WHERE a.user_id = b.user_id
AND b.user_name IN ('SYSADMIN')
AND TRUNC(start_time) between '1-Mar-2015' and '31-Jan-2016'
AND login_type = 'FORM'
ORDER BY start_time;
 

Ref:
===
https://community.oracle.com/thread/2518997?tstart=0
http://allaboutoracleapplication.blogspot.com/2014/01/sql-query-to-find-scheduled-concurrent.html

Thursday, 25 June 2015

Create a view to check Locked objects.

------------DROP VIEW APPS.XXALL_LOCKED_OBJECTS;
/* Formatted on 6/25/2015 11:10:04 AM (QP5 v5.256.13226.35510) */
CREATE OR REPLACE FORCE VIEW APPS.XXALL_LOCKED_OBJECTS
(
   OWNER,
   OBJECT_NAME,
   OBJECT_TYPE,
   SID,
   SERIAL#,
   STATUS,
   OSUSER,
   MACHINE
)
   BEQUEATH DEFINER
AS
   SELECT c.owner,
          c.object_name,
          c.object_type,
          b.SID,
          b.serial#,
          b.status,
          b.osuser,
          b.machine
     FROM v$locked_object a, v$session b, dba_objects c
    --TO ALTER AND KILL THE LOCKED OBJECTS
    ----  ALTER SYSTEM KILL SESSION 'SID,serial#' IMMEDIATE;
    ----  ALTER SYSTEM KILL SESSION '408,19512' IMMEDIATE;
    WHERE b.SID = a.session_id AND a.object_id = c.object_id;


===============================================================
How to find the locked objects and Kill the Session in Oracle
===========================================
Step-1 Run the following SQL query to find out the list of objects that has been locked

SELECT aob.object_name
,aob.object_id
,b.process
,b.session_id
FROM all_objects aob, v$locked_object b
WHERE aob.object_id = b.object_id

OR

SELECT aob.object_name
,aob.object_id
,b.process
,b.session_id
FROM all_objects aob, v$locked_object b
,V$session a
WHERE aob.object_id = b.object_id
and a.sid=b.session_id ;


Step-2 Now run the following SQL query with session id (from step-1)

SELECT SID, SERIAL#  FROM v$session WHERE SID = <SESSION_ID>
Note <SID> <SERIAL#>


Step-3 Run the following Query to kill the session with session_id and Serial no (from step-2)

ALTER SYSTEM KILL SESSION '<SID> ,<SERIAL#>';

Ref:--
http://appsdbaclass.blogspot.com/2011/05/how-to-find-locked-objects-and-kill_5619.html


Another eg:
=========
CREATE VIEW scott.empview
(
   ename,
   enumber
)
   BEQUEATH DEFINER
AS
   SELECT ename,enumber FROM scott.emp;

 now give grant to another user
======================
GRANT SELECT ON scott.empVIEW TO tom;

as tom user(fyi)
========
CREATE OR REPLACE SYNONYM tom.empVIEW  FOR scott.empview;

Query to check all application sessions along with their sid & pid:
================================================================
select /*+ ORDERED */
p.spid db_pid
, s.process mt_pid
, s.username||':'||SUBSTR(s.sid||','||s.serial#,1,15 ) sid_serial
, fl.start_time apps_logon_time
, fu.user_name apps_user
, s.machine||'.'||s.osuser mt_detail
--, logon_time mt_start_time
, substr(module,1,10) module, action
from fnd_logins fl
, fnd_user fu
, v$process p
, v$session s
where  decode('&&1','web',fl.spid,1)=decode('&&1','web',s .process,1)
and fl.pid=p.pid
and fl.process_spid=p.spid
and fl.serial#=p.serial#
and s.process is not null
and s.paddr = p.addr
and fl.user_id=fu.user_id
and fl.end_time is null


query to check locked objects
=====================
 SELECT c.owner,
       c.object_name,
       c.object_type,
       b.SID,
       b.serial#,
       b.status,
       b.osuser,
       b.machine
  FROM v$locked_object a, v$session b, dba_objects c
 WHERE b.SID = a.session_id AND a.object_id = c.object_id;


 ALTER SYSTEM KILL SESSION '428,4729'


Thursday, 4 June 2015

Sysadmin queries.

find users who have SYSADMIN responsibility


SELECT fu.USER_NAME,fu.DESCRIPTION,furgd.start_date,furgd.end_date,frvl.responsibility_name
FROM fnd_user_resp_groups_direct furgd, fnd_responsibility_vl frvl, fnd_user fu
WHERE furgd.responsibility_id = frvl.responsibility_id
AND fu.user_id = furgd.user_id
AND(to_char(furgd.end_date) is null
OR furgd.end_date > sysdate)
AND frvl.end_date is null
AND frvl.responsibility_name = 'System Administrator';

Another Script
==============
SELECT u.USER_NAME,u.START_DATE,u.END_DATE
       ,tr.RESPONSIBILITY_NAME
       ,r.START_DATE RESP_START_DATE,r.END_DATE RESP_END_DATE
    FROM fnd_user u,
         fnd_user_resp_groups_all ur,
         fnd_responsibility r,
         fnd_responsibility_tl tr,
         fnd_security_groups_vl s,
         fnd_application fa
  WHERE  1=1
  AND ur.responsibility_application_id = r.application_id
  AND ur.responsibility_id = r.responsibility_id
  AND tr.responsibility_id = r.responsibility_id
  AND u.user_id = ur.user_id
  AND ur.security_group_id = s.security_group_id
  AND r.application_id = fa.application_id
  AND trunc(sysdate) between  ur.start_date and nvl(ur.end_date,sysdate)
  AND trunc(sysdate) between  u.start_date and nvl(u.end_date,sysdate)
  AND tr.LANGUAGE='US'
  and RESPONSIBILITY_NAME='System Administrator'



With all details.
===========
SELECT fu.*
FROM fnd_user_resp_groups_direct furgd, fnd_responsibility_vl frvl, fnd_user fu
WHERE furgd.responsibility_id = frvl.responsibility_id
AND fu.user_id = furgd.user_id
AND(to_char(furgd.end_date) is null
OR furgd.end_date > sysdate)
AND frvl.end_date is null
AND frvl.responsibility_name = 'System Administrator';


-------------------------------------------------------------------------------
-- Query to find all responsibilities of a user
-------------------------------------------------------------------------------
SELECT fu.user_name                "User Name",
       frt.responsibility_name     "Responsibility Name",
       furg.start_date             "Start Date",
       furg.end_date               "End Date",     
       fr.responsibility_key       "Responsibility Key",
       fa.application_short_name   "Application Short Name"
  FROM fnd_user_resp_groups_direct        furg,
       applsys.fnd_user                   fu,
       applsys.fnd_responsibility_tl      frt,
       applsys.fnd_responsibility         fr,
       applsys.fnd_application_tl         fat,
       applsys.fnd_application            fa
 WHERE furg.user_id             =  fu.user_id
   AND furg.responsibility_id   =  frt.responsibility_id
   AND fr.responsibility_id     =  frt.responsibility_id
   AND fa.application_id        =  fat.application_id
   AND fr.application_id        =  fat.application_id
   AND frt.language             =  USERENV('LANG')
   AND UPPER(fu.user_name)      =  UPPER('SYSADMIN')  -- <change it>
   -- AND (furg.end_date IS NULL OR furg.end_date >= TRUNC(SYSDATE))
 ORDER BY frt.responsibility_name;


Another query to remove duplicates..
===========================
SELECT fu.user_id, fu.user_name, fur.responsibility_id,
fr.responsibility_name
FROM fnd_user fu, fnd_user_resp_groups fur, fnd_responsibility_vl fr
WHERE fu.user_id = fur.user_id
AND fr.application_id = fur.responsibility_application_id
AND fr.responsibility_id = fur.responsibility_id
AND TRUNC (SYSDATE) BETWEEN TRUNC (fr.start_date)
AND TRUNC (NVL ((fr.end_date - 1), SYSDATE))
AND TRUNC (SYSDATE) BETWEEN TRUNC (fur.start_date)
AND TRUNC (NVL ((fur.end_date - 1), SYSDATE))
and user_name like 'AHMED' --- for all user or for perticular user
-- AND fur.responsibility_application_id = 275 -- to check users for perticular responsibility
order by user_name


query to check list of users with last login date and end date
============================================
SQL>select USER_NAME,START_DATE,END_DATE,DESCRIPTION,LAST_LOGON_DATE
from fnd_user
where CREATED_BY<>1;


Monday, 1 June 2015

SYSAUX consuming more space

SYSAUX consuming more space
========================

Tips regarding 
SYSAUX Space Usage:
================
  • Make sure the SYSAUX tablespace is set to AUTOEXTEND off     -- This allows storage to be re-used vs. appended to
  • Confirm the STATISTICS_LEVEL value **                                    --  ALL is known to potentially be resource intensive while Basic and Typical are typically not
  • Check for the usage of advisors, baselines or sql tuning sets:   -- These require trapping information which is retained in snapshots even if the snapshot range was scheduled to be dropped
  • Run awrinfo.sql scripts on each instance                                    -- To better report and verify which objects are consuming the most space in the SYSAUX tablespace.
  • Run queries against sysaux_occupants                                      -- A simple method to get general information on SYSAUX storage consumption


A nicely formated report can be generated by running the awrinfo.sql script
SQL> connect / as sysdba
SQL> @?/rdbms/admin/awrinfo.sql                   --- For Windows - The script ( = @ ) was executed from the Oracle_Home (= ? ) if set

In the report you may tend to see the following or similar tables are consuming the most storage space

1. WRH$_ACTIVE_SESSION_HISTORY     
2. WRI$_OPTSTAT_HISTGRM_HISTORY
3. WRI$_OPTSTAT_HISTHEAD_HISTORY
@?/rdbms/admin/awrinfo.sql

In my case the culprit was

Space usage by non-AWR components (> 500K)
**********************************

COMPONENT MB SEGMENT_NAME SEGMENT_TYPE
--------- --------- --------------------------------------------------------------------- ---------------
NON_AWR 743.0 SYS.I_WRI$_OPTSTAT_HH_OBJ_ICOL_ST INDEX
NON_AWR 512.0 SYS.I_WRI$_OPTSTAT_HH_ST INDEX
NON_AWR 375.0 SYS.WRI$_OPTSTAT_OPR TABLE
NON_AWR 155.0 SYS.WRI$_OPTSTAT_OPR_TASKS TABLE
NON_AWR 144.0 SYS.WRI$_OPTSTAT_HISTHEAD_HISTORY.SYS_P11840 TABLE PARTITION

| Occupant Name Schema Name Space Usage
| -------------------- -------------------- ----------------
| SM/OPTSTAT SYS 3,260.1 MB


Resolution
======
exec DBMS_STATS.PURGE_STATS(DBMS_STATS.PURGE_ALL);
this will truncate all stats history tables .


changed the value to 4 days as it will purge every 31 days

exec dbms_stats.alter_stats_history_retention(4);

You can query DBA_AUTOTASK_CLIENT_HISTORY
DBA_AUTOTASK_CLIENT_HISTORY displays a per-window history of job execution counts for each automated maintenance task. In the history, you can see if the job has run successfully in the past and when it stopped running succesfully.


SQL> SELECT client_name, window_name, jobs_created, jobs_started, jobs_completed, window_start_time,window_end_time FROM dba_autotask_client_history WHERE client_name like '%stats%'; 


How long old stats are kept

select dbms_stats.get_stats_history_retention from dual;
Set retention of old stats to 10 days

exec dbms_stats.alter_stats_history_retention(10);
Purge stats older than 10 days (best to do this in stages if there is a lot of data (sysdate-30,sydate-25 etc)

exec DBMS_STATS.PURGE_STATS(SYSDATE-10);
Show available stats that have not been purged

select dbms_stats.get_stats_history_availability from dual;
Show how big the tables are and rebuild after stats have been purged

col Mb form 9,999,999
col SEGMENT_NAME form a40
col SEGMENT_TYPE form a6
set lines 120
select sum(bytes/1024/1024) Mb, segment_name,segment_type from dba_segments
where  tablespace_name = 'SYSAUX'
and segment_name like 'WRI$_OPTSTAT%'
and segment_type='TABLE'
group by segment_name,segment_type order by 1 asc
        MB SEGMENT_NAME                             SEGMEN
---------- ---------------------------------------- ------
         0 WRI$_OPTSTAT_OPR                         TABLE
         0 WRI$_OPTSTAT_AUX_HISTORY                 TABLE
        88 WRI$_OPTSTAT_TAB_HISTORY                 TABLE
       126 WRI$_OPTSTAT_IND_HISTORY                 TABLE
       158 WRI$_OPTSTAT_HISTGRM_HISTORY             TABLE
     4,482 WRI$_OPTSTAT_HISTHEAD_HISTORY            TABLE
Show how big the indexes are ready for a rebuild after stats have been purged

col Mb form 9,999,999
col SEGMENT_NAME form a40
col SEGMENT_TYPE form a6
set lines 120
select sum(bytes/1024/1024) Mb, segment_name,segment_type from dba_segments
where  tablespace_name = 'SYSAUX'
and segment_name like '%OPT%'
and segment_type='INDEX'
group by segment_name,segment_type order by 1 asc
/
        MB SEGMENT_NAME                             SEGMEN
---------- ---------------------------------------- ------
         0 WRH$_OPTIMIZER_ENV_PK                    INDEX
         0 I_WRI$_OPTSTAT_OPR_STIME                 INDEX
         0 I_WRI$_OPTSTAT_AUX_ST                    INDEX
        88 I_WRI$_OPTSTAT_TAB_ST                    INDEX
       105 I_WRI$_OPTSTAT_IND_ST                    INDEX
       105 I_WRI$_OPTSTAT_H_ST                      INDEX
       195 I_WRI$_OPTSTAT_TAB_OBJ#_ST               INDEX
       213 I_WRI$_OPTSTAT_H_OBJ#_ICOL#_ST           INDEX
       214 I_WRI$_OPTSTAT_IND_OBJ#_ST               INDEX
     2,055 I_WRI$_OPTSTAT_HH_ST                     INDEX
     3,883 I_WRI$_OPTSTAT_HH_OBJ_ICOL_ST            INDEX
Note that you cannot enable row movement and shrink the tables as the indexes are function based

alter table WRI$_OPTSTAT_IND_HISTORY enable row movement;
alter table WRI$_OPTSTAT_IND_HISTORY shrink space;
*
ERROR at line 1:
ORA-10631: SHRINK clause should not be specified for this object

select 'alter table '||segment_name||'  move tablespace SYSAUX;' from dba_segments where tablespace_name = 'SYSAUX'
and segment_name like '%OPT%' and segment_type='TABLE'
Run the rebuild table commands – note that this does cause any gather_stats jobs to fail

alter table WRI$_OPTSTAT_TAB_HISTORY  move tablespace sysaux;
alter table WRI$_OPTSTAT_IND_HISTORY  move tablespace sysaux;
alter table WRI$_OPTSTAT_HISTHEAD_HISTORY  move tablespace sysaux;
alter table WRI$_OPTSTAT_HISTGRM_HISTORY  move tablespace sysaux;
alter table WRI$_OPTSTAT_AUX_HISTORY  move tablespace sysaux;
alter table WRI$_OPTSTAT_OPR  move tablespace sysaux;
alter table WRH$_OPTIMIZER_ENV  move tablespace sysaux;
Script to generate rebuild statements

select 'alter index '||segment_name||'  rebuild online parallel (degree 14);' from dba_segments where tablespace_name = 'SYSAUX'
and segment_name like '%OPT%' and segment_type='INDEX'
Once completed it is best to check that the indexes (indices) are usable

select  di.index_name,di.index_type,di.status  from  dba_indexes di , dba_tables dt
where  di.tablespace_name = 'SYSAUX'
and dt.table_name = di.table_name
and di.table_name like '%OPT%'
order by 1 asc
/
SQL>
INDEX_NAME                     INDEX_TYPE                  STATUS
------------------------------ --------------------------- --------
I_WRI$_OPTSTAT_AUX_ST          FUNCTION-BASED NORMAL       VALID
I_WRI$_OPTSTAT_HH_OBJ_ICOL_ST  FUNCTION-BASED NORMAL       VALID
I_WRI$_OPTSTAT_HH_ST           FUNCTION-BASED NORMAL       VALID
I_WRI$_OPTSTAT_H_OBJ#_ICOL#_ST FUNCTION-BASED NORMAL       VALID
I_WRI$_OPTSTAT_H_ST            FUNCTION-BASED NORMAL       VALID
I_WRI$_OPTSTAT_IND_OBJ#_ST     FUNCTION-BASED NORMAL       VALID
I_WRI$_OPTSTAT_IND_ST          FUNCTION-BASED NORMAL       VALID
I_WRI$_OPTSTAT_OPR_STIME       FUNCTION-BASED NORMAL       VALID
I_WRI$_OPTSTAT_TAB_OBJ#_ST     FUNCTION-BASED NORMAL       VALID
I_WRI$_OPTSTAT_TAB_ST          FUNCTION-BASED NORMAL       VALID
WRH$_OPTIMIZER_ENV_PK          NORMAL                      VALID
Finally lets see what space has been saved with a retention date of 1 day and a gather schema stats for the SYSASDM schema

exec dbms_stats.alter_stats_history_retention(1);
select dbms_stats.get_stats_history_retention from dual;
        MB SEGMENT_NAME                             SEGMEN
---------- ---------------------------------------- ------
         0 WRI$_OPTSTAT_OPR                         TABLE
         0 WRI$_OPTSTAT_AUX_HISTORY                 TABLE
         3 WRI$_OPTSTAT_TAB_HISTORY                 TABLE
         4 WRI$_OPTSTAT_IND_HISTORY                 TABLE
         8 WRI$_OPTSTAT_HISTGRM_HISTORY             TABLE
       104 WRI$_OPTSTAT_HISTHEAD_HISTORY            TABLE

        MB SEGMENT_NAME                             SEGMEN
---------- ---------------------------------------- ------
         0 WRH$_OPTIMIZER_ENV_PK                    INDEX
         0 I_WRI$_OPTSTAT_OPR_STIME                 INDEX
         0 I_WRI$_OPTSTAT_AUX_ST                    INDEX
         2 I_WRI$_OPTSTAT_IND_ST                    INDEX
         2 I_WRI$_OPTSTAT_TAB_ST                    INDEX
         3 I_WRI$_OPTSTAT_TAB_OBJ#_ST               INDEX
         4 I_WRI$_OPTSTAT_IND_OBJ#_ST               INDEX
         5 I_WRI$_OPTSTAT_H_ST                      INDEX
         9 I_WRI$_OPTSTAT_H_OBJ#_ICOL#_ST           INDEX
        41 I_WRI$_OPTSTAT_HH_ST                     INDEX
        96 I_WRI$_OPTSTAT_HH_OBJ_ICOL_ST            INDEX

Ref
====
For more information : FAQ: Automatic Statistics Collection ( Doc ID 1233203.1 )
Suggestions if Your SYSAUX Tablespace Grows Rapidly or Too Large (Doc ID 1292724.1)
https://jhdba.wordpress.com/tag/dbms_stats-purge_stats/