C:\>sqlplus ywbz/ywbzwater
SQL*Plus: Release 11.2.0.1.0 Production on 星期日 1月 20 20:55:49 2013
Copyright (c) 1982, 2010, Oracle. All rights reserved.
连接到:
Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
--创建测试表
SQL> create table t as select * from dba_objects;
表已创建。
--制造列值分布不均匀
SQL> update t set object_id=1 where object_id<2001;
已更新1932行。
SQL> show parameter dynamic
NAME TYPE VALUE
------------------------------------ ----------- ------------------------------
optimizer_dynamic_sampling integer 2
SQL> commit;
提交完成。
--表已分析未生成直方图
SQL> exec dbms_stats.gather_table_stats('YWBZ','T',method_opt=>'for all columns
size 1');
PL/SQL 过程已成功完成。
SQL> select num_rows,blocks from user_tables where table_name='T';
NUM_ROWS BLOCKS
---------- ----------
73051 1065
SQL> set autotrace traceonly explain
SQL> select * from t where object_id=1;
执行计划
----------------------------------------------------------
Plan hash value: 1601196873
--------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
--------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 97 | 292 (1)| 00:00:04 |
|* 1 | TABLE ACCESS FULL| T | 1 | 97 | 292 (1)| 00:00:04 |
--------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - filter("OBJECT_ID"=1)
SQL> set autotrace off;
--object_id值为1的实际数量为1932
SQL> select count(*) from t where object_id=1;
COUNT(*)
----------
1932
--收集表T的统计信息并生成直方图
SQL> set autotrace off;
SQL> exec dbms_stats.gather_table_stats('YWBZ','T',method_opt=>'for all columns
size 254');
PL/SQL 过程已成功完成。
--高度平衡直方图(254)
SQL> set autotrace traceonly explain
SQL> select * from t where object_id=1;
执行计划
----------------------------------------------------------
Plan hash value: 1601196873
--------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
--------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1726 | 163K| 292 (1)| 00:00:04 |
|* 1 | TABLE ACCESS FULL| T | 1726 | 163K| 292 (1)| 00:00:04 |
--------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - filter("OBJECT_ID"=1)
SQL>
基础知识之生成直方图的重要性
最新推荐文章于 2022-02-09 16:02:34 发布