DBMS_STATS.GATHER_TABLE_STATS
由于Oracle的优化器是CBO,所以对象的统计数据对执行计划的生成至关重要!
作用:DBMS_STATS.GATHER_TABLE_STATS统计表,列,索引的统计信息(默认参数下是对表进行直方图信息收集,包含该表的自身-表的行数、数据块数、行长等信息;列的分析--列值的重复数、列上的空值、数据在列上的分布情况;索引的分析-索引页块的数量、索引的深度、索引聚合因子).
DBMS_STATS.GATHER_TABLE_STATS的语法如下:
DBMS_STATS.GATHER_TABLE_STATS ( ownname VARCHAR2, tabname VARCHAR2, partname VARCHAR2, estimate_percent NUMBER, block_sample BOOLEAN, method_opt VARCHAR2, degree NUMBER, granularity VARCHAR2, cascade BOOLEAN, stattab VARCHAR2, statid VARCHAR2, statown VARCHAR2, no_invalidate BOOLEAN, force BOOLEAN);
参数说明:
ownname:要分析表的拥有者
tabname:要分析的表名.
partname:分区的名字,只对分区表或分区索引有用.
estimate_percent:采样行的百分比,取值范围[0.000001,100],null为全部分析,不采样. 常量:DBMS_STATS.AUTO_SAMPLE_SIZE是默认值,由oracle决定最佳取采样值.
block_sapmple:是否用块采样代替行采样.
method_opt:决定histograms信息是怎样被统计的.method_opt的取值如下(默认值为FOR ALL COLUMNS SIZE AUTO):
for all columns:统计所有列的histograms.
for all indexed columns:统计所有indexed列的histograms.
for all hidden columns:统计你看不到列的histograms
for columns <list> SIZE <N> | REPEAT | AUTO | SKEWONLY:统计指定列的histograms.N的取值范围[1,254]; REPEAT上次统计过的histograms;AUTO由oracle决定N的大小;SKEWONLY multiple end-points with the same value which is what we define by "there is skew in thedata
degree:决定并行度.默认值为null.
granularity:Granularity of statistics to collect ,only pertinent if the table is partitioned.
cascade:是收集索引的信息.默认为FALSE.
stattab:指定要存储统计信息的表,statid如果多个表的统计信息存储在同一个stattab中用于进行区分.statown存储统计信息表的拥有者.以上三个参数若不指定,统计信息会直接更新到数据字典.
no_invalidate: Does not invalidate the dependent cursors if set to TRUE. The procedure invalidates the dependent cursors immediately if set to FALSE.
force:即使表锁住了也收集统计信息.
例子:
execute dbms_stats.gather_table_stats(ownname => 'owner',tabname => 'table_name' ,estimate_percent => null ,method_opt => 'for all indexed columns' ,cascade => true);
http://blog.sina.com.cn/s/blog_679e928c0100yi15.html
例如:
在使用DBMS_STATS分析表的时候,我们经常要保存之前的分析,以防分析后导致系统性能低下然后进行快速恢复。
1、首先创建一个分析表,该表是用来保存之前的分析值:
SQL> begin
dbms_stats.create_stat_table(ownname=>'TEST',stattab=>'STAT_TABLE');
end;
/
PL/SQL 过程已成功完成。
SQL> begin
dbms_stats.gather_table_stats(ownname=>'TEST',tabname=>'T1');
end;
/
PL/SQL 过程已成功完成。
2、导出表分析信息到stat_table中
SQL> begin
dbms_stats.export_table_stats(ownname=>'TEST',tabname=>'T1',stattab=>'STAT_TABLE');
end;
/
PL/SQL 过程已成功完成。
SQL> select count(*) from TEST.STAT_TABLE;
COUNT(*)
----------
4
4、删除分析信息
SQL> begin
dbms_stats.delete_table_stats(ownname=>'TEST',tabname=>'T1');
end;
/
PL/SQL 过程已成功完成。
SQL> SELECT num_rows,blocks,empty_blocks as empty, avg_space, chain_cnt, avg_row_len FROM dba_tables WHERE owner = 'TEST'
AND table_name = 'T1';
NUM_ROWS BLOCKS EMPTY AVG_SPACE CHAIN_CNT AVG_ROW_LEN
---------- ---------- ---------- ---------- ---------- -----------
没有查到分析数据
5、导入分析信息
SQL> begin
dbms_stats.import_table_stats(ownname=>'TEST',tabname=>'T1',stattab=>'STAT_TABLE');
end;
/
PL/SQL 过程已成功完成。
SQL> SELECT num_rows,blocks,empty_blocks as empty, avg_space, chain_cnt, avg_row_len FROM dba_tables WHERE owner = 'TEST'
AND table_name = 'T1';
NUM_ROWS BLOCKS EMPTY AVG_SPACE CHAIN_CNT AVG_ROW_LEN
---------- ---------- ---------- ---------- ---------- -----------
1000 5 0 0 0 16
可以查到分析数据
补充:
EXPORT_COLUMN_STATS:导出列的分析信息
EXPORT_INDEX_STATS:导出索引分析信息
EXPORT_SYSTEM_STATS:导出系统分析信息
EXPORT_TABLE_STATS:导出表分析信息
EXPORT_SCHEMA_STATS:导出方案分析信息
EXPORT_DATABASE_STATS:导出数据库分析信息
IMPORT_COLUMN_STATS:导入列分析信息
IMPORT_INDEX_STATS:导入索引分析信息
IMPORT_SYSTEM_STATS:导入系统分析信息
IMPORT_TABLE_STATS:导入表分析信息
IMPORT_SCHEMA_STATS:导入方案分析信息
IMPORT_DATABASE_STATS:导入数据库分析信息
GATHER_INDEX_STATS:分析索引信息
GATHER_TABLE_STATS:分析表信息,当cascade为true时,分析表、列(索引)信息
GATHER_SCHEMA_STATS:分析方案信息
GATHER_DATABASE_STATS:分析数据库信息
GATHER_SYSTEM_STATS:分析系统信息
注: 转自 http://cjjwzs.iteye.com/blog/1143893
http://blog.csdn.net/mercenarylin/article/details/8559832
http://www.cnblogs.com/suredandan/p/3200157.html
I encountered this question in an interview and had no clue how to answer:
There is a table which has a index on a column, and you query:
select * from table_name where column_having_index="some value";
The query takes too long, and you find out that the index is not being used. If you think the performance of the query will be better using the index, how could you force the query to use the index?
You can use optimizer hints
select /*+ INDEX(table_name index_name) */ from table etc...
More on using optimizer hints: http://download.oracle.com/docs/cd/B19306_01/server.102/b14211/hintsref.htm
There could be many reasons for Index not being used. Even after you specify hints, there are chances Oracle optimizer thinks otherwise and decide not to use Index. You need to go through the EXPLAIN PLAN part and see what is the cost of the statement with INDEX and without INDEX.
Assuming the Oracle uses CBO. Most often, if the optimizer thinks the cost is high with INDEX, even though you specify it in hints, the optimizer will ignore and continue for full table scan. Your first action should be checking DBA_INDEXES to know when the statistics are LAST_ANALYZED. If not analyzed, you can set table, index for analyze.
begin
DBMS_STATS.GATHER_INDEX_STATS ( OWNNAME=>user
, INDNAME=>IndexName);
end;
For table.
begin
DBMS_STATS.GATHER_TABLE_STATS ( OWNNAME=>user
, TABNAME=>TableName);
end;
In extreme cases, you can try setting up the statistics on your own.
http://stackoverflow.com/questions/1838094/force-index-use-in-oracle/1838216#1838216
DBMS_STATS.GATHER_TABLE_STATS的更多相关文章
- 【转】DBMS_STATS.GATHER_TABLE_STATS详解
转自http://blog.itpub.net/26892340/viewspace-721935/ [作用] DBMS_STATS.GATHER_TABLE_STATS统计表,列,索引的统计信息(默 ...
- 【转】DBMS_STATS.GATHER_TABLE_STATS详解 2012-04-22 09:20:10
[转]DBMS_STATS.GATHER_TABLE_STATS详解 2012-04-22 09:20:10 分类: Linux 由于Oracle的优化器是CBO,所以对象的统计数据对执行计划的生成至 ...
- dbms_stats.gather_table_stats与analyze table 的区别[转贴]
Analyze StatementThe ANALYZE statement can be used to gather statistics for a specific table, index ...
- dbms_stats.gather_table_stats详解
dbms_stats.gather_table_stats 统计表,列,索引的统计信息(包含该表的自身-表的行数.数据块数.行长等信息: 列的分析--列值的重复数.列上的空值.数据在列上的分布情 ...
- 【Oracle】DBMS_STATS.GATHER_TABLE_STATS详解
由于Oracle的优化器是CBO,所以对象的统计数据对执行计划的生成至关重要! 作用:DBMS_STATS.GATHER_TABLE_STATS统计表,列,索引的统计信息(默认参数下是对表进行直 ...
- 在Oracle 11.2.0.1.0下dbms_stats.gather_table_stats收集直方图不准
SQL> select * from v$version; BANNER ------------------------------------------------------------ ...
- Oracle:DBMS_STATS.GATHER_TABLE_STATS的语法
转自: http://cjjwzs.iteye.com/blog/1143893 作用:DBMS_STATS.GATHER_TABLE_STATS统计表,列,索引的统计信息. DBMS_STATS.G ...
- 使用dbms_stats.gather_table_stats调整表的统计信息
创建实验表,插入10万行数据 SQL> create table test (id number,name varchar2(10)); Table created. SQL> decla ...
- 【Oracle】DBMS_STATS.GATHER_TABLE_STATS
月初一直在忙保监会报送的事情,苦逼的保险行业的ETL大家都懂的.今天闲来无事查看了一下前阵子的报送存储过程,发现系统隔一段时间就会调用一次DBMS_STATS.GATHER_TABLE_STATS,所 ...
随机推荐
- Swift - 移除页面视图上的所有元素
下面代码可以遍历移除页面视图上的所有元件 1 2 3 4 5 6 //清空所有子视图 func clearViews() { for v in self.view.subviews as [U ...
- hadoop namanodejava
最近突然想看下hadoop源码,有利于处理一些突发问题.先从name启动开始, NameNode.java public static void main(String argv[]) throws ...
- android: WheelView组件(滑轮组件)的应用!
android前段组件中, 填表单,选择条目 的样式有很多, WheelView滚动组件为其中一种,如下图所示: 前两 ...
- Unity3D 游戏开发构架篇 ——输入控制
临近毕业之初.进入Unity3D这个行业,是一家小工作室.老板人非常不错,公司氛围也非常单纯.近期公司开发一款小游戏,初次上手,颇多周折,记录下自己的开发心得.主要涉及一些设计理念,互相交流. 先说下 ...
- cURL安装和使用笔记
0.前言 cURL是一个利用URL语法在命令行下工作的文件传输工具.它支持文件上传和下载,所以是综合传输工具,但习惯称cURL为下载工具.cURL还包含了用于程序开发的libcurl.cURL ...
- PHP网站安装程序的原理及代码
原文:PHP网站安装程序的原理及代码 原理: 其实PHP程序的安装原理无非就是将数据库结构和内容导入到相应的数据库中,从这个过程中重新配置连接数据库的参数和文件,为了保证不被别人恶意使用安装文件,当安 ...
- [Android学习笔记]组合控件的使用
组合控件的使用 开发过程中,多个UI控件需要协同工作,相互交互之后,才可完成一个完整的业务需求,此时可把这些控件封装成为一个整体,相互之间的交互逻辑封装其中,外部调用可无需关心内部逻辑,只需获取处理后 ...
- XML实例文档
from: http://www.w3school.com.cn/xpath/xpath_examples.asp XML实例文档 我们将在下面的例子中使用这个 XML 文档: "books ...
- .idata数据的解析
每类Section代表不同的数据,不同的数据存储组织方式一定是有非常大区别的.代码段与资源段一定区别巨大,这意味着我需要一个一个的学习每个段的解析. idata段解析 这个段主要存储的是导入符号信息. ...
- 真实世界里的钢铁侠-特斯拉汽车创始人埃隆·马斯克(Elon Musk)
真实世界里的钢铁侠--特斯拉汽车公司和SpaceX公司总裁马斯克(31岁).当我们得意于「站在山上踢几块石头下去」或是「站在风口上的猪」的成功理论的时候,我们真的成功了吗?我们到底创造了什么?改变了什 ...