【练习】ORACLE统计信息--直方图

①创建表tSQL> create table t as select * from dba_objects;

Table created.
--收集直方图
SQL> exec dbms_stats.gather_table_stats(user,‘T‘);

PL/SQL procedure successfully completed.
--此时数据库为表上的所有字段收集了缺省的统计信息,每个列两个Bucket:
SQL> col column_name for a30
SQL> col owner for a10
SQL> col table_name for a10
SQL> col ENDPOINT_ACTUAL_VALUE for a10
②查询
SQL> select * from dba_tab_histograms where table_name=‘T‘ and owner=‘SYS‘ order by 3;

OWNER TABLE_NAME COLUMN_NAME ENDPOINT_NUMBER
---------- ---------- ------------------------------ ---------------
ENDPOINT_VALUE ENDPOINT_A
-------------- ----------
SYS T CREATED 1
2457673.05

SYS T CREATED 0
2456529.48

SYS T DATA_OBJECT_ID 1
88710

OWNER TABLE_NAME COLUMN_NAME ENDPOINT_NUMBER
---------- ---------- ------------------------------ ---------------
ENDPOINT_VALUE ENDPOINT_A
-------------- ----------
SYS T DATA_OBJECT_ID 0
0

SYS T GENERATED 0
4.0500E+35

SYS T GENERATED 1
4.6211E+35

OWNER TABLE_NAME COLUMN_NAME ENDPOINT_NUMBER
---------- ---------- ------------------------------ ---------------
ENDPOINT_VALUE ENDPOINT_A
-------------- ----------
SYS T LAST_DDL_TIME 1
2457673.05

SYS T LAST_DDL_TIME 0
2452549.53

SYS T NAMESPACE 0
1

OWNER TABLE_NAME COLUMN_NAME ENDPOINT_NUMBER
---------- ---------- ------------------------------ ---------------
ENDPOINT_VALUE ENDPOINT_A
-------------- ----------
SYS T NAMESPACE 1
64

SYS T OBJECT_ID 1
88710

SYS T OBJECT_ID 0
2

OWNER TABLE_NAME COLUMN_NAME ENDPOINT_NUMBER
---------- ---------- ------------------------------ ---------------
ENDPOINT_VALUE ENDPOINT_A
-------------- ----------
SYS T OBJECT_NAME 0
2.4504E+35

SYS T OBJECT_NAME 1
6.2963E+35

SYS T OBJECT_TYPE 0
3.4943E+35

OWNER TABLE_NAME COLUMN_NAME ENDPOINT_NUMBER
---------- ---------- ------------------------------ ---------------
ENDPOINT_VALUE ENDPOINT_A
-------------- ----------
SYS T OBJECT_TYPE 1
4.5849E+35

SYS T OWNER 0
3.3913E+35

SYS T OWNER 1
4.5831E+35

OWNER TABLE_NAME COLUMN_NAME ENDPOINT_NUMBER
---------- ---------- ------------------------------ ---------------
ENDPOINT_VALUE ENDPOINT_A
-------------- ----------
SYS T SECONDARY 1
4.6211E+35

SYS T SECONDARY 0
4.0500E+35

SYS T STATUS 1
4.4786E+35

OWNER TABLE_NAME COLUMN_NAME ENDPOINT_NUMBER
---------- ---------- ------------------------------ ---------------
ENDPOINT_VALUE ENDPOINT_A
-------------- ----------
SYS T STATUS 0
4.4786E+35

SYS T SUBOBJECT_NAME 1
4.5340E+35

SYS T SUBOBJECT_NAME 0
1.8867E+35

OWNER TABLE_NAME COLUMN_NAME ENDPOINT_NUMBER
---------- ---------- ------------------------------ ---------------
ENDPOINT_VALUE ENDPOINT_A
-------------- ----------
SYS T TEMPORARY 1
4.6211E+35

SYS T TEMPORARY 0
4.0500E+35

SYS T TIMESTAMP 0
2.5558E+35

