默认oracle会收集表中各个列的统计信息,但是会忽略列之间的关联关系。在大多情况下,优化器假设在复杂查询中的列之间是独立的。当where子句后指定了一个表的多个列条件时,优化器通常会将多个列的选择性(selectivity)相乘得到where语句的选择性,导致优化器做出错误判断!
Oracle 11g引入了多列统计信息概念,如果上面情况列关联性很好,可以做多列统计信息收集,让优化器做出正确判断。

在oracle 10g中,只有在一些特殊场合,优化器才会考虑列之间的关联关系:
-The optimizer used the number of distinct keys in an index to estimate selectivity provided all columns of a conjunctive predicate match all columns of a concatenated index key. In addition, the predicates must be equalities used in equijoins.
- If you set DYNAMIC_SAMPLING to level 4, the optimizer used dynamic sampling to estimate the selectivity of predicates involving multiple columns from a table. Because the sampling size is quite small, the results are dubious in most cases.

创建Column Groups:

DECLARE
cg_name varchar2();
BEGIN
cg_name := dbms_stats.create_extended_stats(null,'customers', '(cust_state_province,country_id)');
END;
/

查看Column Groups:

SQL> select extension_name, extension from dba_stat_extensions where table_name='CUSTOMERS';

EXTENSION_NAME                 EXTENSION
------------------------------ --------------------------------------------------------------------------------
SYS_STU#S#WF25Z#QAHIHE#MOFFMM_ ("CUST_STATE_PROVINCE","COUNTRY_ID") 或者
SQL> select sys.dbms_stats.show_extended_stats_name ('sh','customers','(cust_state_province,country_id)') col_group_name from dual; COL_GROUP_NAME
--------------------------------------------------
SYS_STU#S#WF25Z#QAHIHE#MOFFMM_

删除:

SQL> exec dbms_stats.drop_extended_stats('sh','customers','(cust_state_province, country_id)');

收集Column Groups的统计信息:

SQL> exec dbms_stats.gather_table_stats('sh','customers',method_opt =>'for all columns size skewonly for columns (cust_state_province,country_id) size skewonly');

监控Column Groups:

--查询多列统计信息
SQL> Select extension_name, extension from user_stat_extensions where table_name='CUSTOMERS'; EXTENSION_NAME EXTENSION
------------------------------ --------------------------------------------------------------------------------
SYS_STU#S#WF25Z#QAHIHE#MOFFMM_ ("CUST_STATE_PROVINCE","COUNTRY_ID") SQL>
--查看distinct数和柱状图使用情况
SQL> select e.extension col_group, t.num_distinct, t.histogram from user_stat_extensions e, user_tab_col_statistics t where e.extension_name = t.column_name and e.table_name = t.table_name and t.table_name = 'CUSTOMERS'; COL_GROUP NUM_DISTINCT HISTOGRAM
-------------------------------------------------------------------------------- ------------ ---------------
("CUST_STATE_PROVINCE","COUNTRY_ID") FREQUENCY SQL>

实验:
1)当不使用多列统计信息时,真实结果是3341,执行计划是1132.

SQL> exec dbms_stats.drop_extended_stats('sh','customers','(cust_state_province,country_id)');

PL/SQL procedure successfully completed.

SQL> select count(*) from sh.customers where CUST_STATE_PROVINCE = 'CA' and country_id=;

  COUNT(*)
---------- Execution Plan
----------------------------------------------------------
Plan hash value: --------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
--------------------------------------------------------------------------------
| | SELECT STATEMENT | | | | ()| :: |
| | SORT AGGREGATE | | | | | |
|* | TABLE ACCESS FULL| CUSTOMERS | | | ()| :: |
-------------------------------------------------------------------------------- Predicate Information (identified by operation id):
--------------------------------------------------- - filter("CUST_STATE_PROVINCE"='CA' AND "COUNTRY_ID"=) Statistics
----------------------------------------------------------
recursive calls
db block gets
consistent gets
physical reads
redo size
bytes sent via SQL*Net to client
bytes received via SQL*Net from client
SQL*Net roundtrips to/from client
sorts (memory)
sorts (disk)
rows processed

2)当使用多列统计信息时,真实结果是3341,执行计划是3437.

SQL> EXEC DBMS_STATS.GATHER_TABLE_STATS('SH','CUSTOMERS',METHOD_OPT =>'FOR ALL COLUMNS SIZE SKEWONLY FOR COLUMNS (CUST_STATE_PROVINCE,COUNTRY_ID) SIZE SKEWONLY');

PL/SQL procedure successfully completed.

SQL>  select count(*) from sh.customers where CUST_STATE_PROVINCE = 'CA' and country_id=;

  COUNT(*)
---------- Execution Plan
----------------------------------------------------------
Plan hash value: --------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
--------------------------------------------------------------------------------
| | SELECT STATEMENT | | | | ()| :: |
| | SORT AGGREGATE | | | | | |
|* | TABLE ACCESS FULL| CUSTOMERS | | | ()| :: |
-------------------------------------------------------------------------------- Predicate Information (identified by operation id):
--------------------------------------------------- - filter("CUST_STATE_PROVINCE"='CA' AND "COUNTRY_ID"=) Statistics
----------------------------------------------------------
recursive calls
db block gets
consistent gets
physical reads
redo size
bytes sent via SQL*Net to client
bytes received via SQL*Net from client
SQL*Net roundtrips to/from client
sorts (memory)
sorts (disk)
rows processed

