Friday, August 2, 2013

Some of the statistics collection commands which I often use in 11gR2

Some of the statistics collection commands which I often use in 11gR2



One drawback with METHOD_OPT => 'FOR ALL COLUMNS SIZE AUTO' is that Oracle will collect histograms.  If you don’t want that, give  FOR ALL COLUMNS SIZE 1 so that it won’t collect histograms. 

To collect statistics for an entire schema
SQL> exec dbms_stats.gather_schema_stats (ownname => 'MADHU', estimate_percent => 5, method_opt=>'FOR ALL COLUMNS SIZE AUTO', cascade=>true, degree => 4, no_invalidate=>false, GRANULARITY=> 'ALL');

To collect statistics for an entire schema even if statistics is LOCKED for that schema
SQL> exec dbms_stats.gather_schema_stats (ownname => 'MADHU', estimate_percent => 5, method_opt=>'FOR ALL COLUMNS SIZE AUTO', cascade=>true, degree => 4, no_invalidate=>false, GRANULARITY=> 'ALL',FORCE=>TRUE);


To collect statistics on  a particular table
SQL> exec DBMS_STATS.GATHER_TABLE_STATS (ownname => 'MADHU', tabname => 'TASK' , estimate_percent => 10 , cascade => TRUE, degree => 4, no_invalidate=>false, METHOD_OPT => 'FOR ALL COLUMNS SIZE AUTO', GRANULARITY=> 'DEFAULT');

To collect statistics on  a particular table even if statistics is LOCKED for that table
SQL> exec DBMS_STATS.GATHER_TABLE_STATS (ownname => 'MADHU', tabname => 'TASK' , estimate_percent => 10 , cascade => TRUE, degree => 4, no_invalidate=>false, METHOD_OPT => 'FOR ALL COLUMNS SIZE AUTO', GRANULARITY=> 'DEFAULT', FORCE=> TRUE);

Use following option in case table has '/' character inside the table name like we might have seen in SAP instances. Note that I have used double quotes around the table name and used single quote outside.


exec DBMS_STATS.GATHER_TABLE_STATS (
      OwnName        => 'SAPBIW'
     ,TabName        => '"/BIC/AZEIP_O0400"'
    ,Estimate_Percent  => 10
    ,Method_Opt        => 'FOR ALL COLUMNS SIZE 1'
    ,Degree            => 4
    ,Cascade           => FALSE
    ,No_Invalidate     => FALSE);


To collect statistics for only tables which has NULL stats in a particular schema
SQL> exec dbms_stats.gather_schema_stats (ownname => 'MADHU', estimate_percent => 5, method_opt=>'FOR ALL COLUMNS SIZE AUTO', cascade=>true, degree => 4, no_invalidate=>false, GRANULARITY=> 'ALL',options=>'GATHER EMPTY');

Use following command to find tables where stats is locked
SQL> select owner, table_name, stattype_locked from dba_tab_statistics where owner='MADHU';

command to lock table stats
exec dbms_stats.lock_table_stats('OWNER_NAME','TABLE_NAME');

command to unlock locked stats
exec dbms_stats.unlock_table_stats('OWNER_NAME','TABLE_NAME');

Details about statistics collection history
For table
SQL> select * from dba_tab_stats_history where table_name='SKUSAFETYSTOCKPARAM';
SQL> select * from sys.WRI$_OPTSTAT_tab_history where obj#=62595 order by savtime; - to see other details

or
select a.savtime,a.flags,a.rowcnt,a.blkcnt,a.avgrln,a.samplesize,to_char(a.analyzetime,'DD-MON-YY HH24:MI:SS')  as anlzetime
 from sys.WRI$_OPTSTAT_tab_history a, dba_objects b where a.obj#=b.object_id and b.owner='MADHU' 
and b.object_name='PROCESSRECONCILE' order by 1

For index
SQL>select a.savtime,a.flags,a.rowcnt,a.blevel,a.leafcnt,a.samplesize,to_char(a.analyzetime,'DD-MON-YY HH24:MI:SS')  as anlzetime
from sys.WRI$_OPTSTAT_ind_history a, dba_objects b where a.obj#=b.object_id and
b.owner='MADHU' and b.object_name='I173124' order by 1;

Stats commands on partitions.
-- Command to export stats of a table
set echo on
spool export_partitions.log
rem Create stats table
exec dbms_stats.create_stat_table('MADHU','STAT_W_TRANSACTION_MAY16');
rem Export stats for MADHU.WHOLESALE_TRANSACTION to above stats table
exec dbms_stats.export_table_stats(ownname => 'MADHU',tabname => 'WHOLESALE_TRANSACTION',stattab =>'STAT_W_TRANSACTION_MAY16', cascade => true, statown =>'MADHU');
spool off;