OWNER TABLE_NAME COLUMN_NAME ENDPOINT_NUMBER
---------- ---------- ------------------------------ ---------------
ENDPOINT_VALUE ENDPOINT_A
-------------- ----------
SYS T TIMESTAMP 1
2.6059E+35
28 rows selected.
--同时,列的低值、高值等信息会被收集记录在dba_tab_col_statistics中:
SQL> select table_name,column_name,num_distinct,low_value,high_value,DENSITY from dba_tab_col_statistics where owner=‘SYS‘ and table_name=‘T‘;

TABLE_NAME COLUMN_NAME NUM_DISTINCT
---------- ------------------------------ ------------
LOW_VALUE
----------------------------------------------------------------
HIGH_VALUE DENSITY
---------------------------------------------------------------- ----------
T OWNER 29
415045585F303330323030
584442 .034482759

T OBJECT_NAME 52212
2F31303030333233645F44656C6567617465496E766F636174696F6E4861
794362437253756253616D706C696E67547970653232395F54 .000019153

TABLE_NAME COLUMN_NAME NUM_DISTINCT
---------- ------------------------------ ------------
LOW_VALUE
----------------------------------------------------------------
HIGH_VALUE DENSITY
---------------------------------------------------------------- ----------

T SUBOBJECT_NAME 150
2456534E5F31
575248245F5741495453545F3234313436323135355F30 .006666667

T OBJECT_ID 86995
C103

TABLE_NAME COLUMN_NAME NUM_DISTINCT
---------- ------------------------------ ------------
LOW_VALUE
----------------------------------------------------------------
HIGH_VALUE DENSITY
---------------------------------------------------------------- ----------
C309580B .000011495

T DATA_OBJECT_ID 9106
80
C309580B .000109818

T OBJECT_TYPE 45

TABLE_NAME COLUMN_NAME NUM_DISTINCT
---------- ------------------------------ ------------
LOW_VALUE
----------------------------------------------------------------
HIGH_VALUE DENSITY
---------------------------------------------------------------- ----------
434C5553544552
584D4C20534348454D41 .022222222

T CREATED 978
787108180C2624
78740A0B02082A .001022495

TABLE_NAME COLUMN_NAME NUM_DISTINCT
---------- ------------------------------ ------------
LOW_VALUE
----------------------------------------------------------------
HIGH_VALUE DENSITY
---------------------------------------------------------------- ----------
T LAST_DDL_TIME 1096
78660A010D2A32
78740A0B02082A .000912409

T TIMESTAMP 1130
313939302D30382D32363A31313A32353A3030
323031362D31302D31313A30313A30373A3431 .000884956

TABLE_NAME COLUMN_NAME NUM_DISTINCT
---------- ------------------------------ ------------
LOW_VALUE
----------------------------------------------------------------
HIGH_VALUE DENSITY
---------------------------------------------------------------- ----------

T STATUS 1
56414C4944
56414C4944 1

T TEMPORARY 2
4E

TABLE_NAME COLUMN_NAME NUM_DISTINCT
---------- ------------------------------ ------------
LOW_VALUE
----------------------------------------------------------------
HIGH_VALUE DENSITY
---------------------------------------------------------------- ----------
59 .5

T GENERATED 2
4E
59 .5

T SECONDARY 2

TABLE_NAME COLUMN_NAME NUM_DISTINCT
---------- ------------------------------ ------------
LOW_VALUE
----------------------------------------------------------------
HIGH_VALUE DENSITY
---------------------------------------------------------------- ----------
4E
59 .5

T NAMESPACE 21
C102
C141 .047619048

TABLE_NAME COLUMN_NAME NUM_DISTINCT
---------- ------------------------------ ------------
LOW_VALUE
----------------------------------------------------------------
HIGH_VALUE DENSITY
---------------------------------------------------------------- ----------
T EDITION_NAME 0

0

15 rows selected.

③SQL> exec dbms_stats.gather_table_stats(user,‘T‘,method_opt=> ‘for all columns size 1‘);

PL/SQL procedure successfully completed.

SQL> exec dbms_stats.gather_table_stats(user,‘T‘,method_opt=>‘for columns owner size 1‘);

PL/SQL procedure successfully completed.
或删除owner字段统计信息,(无用)
--查询发现直方图信息并未被删除SQL> select * from dba_tab_histograms where table_name=‘T‘ and owner=‘SYS‘ order by 3;

