Oracle如何預估將要建立的索引和表的大小

lhrbest發表於2014-12-26


Oracle如何預估將要建立的索引和表的大小



1. 對於表和索引空間的預估,可以使用DBMS_SPACE包的CREATE_TABLE_COST和CREATE_INDEX_COST儲存過程,雖然沒有看這兩個儲存過程的實現,但猜測平均行長演算法,會根據預計行數,做一些計算,欄位定義演算法,則會根據每個欄位的長度,和預計行數,做一些計算,其實和我們手工根據這些演算法,計算的方式類似,只是封裝起來,便於呼叫。

2. CREATE_TABLE_COST根據列欄位定義預估,是比較準確的,根據平均行長,並不很準確。需要注意的是,這裡計算的是欄位極限值,不會超過此值,但有可能實際用不了這些。


https://mp.weixin.qq.com/s?__biz=MzI2NzM1OTM4OA==&mid=2247484190&idx=1&sn=6135f4254ed6e84ac3ea161e3e5066a6&chksm=ea8146edddf6cffb7e089a36594321d4b5bbe28118f9f5d7a00b629b141ab27b45d35124b3ae&mpshare=1&scene=22&srcid=0705zFvXGOGQb52Sp8TaefxO#rd






oracle 提供了2種可以預估將要建立的索引大小的辦法:


①  利用包 Dbms_space.create_index_cost 直接得到

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

下邊分別舉例說明。

1   環境說明