-- Command to copy table partition stats
set echo on
set trimspool on
spool copy_partitions_stats.log
rem Script to copy partition stats from partition A to partition B
exec dbms_stats.copy_table_stats(ownname => 'MADHU',tabname => 'WHOLESALE_TRANSACTION',srcpartname => 'WHOLESALE_TRANSACTION_20111231',dstpartname => 'WHOLESALE_TRANSACTION_20120430',FORCE => TRUE);
spool off;

Gathering system stats
exec dbms_stats.gather_system_stats('Start');

run for 2 hours and stop it by executing
exec dbms_stats.gather_system_stats('Stop');
or by
EXEC DBMS_STATS.GATHER_SYSTEM_STATS('interval', interval => 60)

To disable auto stats collection in 10g and 11g. We could also do this from Grid Control from Automated Maintenance Tasks under
Scheduler Central.

BEGIN
dbms_auto_task_admin.disable(client_name => 'auto optimizer stats collection', operation => NULL, window_name => NULL);
END;

Changing dbms_stats time
BEGIN
sys.dbms_scheduler.disable( '"SYS"."GATHER_STATS_JOB"' );
sys.dbms_scheduler.set_attribute_null( name => '"SYS"."GATHER_STATS_JOB"', attribute => 'schedule_name');
sys.dbms_scheduler.set_attribute( name => '"SYS"."GATHER_STATS_JOB"', attribute => 'end_date', value => to_timestamp_tz('2010-12-10 09:45:00 America/Los_Angeles', 'YYYY-MM-DD HH24:MI:SS TZR'));
sys.dbms_scheduler.enable( '"SYS"."GATHER_STATS_JOB"' );
END;

BEGIN
sys.dbms_scheduler.disable( '"SYS"."GATHER_STATS_JOB"' );
sys.dbms_scheduler.set_attribute_null( name => '"SYS"."GATHER_STATS_JOB"', attribute => 'schedule_name');
sys.dbms_scheduler.set_attribute( name => '"SYS"."GATHER_STATS_JOB"', attribute => 'repeat_interval', value => 'FREQ=DAILY;BYHOUR=10;BYMINUTE=0;BYSECOND=0');
sys.dbms_scheduler.enable( '"SYS"."GATHER_STATS_JOB"' );
END;

BEGIN
DBMS_SCHEDULER.DISABLE(
name=>'"SYS"."WEEKNIGHT_WINDOW"',
force=>TRUE);
END;

BEGIN
DBMS_SCHEDULER.SET_ATTRIBUTE(
name=>'"SYS"."WEEKNIGHT_WINDOW"',
attribute=>'REPEAT_INTERVAL',
value=>'FREQ=WEEKLY;BYDAY=MON,TUE,WED,THU,FRI;BYHOUR=10;BYMINUTE=0;BYSECOND=0');
END;

BEGIN
DBMS_SCHEDULER.ENABLE(
name=>'"SYS"."WEEKNIGHT_WINDOW"');
END;

EXP-00091: Exporting questionable statistics

Getting following error while performing a schema export as follows
$ exp / owner=WEBADMIN  direct=y file=$DMP_FILE log=$EXPORT_DIR/$EXPORT_LOG 2>&1

Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
Export done in US7ASCII character set and AL16UTF16 NCHAR character set
server uses AL32UTF8 character set (possible charset conversion)

About to export specified users ...
. exporting pre-schema procedural objects and actions
. exporting foreign function library names for user WEBADMIN
. exporting PUBLIC type synonyms
. exporting private type synonyms
. exporting object type definitions for user WEBADMIN
About to export WEBADMIN's objects ...
. exporting database links
. exporting sequence numbers
. exporting cluster definitions
. about to export WEBADMIN's tables via Direct Path ...
. . exporting table                  ALL_DATABASES       1232 rows exported
EXP-00091: Exporting questionable statistics.
. . exporting table                    APPLICATION        578 rows exported
EXP-00091: Exporting questionable statistics.
. . exporting table               APPLICATION_BKUP        449 rows exported
EXP-00091: Exporting questionable statistics.
…………
. exporting refresh groups and children
. exporting dimensions
. exporting post-schema procedural objects and actions
. exporting statistics
Export terminated successfully with warnings.