OWNER TABLE_NAME COLUMN_NAME ENDPOINT_NUMBER
---------- ---------- ------------------------------ ---------------
ENDPOINT_VALUE ENDPOINT_A
-------------- ----------
SYS T CREATED 1
2457673.05

SYS T CREATED 0
2456529.48

SYS T DATA_OBJECT_ID 1
88710

OWNER TABLE_NAME COLUMN_NAME ENDPOINT_NUMBER
---------- ---------- ------------------------------ ---------------
ENDPOINT_VALUE ENDPOINT_A
-------------- ----------
SYS T DATA_OBJECT_ID 0
0

SYS T GENERATED 0
4.0500E+35

SYS T GENERATED 1
4.6211E+35

OWNER TABLE_NAME COLUMN_NAME ENDPOINT_NUMBER
---------- ---------- ------------------------------ ---------------
ENDPOINT_VALUE ENDPOINT_A
-------------- ----------
SYS T LAST_DDL_TIME 1
2457673.05

SYS T LAST_DDL_TIME 0
2452549.53

SYS T NAMESPACE 0
1

OWNER TABLE_NAME COLUMN_NAME ENDPOINT_NUMBER
---------- ---------- ------------------------------ ---------------
ENDPOINT_VALUE ENDPOINT_A
-------------- ----------
SYS T NAMESPACE 1
64

SYS T OBJECT_ID 1
88710

SYS T OBJECT_ID 0
2

OWNER TABLE_NAME COLUMN_NAME ENDPOINT_NUMBER
---------- ---------- ------------------------------ ---------------
ENDPOINT_VALUE ENDPOINT_A
-------------- ----------
SYS T OBJECT_NAME 0
2.4504E+35

SYS T OBJECT_NAME 1
6.2963E+35

SYS T OBJECT_TYPE 0
3.4943E+35

OWNER TABLE_NAME COLUMN_NAME ENDPOINT_NUMBER
---------- ---------- ------------------------------ ---------------
ENDPOINT_VALUE ENDPOINT_A
-------------- ----------
SYS T OBJECT_TYPE 1
4.5849E+35

SYS T OWNER 0
3.3913E+35

SYS T OWNER 1
4.5831E+35

OWNER TABLE_NAME COLUMN_NAME ENDPOINT_NUMBER
---------- ---------- ------------------------------ ---------------
ENDPOINT_VALUE ENDPOINT_A
-------------- ----------
SYS T SECONDARY 1
4.6211E+35

SYS T SECONDARY 0
4.0500E+35

SYS T STATUS 1
4.4786E+35

OWNER TABLE_NAME COLUMN_NAME ENDPOINT_NUMBER
---------- ---------- ------------------------------ ---------------
ENDPOINT_VALUE ENDPOINT_A
-------------- ----------
SYS T STATUS 0
4.4786E+35

SYS T SUBOBJECT_NAME 1
4.5340E+35

SYS T SUBOBJECT_NAME 0
1.8867E+35

OWNER TABLE_NAME COLUMN_NAME ENDPOINT_NUMBER
---------- ---------- ------------------------------ ---------------
ENDPOINT_VALUE ENDPOINT_A
-------------- ----------
SYS T TEMPORARY 1
4.6211E+35

SYS T TEMPORARY 0
4.0500E+35

SYS T TIMESTAMP 0
2.5558E+35

OWNER TABLE_NAME COLUMN_NAME ENDPOINT_NUMBER
---------- ---------- ------------------------------ ---------------
ENDPOINT_VALUE ENDPOINT_A
-------------- ----------
SYS T TIMESTAMP 1
2.6059E+35

28 rows selected.
④使用delete_column_stats可以彻底删除列的柱状图信息:
SQL> exec dbms_stats.delete_column_stats(user,‘T‘,‘OWNER‘);

PL/SQL procedure successfully completed.
--此时查询发现owner字段在直方图中已经删除
SQL> select * from dba_tab_histograms where table_name=‘T‘ and owner=‘SYS‘ order by 3;