3)即以上情况,使用多列统计信息能让优化器得到更准确的判断!

11G新特性 -- Multicolumn Statistics (Column groups)的更多相关文章

  1. 11G新特性 -- Expression Statistics

    当在查询中使用了function,返回值会受到影响. 比如: select count(*) from customers where lower(cust_state_province)='ca'; ...

  2. 11g新特性与12c新特性

    1. 11g新特性概图 管理新特性> 开发新特性> 2. 12c 新特性概图

  3. 11g新特性-自动sql调优(Automatic SQL Tuning)

    11g新特性-自动sql调优(Automatic SQL Tuning) 在Oracle 10g中,引进了自动sql调优特性.此外,ADDM也会监控捕获高负载的sql语句. 在Oracle 11g中, ...

  4. 使用Oracle 11g新特性 Active Database Duplication 搭建Dataguard环境

    Duplication Database 介绍 Duplicate database可以按照用途分为2种: duplicate database(复制出一个数据库) duplicate standby ...

  5. Oracle 11g 新特性 --SQL Plan Management 说明

    Oracle 11g 新特性 --SQL Plan Management 说明 参见大神博主文章: http://blog.csdn.net/tianlesoftware/article/detail ...

  6. Oracle 11g 新特性 – HM(Hang Manager)简介

    在这篇文章中我们会对oracle 11g 新特性—hang 管理器(Hang Manager) 进行介绍.我们需要说明,HM 只在RAC 数据库中存在. 在我们诊断数据库问题的时候,经常会遇到一些数据 ...

  7. 11G 新特性之 密码延迟认证

    11G 新特性之 密码延迟认证 11G 新特性之 密码延迟认证 Table of Contents 1. 特性简述 2. 特性潜在引发问题 3. 关闭特性 1 特性简述 为了防止用户密码的暴力破解,从 ...

  8. 11G新特性 -- Statistics Preferences

    Statistics Preferences新特性可以实现对指定对象进行信息收集. 可以在table.schema.database.global级别设置statistics preference. ...

  9. Oracle 11g新特性

    文章转自网络 Oracle 11g于2007年7月11日美国东部时间11时(北京时间11日22时)正式发布,11g是甲骨文公司30年来发布的最重要的数据库版本,根据用户的需求实现了信息生命周期管理(I ...

随机推荐

  1. CentOS7.5安装nodejs

    安装方法1——直接部署 1.首先安装wget yum install -y wget 如果已经安装了可以跳过该步 2.下载nodejs最新的tar包 可以在下载页面https://nodejs.org ...

  2. 【Java】 剑指offer(48) 最长不含重复字符的子字符串

    本文参考自<剑指offer>一书,代码采用Java语言. 更多:<剑指Offer>Java实现合集   题目 请从字符串中找出一个最长的不包含重复字符的子字符串,计算该最长子字 ...

  3. 012 HDFS API 文件读写代码演示

    一:准备工作 1.新建class类 2.开启HDFS服务 3.将配置文件拷贝进resources路径 方便了Configuration的读取配置. 二:读出HDFS文件系统中的文件到控制台 4.读出在 ...

  4. 018 spark on yarn (Job history)的配置,主要是yarn处跳转到历史聚合页面

    一:目标 1.目标 在yarn的8080页面可以跳转到spark的日志18080页面. 因为在运行spark之后,看对应的job的日志,这样直接连接,更合理直接. 2.总结 在后面可以看到,其实不需要 ...

  5. 今天刚学到truncate和delete的区别,做个总结吧

    truncate table : 删除内容,释放空间(表中数据会被删除,但不会进入oracle回收站,直接删除),不删除定义 delete table : 删除内容,不释放空间(表中数据虽被删除,但是 ...

  6. 【Java并发核心五】Future 和 Callable

    默认情况下,线程Thread对象不具有返回值的功能,如果在需要取得返回值的情况下会极为不方便.jdk1.5中可以使用Future 和 Callable 来获取线程返回值. Callable 可以 看成 ...

  7. Luogu

    dalao们的博客a http://hzwer.com   //Orz  %%% https://oi-wiki.org  //Orz https://www.cnblogs.com/-guz/p/9 ...

  8. 【RAY TRACING THE REST OF YOUR LIFE 超详解】 光线追踪 3-4 基于重要性采样的材质初探

     Preface 我们今天来把第三本书从开局到现在讲的一大堆理论运用到我们的框架中,那么今天我们首先将原始的材质改为基于重要性采样原理的材质 这一篇是代码工程中进行MC理论应用的初步尝试篇  Read ...

  9. pojA Star not a Tree?

    题目链接 pojA Star not a Tree? 题解 啊,模拟退火是个好东西 模拟退火即可 代码 #include<cmath> #include<cstdio> #in ...

  10. rabbitmq使用(三)

    Publish/Subscribe In the previous tutorial we created a work queue. The assumption behind a work que ...