[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.

 

2  第一種 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_ddl varchar2(1000);

  3    l_used_bytes number;

  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 是當實際建立索引時在表空間中實際佔用的位元組數。 

 

 

3  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| 350(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.

 

 

4  建立真實索引

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種辦法給出的索引評估大小與實際索引佔用空間差別不大但這裡有個前提條件就是預估索引大小之前必須對錶進行分析過。





 How to Estimate the Size of Tables and Indexes Before Being Created and Populated in the Database? (文件 ID 1585326.1)

型別:
狀態:
上次主更新:
上次更新:
HOWTO
PUBLISHED
2016-5-23
2016-5-23



In this Document

Goal
Solution
  Estimate The Size Of Tables
  Example for using Version 1:
  Example for using Version 2:
  Estimate Table Size Using Enterprise Manager
 
Estimate The Size Of Indexes
  Estimate Index Size example :
References


APPLIES TO:

Oracle Database - Enterprise Edition - Version 10.1.0.3 to 12.1.0.2 [Release 10.1 to 12.1]
Information in this document applies to any platform.
***Checked for relevance on 23-May-2016***

GOAL

How TO Estimate The Size Of Tables and Indexes Before They Actually Gets Created And Populated In The Database , to Prepare Sufficient storage available .

 

SOLUTION

Estimate The Size Of Tables


The CREATE_TABLE_COST procedure of the DBMS_SPACE package helps to estimate the size of the table segment , 

This procedure is used in capacity planning to determine the size of the table given various attributes , The CREATE_TABLE_COST procedure has two versions :

  • The first version takes the average row size of the table as argument and outputs the table size.
  • The second version takes the column information of the table as argument and outputs the table size.


Both Versions also Requires the following input :

  •  the expected number of rows
  •  pct_free setting for the table
  •  the tablespace name where the table would be created.

 

Version 1
=========
DBMS_SPACE.CREATE_TABLE_COST (
   tablespace_name    IN VARCHAR2,
   avg_row_size       IN NUMBER,
   row_count          IN NUMBER,
   pct_free           IN NUMBER,
   used_bytes         OUT NUMBER,
   alloc_bytes        OUT NUMBER);

Version 2
=========
DBMS_SPACE.CREATE_TABLE_COST (
   tablespace_name    IN VARCHAR2,
   colinfos           IN CREATE_TABLE_COST_COLUMNS,
   row_count          IN NUMBER,
   pct_free           IN NUMBER,
   used_bytes         OUT NUMBER,
   alloc_bytes        OUT NUMBER);


The output of the procedure contains used_bytes and alloc_bytes :

  •  The used_bytes : represent the actual bytes used by the data. This includes the overhead due to the block metadata, pctfree etc.
  •  The alloc_bytes : represent the size of the table segment when it is created in the tablespace. This takes into account, the size of the extents in the tablespace and tablespace extent management properties.

Example for using Version 1:


This methode requires creating the table first with some sample data to calculate the average row size of the table ,

create table test (a NUMBER (10) , b VARCHAR2 (30) ,c VARCHAR2 (30), d date ) tablespace USERS pctfree 10;

INSERT INTO test VALUES (9999999999,'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' ,'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa',sysdate);
INSERT INTO test VALUES (9999999,'aaaaaaaaaaaaaaaaaaa' ,'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa',sysdate);
INSERT INTO test VALUES (999999,'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' ,'aaaaaaaaaaaaaaaaaa',sysdate);

exec DBMS_STATS.GATHER_TABLE_STATS (ownname=>'hr',tabname=>'test',estimate_percent=>100,block_sample=>true,method_opt=>'FOR ALL COLUMNS size 254');

select AVG_ROW_LEN from user_tables where TABLE_NAME='TEST';

AVG_ROW_LEN
-----------
         68

Drop table test;

set serveroutput on 

DECLARE 
 ub NUMBER; 
 ab NUMBER; 
BEGIN 
  DBMS_SPACE.CREATE_TABLE_COST('USERS',68,100000,10,ub,ab); 
  DBMS_OUTPUT.PUT_LINE('Used Bytes      = ' || TO_CHAR(ub)); 
  DBMS_OUTPUT.PUT_LINE('Allocated Bytes = ' || TO_CHAR(ab)); 
END; 


Used Bytes      =  8036352
Allocated Bytes =  8388608


which is around 8 MB , now lets create and populate the actual table.

create table test (a NUMBER (10) , b VARCHAR2 (30) ,c VARCHAR2 (30), d date ) tablespace USERS pctfree 10;

BEGIN
  FOR i IN 1..100000 LOOP
  INSERT INTO test VALUES (i,'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' ,'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa',sysdate);
  END LOOP;
 END;
/

select BYTES,SEGMENT_TYPE from user_segments where SEGMENT_NAME='TEST';

     BYTES SEGMENT_TYPE
---------- ------------------
  9437184 TABLE


which is around 9 MB

Example for using Version 2:


This example estimate a Table size with one column NUMBER (10) , two columns VARCHAR2 (30) and one column date , with pct_free=10 and row_count=100000 :

set serveroutput on 

DECLARE 
 ub NUMBER; 
 ab NUMBER; 
 cl sys.create_table_cost_columns; 
BEGIN 
  cl := sys.create_table_cost_columns( sys.create_table_cost_colinfo('NUMBER',10), 
        sys.create_table_cost_colinfo('CHAR',30), 
        sys.create_table_cost_colinfo('CHAR',30), 
        sys.create_table_cost_colinfo('DATE',NULL)); 

  DBMS_SPACE.CREATE_TABLE_COST('USERS',cl,100000,10,ub,ab); 

  DBMS_OUTPUT.PUT_LINE('Used Bytes      = ' || TO_CHAR(ub)); 
  DBMS_OUTPUT.PUT_LINE('Allocated Bytes = ' || TO_CHAR(ab)); 
END; 


Used Bytes      = 9314304
Allocated Bytes = 9437184


which is around 9 MB , now lets create and populate the actual table.  

Note : we changed VARCHAR2 to CHAR to get the maximum possible estimation .

  

create table test (a NUMBER (10) , b VARCHAR2 (30) ,c VARCHAR2 (30), d date ) tablespace USERS pctfree 10;

BEGIN
  FOR i IN 1..100000 LOOP
  INSERT INTO test VALUES (i,'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' ,'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa',sysdate);
  END LOOP;
 END;
/

select BYTES,SEGMENT_TYPE from user_segments where SEGMENT_NAME='TEST';

     BYTES SEGMENT_TYPE
---------- ------------------
  9437184 TABLE


which is around 9 MB 

You may notice that version 2 of The CREATE_TABLE_COST procedure is more accurate than version 1 , which is expected because version 2 estimates the size based on the table structure which includes the column data types and length , plus the storage attributes .

Estimate Table Size Using Enterprise Manager

 

During table creation with Enterprise Manager, you can estimate the size of the table. This action enables you to determine whether you have sufficient room in your database or on the available disks to store the table. If you do not have room, then you can still create the table but not populate it until you have obtained the necessary storage.

To estimate the table size:

   1.      In the Database Objects section of the Administration page, click Tables.

      The Tables page appears.

   2.      Click Create.

      The Create Table: Table Organization page appears.

   3.      Select Standard, Heap Organized and click Continue.

      The Create Table page appears.

   4.      Enter the table and schema names as well as your column names and data types, then click Estimate Table Size.

      The Estimate Table Size page appears.

   5.      In Projected Row Count, enter the projected number of rows in the table and click Estimate Table Size

      Enterprise Manager returns its estimate in MB.


Estimate The Size Of Indexes



The CREATE_INDEX_COST Procedure of the DBMS_SPACE package helps to estimate the size of creating an index on an existing table.

The input is the DDL statement that will be used to create the index. The procedure will output the storage required to create the index.

Usage Notes :

  •     The table on which the index is created must already exist.
  •     The computation of the index size depends on statistics gathered on the segment.
  •     It is imperative that the table must have been analyzed recently.
  •     In the absence of correct statistics, the results may be inaccurate, although the procedure will not raise any errors.



The output of the procedure contains used_bytes and alloc_bytes :

  •  The used_bytes : how much space is for the index data
  •  The alloc_bytes : how much space is allocated within the tablespace for the index segment.

Estimate Index Size example :

 

create table test (a NUMBER (10) , b VARCHAR2 (30) ,c VARCHAR2 (30), d date ) tablespace USERS pctfree 10;

BEGIN
  FOR i IN 1..100000 LOOP
  INSERT INTO test VALUES (9999999999,'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' ,'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa',sysdate);
  END LOOP;
 END;
/

exec DBMS_STATS.GATHER_TABLE_STATS (ownname=>'hr',tabname=>'test',estimate_percent=>100,block_sample=>true,method_opt=>'FOR ALL COLUMNS size 254');

set serveroutput on 

declare
   l_used_bytes number;
   l_alloc_bytes number;
begin
   dbms_space.create_index_cost (
      ddl => 'create index test_indx on test (a,b) tablespace users',
      used_bytes => l_used_bytes,
      alloc_bytes => l_alloc_bytes
   );
   dbms_output.put_line ('Used Bytes      = '||l_used_bytes);
   dbms_output.put_line ('Allocated Bytes = '||l_alloc_bytes);
end;
/   

Used Bytes      = 3800000
Allocated Bytes = 6291456


which is around 6 MB , now lets create the actual index.

create index test_indx on test (a,b) tablespace users

select BYTES,SEGMENT_TYPE from user_segments where SEGMENT_NAME='TEST_INDX';

     BYTES SEGMENT_TYPE
---------- ------------------
   6291456 INDEX


which is around 6 MB 



Refer To :
==========
https://docs.oracle.com/cd/E11882_01/appdev.112/e40758/d_space.htm#sthref8857
https://docs.oracle.com/cd/E11882_01/appdev.112/e40758/d_space.htm#sthref8850

REFERENCES

http://docs.oracle.com/cd/E11882_01/appdev.112/e40758/d_space.htm#sthref8850 
http://docs.oracle.com/cd/E11882_01/appdev.112/e40758/d_space.htm#sthref8857 






About Me

...............................................................................................................................

● 本文作者:小麥苗,只專注於資料庫的技術,更注重技術的運用

● 本文在itpub(http://blog.itpub.net/26736162)、部落格園(http://www.cnblogs.com/lhrbest)和個人微信公眾號(xiaomaimiaolhr)上有同步更新

● 本文itpub地址:http://blog.itpub.net/26736162/abstract/1/

● 本文部落格園地址:http://www.cnblogs.com/lhrbest

● 本文pdf版及小麥苗雲盤地址:http://blog.itpub.net/26736162/viewspace-1624453/

● 資料庫筆試面試題庫及解答:http://blog.itpub.net/26736162/viewspace-2134706/

● QQ群:230161599     微信群:私聊

● 聯絡我請加QQ好友(646634621),註明新增緣由

● 於 2017-07-01 09:00 ~ 2017-07-31 22:00 在魔都完成

● 文章內容來源於小麥苗的學習筆記,部分整理自網路,若有侵權或不當之處還請諒解

● 版權所有,歡迎分享本文,轉載請保留出處

...............................................................................................................................

拿起手機使用微信客戶端掃描下邊的左邊圖片來關注小麥苗的微信公眾號:xiaomaimiaolhr,掃描右邊的二維碼加入小麥苗的QQ群,學習最實用的資料庫技術。

Oracle如何預估將要建立的索引和表的大小
DBA筆試面試講解
歡迎與我聯絡

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/26736162/viewspace-1381160/,如需轉載,請註明出處,否則將追究法律責任。

相關文章