在11g中,Oracle在统计信息方面进行了进一步的增强。
这篇介绍函数统计信息。
在11gr2中,可以收集基于函数的统计信息。
SQL> create table t_func as
2 select *
3 from dba_objects;
表已创建。
SQL> create index ind_f_type
2 on t_func(lower(object_type));
索引已创建。
SQL> exec dbms_stats.gather_table_stats(user, 't_func')
PL/SQL过程已成功完成。
SQL> explain plan for
2 select *
3 from t_func
4 where lower(object_type) = 'SYNONYM';
已解释。
SQL> select * from table(dbms_xplan.display);
PLAN_TABLE_OUTPUT
------------------------------------------------------------------------------------------
Plan hash value: 4219253938
------------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
------------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1960 | 212K| 47 (0)| 00:00:01 |
| 1 | TABLE ACCESS BY INDEX ROWID| T_FUNC | 1960 | 212K| 47 (0)| 00:00:01 |
|* 2 | INDEX RANGE SCAN | IND_F_TYPE | 1960 | | 3 (0)| 00:00:01 |
------------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
2 - access(LOWER("OBJECT_TYPE")='SYNONYM')
已选择14行。
SQL> begin
2 dbms_stats.gather_table_stats(
3 user,
4 't_func',
5 method_opt => 'for columns (lower(object_type)) size skewonly');
6 end;
7 /
PL/SQL过程已成功完成。
SQL> explain plan for
2 select *
3 from t_func
4 where lower(object_type) = 'SYNONYM';
已解释。
SQL> select * from table(dbms_xplan.display);
PLAN_TABLE_OUTPUT
------------------------------------------------------------------------------------------
Plan hash value: 4219253938
------------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
------------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 2 | 222 | 2 (0)| 00:00:01 |
| 1 | TABLE ACCESS BY INDEX ROWID| T_FUNC | 2 | 222 | 2 (0)| 00:00:01 |
|* 2 | INDEX RANGE SCAN | IND_F_TYPE | 2 | | 1 (0)| 00:00:01 |
------------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
2 - access(LOWER("OBJECT_TYPE")='SYNONYM')
已选择14行。
上面例子中,对比分析函数统计信息结果的前后,Oracle对于执行计划返回行数的判断,就可以看到Oracle新增的函数统计信息对于优化器的意义。
可以通过查询和列组相同的数据字典来查看函数的统计信息:
SQL> select dbms_stats.show_extended_stats_name(
2 user,
3 't_func',
4 '(lower(object_type))')
5 from dual;
DBMS_STATS.SHOW_EXTENDED_STATS_NAME(USER,'T_FUNC','(LOWER(OBJECT_TYPE))')
-------------------------------------------------------------------------
SYS_NC00016$
SQL> select extension_name, extension
2 from user_stat_extensions
3 where table_name= 'T_FUNC';
EXTENSION_NAME EXTENSION
------------------------------ --------------------------------------------------
SYS_NC00016$ (LOWER("OBJECT_TYPE"))
SQL> col column_name format a30
SQL> select column_name, num_distinct, histogram
2 from user_tab_col_statistics
3 where table_name = 'T_FUNC';
COLUMN_NAME NUM_DISTINCT HISTOGRAM
------------------------------ ------------ ---------------
OWNER 24 NONE
OBJECT_NAME 43580 NONE
SUBOBJECT_NAME 13997 NONE
OBJECT_ID 86232 NONE
DATA_OBJECT_ID 21456 NONE
OBJECT_TYPE 44 NONE
CREATED 3012 NONE
LAST_DDL_TIME 3045 NONE
TIMESTAMP 3153 NONE
STATUS 2 NONE
TEMPORARY 2 NONE
GENERATED 2 NONE
SECONDARY 2 NONE
NAMESPACE 20 NONE
EDITION_NAME 1 NONE
SYS_NC00016$ 44 FREQUENCY
已选择16行。
同样,这个函数的统计信息也可以通过create_extended_stats以及drop_extended_stats过程来进行创建或删除。
oracle视频教程请关注:http://u.youku.com/user_video/id_UMzAzMjkxMjE2.html