OWNER TABLE_NAME COLUMN_NAME ENDPOINT_NUMBER
---------- ---------- ------------------------------ ---------------
ENDPOINT_VALUE ENDPOINT_A
-------------- ----------
SYS T CREATED 1
2457673.05

SYS T CREATED 0
2456529.48

SYS T DATA_OBJECT_ID 1
88710

OWNER TABLE_NAME COLUMN_NAME ENDPOINT_NUMBER
---------- ---------- ------------------------------ ---------------
ENDPOINT_VALUE ENDPOINT_A
-------------- ----------
SYS T DATA_OBJECT_ID 0
0

SYS T GENERATED 0
4.0500E+35

SYS T GENERATED 1
4.6211E+35

OWNER TABLE_NAME COLUMN_NAME ENDPOINT_NUMBER
---------- ---------- ------------------------------ ---------------
ENDPOINT_VALUE ENDPOINT_A
-------------- ----------
SYS T LAST_DDL_TIME 1
2457673.05

SYS T LAST_DDL_TIME 0
2452549.53

SYS T NAMESPACE 1
64

OWNER TABLE_NAME COLUMN_NAME ENDPOINT_NUMBER
---------- ---------- ------------------------------ ---------------
ENDPOINT_VALUE ENDPOINT_A
-------------- ----------
SYS T NAMESPACE 0
1

SYS T OBJECT_ID 1
88710

SYS T OBJECT_ID 0
2

OWNER TABLE_NAME COLUMN_NAME ENDPOINT_NUMBER
---------- ---------- ------------------------------ ---------------
ENDPOINT_VALUE ENDPOINT_A
-------------- ----------
SYS T OBJECT_NAME 0
2.4504E+35

SYS T OBJECT_NAME 1
6.2963E+35

SYS T OBJECT_TYPE 1
4.5849E+35

OWNER TABLE_NAME COLUMN_NAME ENDPOINT_NUMBER
---------- ---------- ------------------------------ ---------------
ENDPOINT_VALUE ENDPOINT_A
-------------- ----------
SYS T OBJECT_TYPE 0
3.4943E+35

SYS T SECONDARY 0
4.0500E+35

SYS T SECONDARY 1
4.6211E+35

OWNER TABLE_NAME COLUMN_NAME ENDPOINT_NUMBER
---------- ---------- ------------------------------ ---------------
ENDPOINT_VALUE ENDPOINT_A
-------------- ----------
SYS T STATUS 0
4.4786E+35

SYS T STATUS 1
4.4786E+35

SYS T SUBOBJECT_NAME 0
1.8867E+35

OWNER TABLE_NAME COLUMN_NAME ENDPOINT_NUMBER
---------- ---------- ------------------------------ ---------------
ENDPOINT_VALUE ENDPOINT_A
-------------- ----------
SYS T SUBOBJECT_NAME 1
4.5340E+35

SYS T TEMPORARY 0
4.0500E+35

SYS T TEMPORARY 1
4.6211E+35

OWNER TABLE_NAME COLUMN_NAME ENDPOINT_NUMBER
---------- ---------- ------------------------------ ---------------
ENDPOINT_VALUE ENDPOINT_A
-------------- ----------
SYS T TIMESTAMP 1
2.6059E+35

SYS T TIMESTAMP 0
2.5558E+35

26 rows selected.
⑤再次收集,owner字段又回来了
SQL> exec dbms_stats.gather_table_stats(user,‘T‘);

PL/SQL procedure successfully completed.

SQL> select * from dba_tab_histograms where table_name=‘T‘ and owner=‘SYS‘ order by 3;

OWNER TABLE_NAME COLUMN_NAME ENDPOINT_NUMBER
---------- ---------- ------------------------------ ---------------
ENDPOINT_VALUE ENDPOINT_A
-------------- ----------
SYS T CREATED 1
2457673.05

SYS T CREATED 0
2456529.48

SYS T DATA_OBJECT_ID 1
88710

OWNER TABLE_NAME COLUMN_NAME ENDPOINT_NUMBER
---------- ---------- ------------------------------ ---------------
ENDPOINT_VALUE ENDPOINT_A
-------------- ----------
SYS T DATA_OBJECT_ID 0
0

