P. 1
Oracle 11g Nf 1.0

Oracle 11g Nf 1.0

|Views: 1,031|Likes:
Published by Ramu Spl

More info:

Published by: Ramu Spl on May 10, 2011
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as PDF, TXT or read online from Scribd
See more
See less

03/12/2013

pdf

text

original

Pending and Published Statistics

Starting with Oracle 11g, when gathering statistics, you have the option to automatically publish the statistics at
the end of the gather operation (default behavior), or to have the new statistics saved as pending. Saving the
new statistics as pending allows you to validate the new statistics and publish them only if they are satisfactory.

You can check whether or not the statistics will be automatically published checking the value of the PUBLISH
attribute using the DBMS_STATS package as in the following example:

SELECT DBMS_STATS.GET_PREFS('PUBLISH') PUBLISH FROM DUAL;

You can change the PUBLISH setting at either the schema or table level. Following are examples to do so:

-- setting PUBLISH at schema level
exec DBMS_STATS.SET_SCHEMA_PREFS('HR', 'PUBLISH', 'FALSE');

-- setting PUBLISH at table level

exec DBMS_STATS.SET_TABLE_PREFS('HR','EMPLOYEES', 'PUBLISH', 'FALSE');

Page 64

Oracle 11g New Features for Administrators - Summary Sheets

Published statistics are stored in data dictionary views, such as DBA_TAB_STATISTICS and USER_IND_
STATISTICS. Pending statistics are stored in views such as DBA_TAB_PENDING_STATISTICS and

DBA_IND_PENDING_STATISTICS.

select NUM_ROWS, BLOCKS, AVG_ROW_LEN, SAMPLE_SIZE, LAST_ANALYZED

from DBA_TAB_PENDING_STATISTICS where OWNER='HR' AND TABLE_NAME ='EMPLOYEES';

By default, the optimizer uses the published statistics stored in the data dictionary views. If you want the
optimizer to use the newly collected pending statistics, set the initialization parameter
OPTIMIZER_PENDING_STATISTICS to TRUE (the default value is FALSE), and then run a workload against the
table or schema or just gather its statistics:

ALTER SESSION SET OPTIMIZER_PENDING_STATISTICS = TRUE;

The optimizer will use the pending statistics (if available) instead of the published statistics when compiling SQL
statements. If the pending statistics are valid, they can be made public by executing the following statement:

-- for the whole database
exec DBMS_STATS.PUBLISH_PENDING_STATS(null, null);

-- publishing specific database object pending statistics

exec DBMS_STATS.PUBLISH_PENDING_STATS('HR','EMPLOYEES');

If you do not want to publish the pending statistics, delete them by executing the following statement:

exec DBMS_STATS.DELETE_PENDING_STATS('HR','EMPLOYEES');

Restoring Previous Versions of Statistics

With Oracle Database 11g, you can restore previous versions of statistics. DBMS_STATS package has the following
procedures to do that: RESTORE_DICTIONARY_STATS, RESTORE_FIXED_OBJECTS_STATS, RESTORE_SCHEMA_STATS,
RESTORE_SYSTEM_STATS, and RESTORE_TABLE_STATS.

First, query the view DBA_OPTSTAT_OPERATIONS to know when gathering the statistics has been done using
DBMS_STATS. Then decide to which point in time you want to restore the statistics.

-- list available versions of statistics

SELECT * FROM DBA_OPTSTAT_OPERATIONS;

-- restore to the point you want

begin

-- restore statistics of a specific schema
DBMS_STATS.RESTORE_SCHEMA_STATS(

OWNNAME => 'HR',

AS_OF_TIMESTAMP => '19-FEB-08 06.00.08.477333 AM -06:00');

-- restore statistics of a specific table

DBMS_STATS.RESTORE_SCHEMA_STATS(

OWNNAME => 'HR',

TABNAME => 'EMPLOYEES',

AS_OF_TIMESTAMP => '19-FEB-08 06.00.08.477333 AM -06:00');

end;

Oracle will manage the historical statistics repository, purging the statistics on a regular basis, by default every
31 days. To adjust this retention, consider the following examples:

-- get the current retention value
select DBMS_STATS.GET_STATS_HISTORY_RETENTION from dual;

-- get the oldest timestamp where statistics history is available
select DBMS_STATS.GET_STATS_HISTORY_AVAILABILITY from dual;

-- set the retention value

exec DBMS_STATS.ALTER_STATS_HISTORY_RETENTION ( 120 ); -- in days

MultiColumn Statistics

When multiple columns from a single table are used together in the where clause of a query (multiple single
column predicates), Oracle optimizer in previous versions (before 11g) does not understand the relationship
between the columns. In Oracle 11g, statistics on these columns as a group (column group) can be gathered and
thus resulting in high enhancement in CBO cardinality estimation.

Page 65

Oracle 11g New Features for Administrators - Summary Sheets

