默认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. Docker+Jenkins持续集成环境(1)使用Docker搭建Jenkins+Docker持续集成环境

    本文介绍如何通过Jenkins的docker镜像从零开始构建一个基于docker镜像的持续集成环境,包含自动化构建.发布到仓库\并部署上线. 0. 前置条件 服务器安装docker,并启动docker ...

  2. buntu14.04和16.04官方默认更新源sources.list和第三方源推荐(干货!)转

    配置完成后: sudo apt-get update 安装和删除软件: sudo apt-get install sudo apt-get remove buntu14.04和16.04官方默认更新源 ...

  3. Codeforces Round #319 (Div. 2) D - Invariance of Tree

    Invariance of Tree 题目大意:给你一个有1-n组成的序列p,让你构造一棵树,如果节点a和b之间有一条边,则p[a]和p[b]之间也有一条边. 思路:没啥思路,看了题解菜爆. 我们可以 ...

  4. k8s 使用详解

    转自:https://www.cnblogs.com/gaoyuechen/p/8685771.html

  5. Python replace

    一.replace替换 a = "wohaoshuai" a.replace('a','o') wohooshuoi a.replace('a','') wohoshui

  6. MT4编程初级手册

    http://www.fxunion.com/college/2015/17554.html

  7. Python 手动新建 Scrapy项目

    # 创建项目 scrapy startproject 工程名 # 创建爬虫 scrapy genspider example example.com

  8. Java各种对象(PO,BO,VO,DTO,POJO,DAO,Entity,JavaBean,JavaBeans)的区分

    PO:持久对象 (persistent object),po(persistent object)就是在Object/Relation Mapping框架中的Entity,po的每个属性基本上都对应数 ...

  9. android studio git 将项目分享到github,推送到其他平台 码云 等。

    android studio git 将项目分享到github,推送到其他平台 码云 等. 作者:韩梦飞沙 Author:han_meng_fei_sha 邮箱:313134555@qq.com E- ...

  10. C语言--pow()函数实现

      为什么自己实现一个pow()函数呢?昨天在Linux操作系统中调用这一个函数时,一直报错,也没有查找到具体的原因.故心血来潮实现这一函数功能. 函数所在头文件:   在"math.h&q ...