SYS T GENERATED 0
4.0500E+35

SYS T GENERATED 1
4.6211E+35

OWNER TABLE_NAME COLUMN_NAME ENDPOINT_NUMBER
---------- ---------- ------------------------------ ---------------
ENDPOINT_VALUE ENDPOINT_A
-------------- ----------
SYS T LAST_DDL_TIME 1
2457673.05

SYS T LAST_DDL_TIME 0
2452549.53

SYS T NAMESPACE 0
1

OWNER TABLE_NAME COLUMN_NAME ENDPOINT_NUMBER
---------- ---------- ------------------------------ ---------------
ENDPOINT_VALUE ENDPOINT_A
-------------- ----------
SYS T NAMESPACE 1
64

SYS T OBJECT_ID 1
88710

SYS T OBJECT_ID 0
2

OWNER TABLE_NAME COLUMN_NAME ENDPOINT_NUMBER
---------- ---------- ------------------------------ ---------------
ENDPOINT_VALUE ENDPOINT_A
-------------- ----------
SYS T OBJECT_NAME 0
2.4504E+35

SYS T OBJECT_NAME 1
6.2963E+35

SYS T OBJECT_TYPE 0
3.4943E+35

OWNER TABLE_NAME COLUMN_NAME ENDPOINT_NUMBER
---------- ---------- ------------------------------ ---------------
ENDPOINT_VALUE ENDPOINT_A
-------------- ----------
SYS T OBJECT_TYPE 1
4.5849E+35

SYS T OWNER 0
3.3913E+35

SYS T OWNER 1
4.5831E+35

OWNER TABLE_NAME COLUMN_NAME ENDPOINT_NUMBER
---------- ---------- ------------------------------ ---------------
ENDPOINT_VALUE ENDPOINT_A
-------------- ----------
SYS T SECONDARY 1
4.6211E+35

SYS T SECONDARY 0
4.0500E+35

SYS T STATUS 1
4.4786E+35

OWNER TABLE_NAME COLUMN_NAME ENDPOINT_NUMBER
---------- ---------- ------------------------------ ---------------
ENDPOINT_VALUE ENDPOINT_A
-------------- ----------
SYS T STATUS 0
4.4786E+35

SYS T SUBOBJECT_NAME 1
4.5340E+35

SYS T SUBOBJECT_NAME 0
1.8867E+35

OWNER TABLE_NAME COLUMN_NAME ENDPOINT_NUMBER
---------- ---------- ------------------------------ ---------------
ENDPOINT_VALUE ENDPOINT_A
-------------- ----------
SYS T TEMPORARY 1
4.6211E+35

SYS T TEMPORARY 0
4.0500E+35

SYS T TIMESTAMP 0
2.5558E+35

OWNER TABLE_NAME COLUMN_NAME ENDPOINT_NUMBER
---------- ---------- ------------------------------ ---------------
ENDPOINT_VALUE ENDPOINT_A
-------------- ----------
SYS T TIMESTAMP 1
2.6059E+35

28 rows selected.

SQL> exec dbms_stats.gather_table_stats(user,‘T‘,method_opt=>‘for columns owner size 1‘);

PL/SQL procedure successfully completed.
时间: 2024-10-03 23:29:23

【练习】ORACLE统计信息--直方图的相关文章

SQL Server 中统计信息直方图中对于没有覆盖到谓词预估以及预估策略的变化(SQL2012-->SQL2014-->SQL2016)

原文:SQL Server 中统计信息直方图中对于没有覆盖到谓词预估以及预估策略的变化(SQL2012-->SQL2014-->SQL2016) 本文出处:http://www.cnblogs.com/wy123/p/6770258.html 统计信息写过几篇了相关的文章了,感觉还是不过瘾,关于统计信息的问题,最近又踩坑了,该问题虽然不算很常见,但也比较有意思.相对SQL Server 2012,发现在新的SQL Server版本(2014,2016)中都有一些明显的变化,下文将对此进行粗浅的

MySQL 8.0 中统计信息直方图的尝试

