oracle 如何预估将要创建的索引的大小

摘要:
createtabletest_index_sizeasselect*fromdb_objects;EXECDBMS_ STATS.collect _ table_ STATS(ownname=>'TEST_INDEX_SIZE');4 l_分配字节数;5开始6 dbms_space创建_索引_成本(ddl=>

一.1  oracle 如何预估将要创建的索引的大小

 

oracle 提供了2种可以预估将要创建的索引大小的办法:

①  利用包 Dbms_space.create_index_cost 直接得到

②  利用11g新特性 Note raised when explain plan for create index

下边分别举例说明。

一.2   环境说明

[oracle@rhel6_lhr ~]$ sqlplus / as sysdba

 

SQL*Plus: Release 11.2.0.3.0 Production on Fri Dec 26 15:58:06 2014

 

Copyright (c) 1982, 2011, Oracle.  All rights reserved.

 

 

Connected to:

Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - 64bit Production

With the Partitioning, Automatic Storage Management, OLAP, Data Mining

and Real Application Testing options

 

SQL> create table test_index_size as select * from dba_objects;

 

Table created.

 

SQL>  EXEC DBMS_STATS.gather_table_stats(ownname => 'SYS',tabname => 'TEST_INDEX_SIZE');

PL/SQL procedure successfully completed.

 

一.3  第一种 Dbms_space.create_index_cost

脚本:

declare

  l_index_ddl       varchar2(1000);

  l_used_bytes      number;

  l_allocated_bytes number;

begin

  dbms_space.create_index_cost(ddl         => 'create index idx_t on sys.test_index_size(object_id) ',

                               used_bytes  => l_used_bytes,

                               alloc_bytes => l_allocated_bytes);

  dbms_output.put_line('used= ' || l_used_bytes || 'bytes' ||

                       '     allocated= ' || l_allocated_bytes || 'bytes');

end;

/

 

实验:


SQL> set serveroutput on
SQL> declare

  2    l_index_ddlvarchar2(1000);

  3    l_used_bytesnumber;

  4    l_allocated_bytes number;

  5  begin

  6    dbms_space.create_index_cost(ddl => 'create index idx_t on sys.test_index_size(object_id) ',

  7      used_bytes=> l_used_bytes,

  8      alloc_bytes => l_allocated_bytes);

  9    dbms_output.put_line('used= ' || l_used_bytes || 'bytes' ||

10      '  allocated= ' || l_allocated_bytes || 'bytes');

11  end;

12  /

used= 383105bytes     allocated= 2097152bytes

PL/SQL procedure successfully completed.

 

SQL>


PL/SQL 过程已成功完成。
说明:  used_bytes  给出索引数据实际表现的字节数。
      allocated 是当实际创建索引时在表空间中实际占用的字节数。 

 

 

一.4  11g新特性:Note raised when explain plan for create index

 

这是一个挺实用的小特性,在11g r2中使用explain plan for create indexOracle会提示评估的索引大小(estimated index size)了:

SQL> set linesize 200 pagesize 1400;

SQL>  explain plan for create index idx_t on sys.test_index_size(object_id) ;

 

Explained.

 

SQL> select * from table(dbms_xplan.display());

 

PLAN_TABLE_OUTPUT

-------------------------------------------------------------------------------------

Plan hash value: 32582980

 

--------------------------------------------------------------------------------

| Id  | Operation         | Name  | Rows  | Bytes | Cost (%CPU)| Time    |

--------------------------------------------------------------------------------

|   0 | CREATE INDEX STATEMENT |       | 76621 |   374K|   35(1)| 00:00:05 |

|   1 |  INDEX BUILD NON UNIQUE| IDX_T |       |       |     |       |

|   2 |   SORT CREATE INDEX    |       | 76621 |   374K|     |       |

|   3 |    INDEX FAST FULL SCAN| IDX_T |       |       |     |       |

--------------------------------------------------------------------------------

 

Note

-----

   - estimated index size: 2097K bytes

 

14 rows selected.

 

 

一.5  创建真实索引

SQL> create index idx_t on sys.test_index_size(object_id) ;

 

Index created.

SQL>  analyze index IDX_T validate structure;

 

Index analyzed.

SQL> select bytes from dba_segments where segment_name='IDX_T';

 

     BYTES

----------

   2097152

 

可以看到2种办法给出的索引评估大小与实际索引占用空间差别不大但这里有个前提条件就是预估索引大小之前必须对表进行分析过。

 

免责声明:文章转载自《oracle 如何预估将要创建的索引的大小》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇Quartz使用mysql 安装失败 start service执行不下去下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

RobotFramework框架做自动化的过程中,遇到不能选择下拉框问题

1.描述 使用RobotFramework框架做自动化的过程中,遇到不能选择下拉框问题。 2.遇到的问题 关于下拉框的使用,网上都是这么使用的 select From List By Value Xpath=//* [@] vlaue # select From List By Value 关键字用于选择下拉框。 Xpath=//* [@] 定位下拉框;...

获取,标签里面属性的值的方法

获取,标签里面属性的值的方法,(下面是获取a标签里面属性source-data-lazy-img的值 ) soup2 = BeautifulSoup(span.encode('utf-8'), 'html.parser')for img2 in soup2.find_all('img',{"source-data-lazy-img":True}):  pr...

Mybatis的@Options注解

mybatis的@Options注解能够设置缓存时间,能够为对象生成自增的key 第一个使用场景: 有一个表 CREATE TABLE instance ( instance_id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '主键id', infos VARCHAR(2048) NOT NULL D...

Linux命令行下批量重命名文件名为数字索引编号(0~N.xxx)的方法

在处理一些数据集的时候,我们经常会碰到数据集的文件名是按时间戳(time stamp),或者其他方式命名的文件,如: 12345679.jpg, sunshine.txt 而我们在编写程序时,往往希望读入的这些数据集的文件名是一种更简洁的形式,比如按照索引(index)方式:1.png,2.png,3.png...,那么如何批量重命名一个文件夹下的文件名呢...

WordPress标题函数wp_title()详解

在wp_title()中通常是在页面头部的title元素中。当wp_title()在主页主循环(loop)外时,可以用在模板的任何地方。 用法: 1 <?php wp_title( $sep, $echo, $seplocation ); ?> 参数: $sep (字符串)(可选)显示在文章标题前后的文字信息(如分隔符)。 默认情况下(若分隔...

tree,js的简单实现内容显示

tree,js的简单实现内容显示,对模型的gltf进行引用内容显示 加载所用资源https://github.com/mrdoob/three.js 整理后代码{ <!DOCTYPE html> <html> <head> <meta charset=utf-8> &l...