We can look at oerr about this error

madrman001:oracle:prman:/backup/prman/webadmin: oerr exp 00091
00091, 00000, "Exporting questionable statistics."
// *Cause:  Export was able export statistics, but the statistics may not be
//          usuable. The statistics are questionable because one or more of
//          the following happened during export: a row error occurred, client
//          character set or NCHARSET does not match with the server, a query
//          clause was specified on export, only certain partitions or
//          subpartitions were exported, or a fatal error occurred while
//          processing a table.
// *Action: To export non-questionable statistics, change the client character
//          set or NCHARSET to match the server, export with no query clause,
//          export complete tables. If desired, import parameters can be
//          supplied so that only non-questionable statistics will be imported,
//          and all questionable statistics will be recalculated.


Beginning with Oracle 9i by default export attempts to include statistics in the export. But the export will still generate the warning message about the questionable statistics in one of the following cases even if using STATISTICS=ESTIMATE / COMPUTE
1. There are row errors while exporting
2. The client character set or NCHAR character set does not match the server character set or NCHAR character set (the NLS_LANG is different than the database character set for example).
3. A QUERY clause is specified
4. Only certain partitions or subpartitions are exported.
The situation in case 2 is the most frequent and can be solved by setting the NLS_LANG character set of the client to the character set of the database.

Modified script as follows and completed export
export NLS_LANG=.AL32UTF8

The Oracle base for ORACLE_HOME=/orasw/prman/app/oracle/product/11.2.0/db is /orasw/prman/app/oracle

Export: Release 11.2.0.3.0 - Production on Wed Jul 10 09:30:21 2013

Copyright (c) 1982, 2011, Oracle and/or its affiliates.  All rights reserved.


Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
Export done in AL32UTF8 character set and AL16UTF16 NCHAR character set

About to export specified users ...
. exporting pre-schema procedural objects and actions
. exporting foreign function library names for user WEBADMIN
. exporting PUBLIC type synonyms
. exporting private type synonyms
. exporting object type definitions for user WEBADMIN
About to export WEBADMIN's objects ...
. exporting database links
. exporting sequence numbers
. exporting cluster definitions
. about to export WEBADMIN's tables via Direct Path ...
. . exporting table                  ALL_DATABASES       1232 rows exported
. . exporting table                    APPLICATION        578 rows exported
. . exporting table               APPLICATION_BKUP        449 rows exported
. . exporting table               APPLICATION_JUN1        449 rows exported
-------
. exporting dimensions
. exporting post-schema procedural objects and actions
. exporting statistics
Export terminated successfully without warnings.

Restoring missing archivelogs for standby in datagauard

Today one of my standby RAC database crashed due to SAN issues and when it came back we got error message saying some data files needs recovery when we tried to open database in read only mode. So the fix is to enable MRP to do recovery using archive log using following command and then check recovery status

SQL>  alter database recover managed standby database disconnect from session;
SQL>  select inst_id,process, status,thread#, sequence#, block#,blocks,delay_mins from gv$managed_standby;

In our case I saw MRP0 is looking for WAIT_FOR_GAP. So I investigated for GAP details in standby

SQL> select * from gv$archive_gap;

   INST_ID    THREAD# LOW_SEQUENCE# HIGH_SEQUENCE#
---------- ---------- ------------- --------------
         2          2         12604          12604
         1          2         12604          12604