Note The MultiColumn Statistics and Expression Statistics new features are both constitute what is called
by Oracle as Extended Statistics. Expression Statistics is discussed in new following sub-section.

You can also create column groups manually by using the CREATE_EXTENDED_STATS function in the DBMS_STATS
package. This function creates a column statistics entry in the system for a user specified column group or an
expression in a table. This function returns a system-generated name of this newly created entry for the
extension. Following is an example:

declare
V_NAME VARCHAR2(30);

begin

-- stats of the combined columns will be collected next time the stats is gathered

V_NAME := DBMS_STATS.CREATE_EXTENDED_STATS(

OWNNAME => NULL,

TABNAME => 'EMPLOYEES',

EXTENSION => '(STATE_ID,COUNTRY_ID)');

-- you can then issue the gathering process

DBMS_STATS.GATHER_TABLE_STATS (NULL, 'EMPLOYEES', METHOD_OPT='for all columns size

skewonly' );

end;

The DBMS_STATS.GATHER_TABLE_STATS procedure can also be used to create column group and gather its
statistics all in one step. The keyword FOR COLUMNS is used in this case as shown in the following example:

begin

DBMS_STATS.GATHER_TABLE_STATS ('HR', 'EMPLOYEES',

METHOD_OPT=>'for all columns size skewonly for columns (STATE_ID,COUNTRY_ID)' );

end;

Note The default value of METHOD_OPT is 'FOR ALL COLUMNS SIZE AUTO' which makes Oracle create
column groups for a table, based on the workload analysis, similar to how it is done for histograms.

You can use the methods in the following code examples to retrieve information on column groups that have
been created:

-- you can query the data dictionary USER_STAT_EXTENSIONS

select EXTENSION_NAME, EXTENSION from USER_STAT_EXTENSIONS where TABLE_NAME='EMPLOYEES';

-- you can query USER_TAB_COL_STATISTICS (extension name appears as COLUMN_NAME)

select COLUMN_NAME, NUM_DISTINCT, HISTOGRAM

from USER_TAB_COL_STATISTICS where TABLE_NAME = 'EMPLOYEES';

-- you can use DBMS_STATS.SHOW_EXTENDED_STATS_NAME function

select DBMS_STATS.SHOW_EXTENDED_STATS_NAME(OWNNAME => 'HR',

TABNAME => 'EMPLOYEES',

EXTENSION => 'STATE_ID,COUNTRY_ID') AS E_NAME

from dual;

After gathering the multi-column statistics as show in the example, when you check the explain plan for a query
of a where condition like "STATE_ID = 'CA' AND COUNTRY_ID = 'US'", you will notice that the optimizer has
retrieved the correct number of expected retrieved rows. Practically, this will lead to a significant improvement in
the statement execution.

Following is how to drop a column group that you have previously defined:

exec DBMS_STATS.DROP_EXTENDED_STATS('HR', 'EMPLOYEES', '(STATE_ID,COUNTRY_ID)');

Expression Statistics

In Oracle 11g, you can create statistics on an expression. Following are examples to do that:

declare

V_NAME VARCHAR2(30);

begin

-- to create expression extended stats (not statistics are yet gathered)
V_NAME := DBMS_STATS.CREATE_EXTENDED_STATS(

OWNNAME => NULL,

Page 66

Oracle 11g New Features for Administrators - Summary Sheets

TABNAME => 'EMPLOYEES',

EXTENSION => '(lower(last_name))');

end;

begin

-- to create expression extended stats and gather the statistics in one step

DBMS_STATS.GATHER_TABLE_STATS(OWNNAME=>NULL,TABLE_NAME=>'EMPLOYEES',

METHOD_OPT=>'for all columns size skewonly for columns (lower(last_name))');

end;

After executing the code above, if you issue a query from EMPLOYEES table with a condition like
LOWER(LAST_NAME)='ABC', the optimizer has statistics about the retrieved rows and thus will be able to estimate
the correct number of returned rows. Consequently, the optimizer will most likely create a more efficient plan
than if those statistics were not present.

Use the DBA_STAT_EXTENSIONS data dictionary view to retrieve information on expression statistics that have
been created in the database.

select EXTENSION_NAME, EXTENSION from USER_STAT_EXTENSIONS where TABLE_NAME='EMPLOYEES';

Following is an example of the removal of an extended expression statistic:

exec DBMS_STATS.DROP_EXTENDED_STATS(null, 'EMPLOYEES', '(lower(lat_name))' );

Note that you will not be able to drop an extended expression statistics, if a function-based index is dependent
on that statistic (ORA-20000 error will be returned).

Automatically Collecting Statistics on Tables

In Oracle Database 11g, statistics are collected automatically for tables regardless of the MONITORING and
NOMONITORING keywords used when creating them. Those keywords are deprecated and ignored, if used.

You're Reading a Free Preview

Download
scribd
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->