Oracle11G多列收集统计信息实验

前端之家收集整理的这篇文章主要介绍了Oracle11G多列收集统计信息实验前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

上一篇文章解决Oracle11g 多列收集统计信息--直方图的问题,现在来做一个实验验证,这是Oracle 11g的新特性。

sql> select * from v$version;
BANNER
--------------------------------------------------------------------------------
Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production
PL/sql Release 11.2.0.1.0 - Production
CORE 11.2.0.1.0 Production
TNS for 64-bit Windows: Version 11.2.0.1.0 - Production
NLSRTL Version 11.2.0.1.0 - Production

sql> drop table test purge;

sql> create table test as select * from dba_objects;
sql> create index ind_tt_oo on test(owner,object_type);
sql> exec dbms_stats.gather_table_stats(user,'test',cascade => true);
sql> select owner,object_type,count(1)
from test
where owner = 'SYS'
and object_type = 'CLUSTER'
group by owner,object_type;
OWNER OBJECT_TYPE COUNT(1)
------- -------------- ----------

SYS CLUSTER 10

--这种组合数据有10条

sql> set autotrace traceonly
sql> select * from test where owner='SYS' and object_type='CLUSTER';
执行计划
----------------------------------------------------------
Plan hash value: 3811756094
-----------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%cpu)| Time |
-----------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 298 | 28906 | 15 (0)| 00:00:01 |
| 1 | TABLE ACCESS BY INDEX ROWID| TEST | 298 | 28906 | 15 (0)| 00:00:01 |
|* 2 | INDEX RANGE SCAN | IND_TT_OO | 298 | | 2 (0)| 00:00:01 |--而评估出来是298条
-----------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
2 - access("OWNER"='SYS' AND "OBJECT_TYPE"='CLUSTER')
统计信息
----------------------------------------------------------
1 recursive calls
0 db block gets
9 consistent gets
0 physical reads
0 redo size
2450 bytes sent via sql*Net to client
519 bytes received via sql*Net from client
2 sql*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
10 rows processed

sql> set autotrace off
sql> declare
v_name varchar2(30);
begin
v_name := dbms_stats.create_extended_stats(user,'(owner,object_type)');
end;
/
sql> set autotrace traceonly
sql> exec dbms_stats.gather_table_stats(user,cascade => true,method_opt => 'FOR ALL COLUMNS SIZE SKEWONLY FOR COLUMNS (owner,object_type)',no_invalidate=>FALSE);
sql> select * from test where owner='SYS' and object_type='CLUSTER';
执行计划
----------------------------------------------------------
Plan hash value: 3811756094
-----------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%cpu)| Time |
-----------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 13 | 1417 | 2 (0)| 00:00:01 |
| 1 | TABLE ACCESS BY INDEX ROWID| TEST | 13 | 1417 | 2 (0)| 00:00:01 |
|* 2 | INDEX RANGE SCAN | IND_TT_OO | 13 | | 1 (0)| 00:00:01 |--评估出来是13条,基本接近
-----------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
2 - access("OWNER"='SYS' AND "OBJECT_TYPE"='CLUSTER')
统计信息
----------------------------------------------------------
8 recursive calls
0 db block gets
11 consistent gets
0 physical reads
0 redo size
2450 bytes sent via sql*Net to client
519 bytes received via sql*Net from client
2 sql*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)

10 rows processed

总结:在Oracle优化sql,很多时候是要让CBO评估准过滤数据的条数。

原文链接:https://www.f2er.com/oracle/210931.html

猜你在找的Oracle相关文章