默认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. python全栈开发day46-BOM、位置信息、jQurey

    一.昨日内容回顾 1.DOM节点获取:三种方式 2.属性的设置: getAttirbute() setAttribute() .点设置,[]设置 3.节点的创建:   var oDiv = creat ...

  2. Docker 启动时容器无法联网

    转自:https://blog.csdn.net/u014062332/article/details/52911405 启动docker web服务时 虚拟机端口转发 外部无法访问 centos 7 ...

  3. Go 语言 IDE 之 VSCode 配置使用

    Gogland 是 JetBrains 公司推出的 Go 语言集成开发环境.Gogland 同样基于 IntelliJ 平台开发,支持 JetBrains 的插件体系.官方:https://www.j ...

  4. 关于Jar包 和 war

    Jar包: 别人写好的java类打包,将这些jar包引入你的项目中,然后就可以直接使用这些jar包中的类和属性以及方法,一般都会放在lib目录下 war 是web项目

  5. 《Gradle权威指南》--Groovy基础

    No1: Groovy中分号不是必须的 No2: Groovy中,单引号和双引号都可以定义一个字符串常量,不同的是单引号标记的是纯粹的字符串常量,而不是对字符串里的表达式做运算,但是双引号可以. ta ...

  6. ESLint + lint-staged 禁用老项目中的es6

    前言 ESLint作为插件化的javascript代码检测工具,为我们的平时的开发保驾护航,好处就不多说了详情查看官网. 问题 有这么一个五年前开发的老项目,机缘巧合到了我们这边来维护. 项目是zep ...

  7. python面向对象编程练习

    练习题 1.面向对象三大特性,各有什么用处,说说你的理解. 面向对象的三大特性: 1.继承:解决代码的复用性问题 2.封装:对数据属性严格控制,隔离复杂度 3.多态性:增加程序的灵活性与可扩展性 2. ...

  8. bootStrap中的ul导航

    <!doctype html><html > <head> <meta charset="utf-8"> <link rel= ...

  9. centos7 重置root 密码

    重置Centos 7 Root密码的方式和Centos 6完全不同.让我来展示一下到底如何操作. 1 - 在启动grub菜单,选择编辑选项启动 2 - 按键盘e键,来进入编辑界面 3 - 找到Linu ...

  10. 几种Unity运行平台的判断

    这里就介绍几种常见的,也是便于使用的几种平台判断的方法. 1.先说第一种,也是我用的顺手的一个.利用RuntimePlatform判断,API上的解释是[The platform applicatio ...