Checking last log shipped in Oracle dataguard standby
1) SQL> select process, status, sequence#,block#,blocks,delay_mins from v$managed_standby;
and you should see latest sequence# against MRP0 line.
2) Following command will tell us if  there is any delay in standby with primary
SQL> SELECT ARCH.THREAD# "Thread", ARCH.SEQUENCE# "Last Sequence Received", APPL.SEQUENCE# "Last Sequence Applied",
      (ARCH.SEQUENCE# - APPL.SEQUENCE#) "Difference"
       FROM
        (SELECT THREAD# ,SEQUENCE# FROM V$ARCHIVED_LOG WHERE (THREAD#,FIRST_TIME ) IN
        (SELECT THREAD#,MAX(FIRST_TIME) FROM V$ARCHIVED_LOG GROUP BY THREAD#)) ARCH,
        (SELECT THREAD# ,SEQUENCE# FROM V$LOG_HISTORY WHERE (THREAD#,FIRST_TIME ) IN
        (SELECT THREAD#,MAX(FIRST_TIME) FROM V$LOG_HISTORY GROUP BY THREAD#)) APPL
         WHERE ARCH.THREAD# = APPL.THREAD#
     ORDER BY 1;

On primary use following command to restore deleted archivelog from tape

#!/bin/ksh
# Script to restore a particular archive log file from tape backup
# We can also use this syntax too
#restore archivelog from logseq = 12604 thread 2 until logseq = 12604 thread 2;
export ORAENV_ASK=NO
. oraenv
export ORACLE_SID=ptsa1
export SERV=`hostname`
export NB_ORA_POLICY=px_ptsa_rman
export NB_ORA_POLICY_ARCH=px_ptsa_arch
export NB_ORA_CLIENT="ptsa1-vip"
export CLASS=px_ptsa_arch
export ORACLE_BASE=/orasw/ptsa/app/oracle
export ORACLE_HOME=/orasw/ptsa/app/oracle/product/11.2.0/db
export PATH=$ORACLE_HOME/bin:$PATH:.:/usr/local/bin:/usr/contrib/bin
export ADMINDIR=/usr/local/admin/bin
export REPOSITORY=rman_user/password@prman
export NLS_LANG=american
export NLS_DATE_FORMAT='dd-mon-yyyy hh24:mi:ss'
rman log /orasw/static/oradba/bin/rman/rman_arch_restore.log <
connect catalog $REPOSITIORY
connect target /
connect rcvcat $REPOSITORY
run {
allocate  channel t1 device type 'sbt_tape';
send 'CPF1_BK_CLASS=$CLASS,CPF1_BK_POLICY=user_cp1';
send 'NB_ORA_POLICY=$NB_ORA_POLICY';
send 'NB_ORA_CLIENT=$NB_ORA_CLIENT';
set archivelog destination to '/arch/ptsa/arch';
restore archivelog sequence 12604 thread 2;
release channel t1;
}
EOF


Once the file is restored, FAL process in standby automatically transferred files from primary to standby  and GAP is resolved.
SQL> select inst_id, process, status,thread#,sequence# from gv$managed_standby;

   INST_ID PROCESS   STATUS          THREAD#  SEQUENCE#
---------- --------- ------------ ---------- ----------
         1 ARCH      CONNECTED             0          0
         1 ARCH      CONNECTED             0          0
         1 ARCH      CONNECTED             0          0
         1 ARCH      CONNECTED             0          0
         1 MRP0      APPLYING_LOG          2      12611
         2 ARCH      CLOSING               1      12598
         2 ARCH      CLOSING               1       9739
         2 ARCH      CLOSING               2      12604
         2 ARCH      CLOSING               2      12610
         2 RFS       IDLE                  0          0
         2 RFS       RECEIVING             0          0

   INST_ID PROCESS   STATUS          THREAD#  SEQUENCE#
---------- --------- ------------ ---------- ----------
         2 RFS       IDLE                  2      12611
         2 RFS       IDLE                  1      12599
         2 RFS       IDLE                  0          0
         2 RFS       IDLE                  0          0
         2 RFS       RECEIVING             0          0

16 rows selected.

SQL> select * from gv$archive_gap;
no rows selected

SQL> SELECT ARCH.THREAD# "Thread", ARCH.SEQUENCE# "Last Sequence Received", APPL.SEQUENCE# "Last Sequence Applied",
(ARCH.SEQUENCE# - APPL.SEQUENCE#) "Difference"
FROM
(SELECT THREAD# ,SEQUENCE# FROM V$ARCHIVED_LOG WHERE (THREAD#,FIRST_TIME ) IN
(SELECT THREAD#,MAX(FIRST_TIME) FROM V$ARCHIVED_LOG GROUP BY THREAD#)) ARCH,
(SELECT THREAD# ,SEQUENCE# FROM V$LOG_HISTORY WHERE (THREAD#,FIRST_TIME ) IN
(SELECT THREAD#,MAX(FIRST_TIME) FROM V$LOG_HISTORY GROUP BY THREAD#)) APPL
WHERE ARCH.THREAD# = APPL.THREAD#
ORDER BY 1; 

    Thread Last Sequence Received Last Sequence Applied Difference
---------- ---------------------- --------------------- ----------
         1                  12598                 12598          0
         2                  12610                 12610          0


Once GAP is resolved we will cancel MRP and will restart in real time as follows

SQL> alter database recover managed standby database cancel;
Database altered.

SQL> alter database open read only;
Database altered.

SQL> alter database recover managed standby database using current logfile disconnect;
Database altered.

SQL> select inst_id, process, status,thread#,sequence# from gv$managed_standby;