直方图是表上某个字段在按照一定百分比和规律采样后的数据分布的一种描述,最重要的作用之一就是根据查询条件,预估符合条件的数据量,为sql执行计划的生成提供重要的依据. 在MySQL 8.0之前的版本中,MySQL仅有一个简单的统计信息却没有直方图,没有直方图的统计信息可以说是没有任何意义的. MySQL 8.0新特性之一就是开始支持统计信息的直方图,这个概念很早就提出来了,抽空具体尝试了一下使用方法. 之前写过MSSQL相关统计信息的一点东西,在原理上都是一致的, 照旧,直接上例子,造数据,创建一

收集oracle统计信息

优化器统计范围: 表统计: --行数,块数,行平均长度:all_tables:NUM_ROWS,BLOCKS,AVG_ROW_LEN:列统计: --列中唯一值的数量(NDV),NULL值的数量,数据分布:             --DBA_TAB_COLUMNS:NUM_DISTINCT,NUM_NULLS,HISTOGRAM:索引统计:--叶块数量,等级,聚簇因子:             --DBA_INDEXES:LEAF_BLOCKS,CLUSTERING_FACTOR,BLEVEL:

Oracle统计信息不准(谓词越界)造成的性能问题

什么是谓词越界?谓词越界其实就是SQL语句的查询条件超出了数据库统计信息所记录的范围.谓词越界会导致Oracle优化器错误的选择SQL语句的执行计划,导致性能问题. 这里举一个简单的例子说明谓词越界导致优化器选择了错误的执行计划. create table t1 (col1 number); create index idx_t1 on t1(col1); begin for i in 1..10000 loop insert into t1 values (i); end loop; comm

Oracle 统计信息介绍

统计信息自动执行需要以下条件满足: dba_autotask_task 字段status值ENABLED dba_autotask_client 字段status值ENABLED dba_autotask_window_clients 字段AUTOTASK_STATUS值ENABLED OPTIMIZER_STATS值ENABLED WINDOW_ACTIVE值FALSE dba_scheduler_windows 字段ENABLED值TRUE 字段ACTIVE值FALSE 字段DURATION

oracle统计信息

create table t2 as select * from dba_objects; 建索引: create index idx_t2 on t2(object_id); 2.收集T2的表所有列和表上所有索引的统计信息: exec dbms_stats.gather_table_stats(ownname=>'SYS',tabname=>'T2',estimate_percent=>100,cascade=>true,degree=>4); degree表示并行度: 3

Oracle 统计信息收集

统计信息收集 1:创建分析表 $ cd $ORACLE_HOME/rdbms/admin $ sqlplus / as sysdba > @utlxplan.sql 2:为了方便,可以创建一个同义表(默认情况下只有sys用户可以使用) SQL> create public synonym plan_tables for plan_table; 3:把这个表的权限给所有人,也可以给指定的人 SQL> grant all on plan_tables to public; 4:创建plust

oracle数据库优化之统计信息

1.统计信息简介 统计信息主要是描述数据库中表,索引的大小,规模,数据分布状况等的一类信息.比如,表的行数,块数,平均每行的大小,索引的leaf blocks,索引字段的行数,不同值的大小等,都属于统计信息.CBO正是根据这些统计信息数据,计算出不同访问路径下,不同join 方式下,各种计划的成本,最后选择出成本最小的计划. 在CBO(基于代价的优化器模式)条件下,SQL语句的执行计划由统计信息来决定,若没有统计信息则会采取动态采样的方式决定执行计划!可以说统计信息关乎sql的执行计划是否正确,

oracle表统计信息的使用

最近开发处理ECIF相关的项目,使用存储过程跑批处理数据.定期跑批数据,每次存储过程跑批使用临时表进行数据整理,再join这张临时表将数据放入目标表,最后清空临时表. 问题:在join处理这步中执行较慢,有时半天不出结果集. 解决方案:更新数据库表统计信息 + 使用多进程执行 多进程在此不多讲.每次往临时表插入数据,数据达到几百万条,存储过程结束之后又清除表,这样会让数据库误以为该临时表数据一致为0,因数据库表统计信息并非实时.更新表统计信息能良好地估计统计数据(尤其是针对较大的分区表),并能获