1. 對於表和索引空間的預估,能夠使用DBMS_SPACE包的CREATE_TABLE_COST和CREATE_INDEX_COST存儲過程,雖然沒有看這兩個存儲過程的實現,但猜想平均行長算法,會根據預計行數,作一些計算,字段定義算法,則會根據每一個字段的長度,和預計行數,作一些計算,其實和咱們手工根據這些算法,計算的方式相似,只是封裝起來,便於調用。算法
2. CREATE_TABLE_COST根據列字段定義預估,是比較準確的,根據平均行長,並不很準確。須要注意的是,這裏計算的是字段極限值,不會超過此值,但有可能實際用不了這些。sql
https://mp.weixin.qq.com/s?__biz=MzI2NzM1OTM4OA==&mid=2247484190&idx=1&sn=6135f4254ed6e84ac3ea161e3e5066a6&chksm=ea8146edddf6cffb7e089a36594321d4b5bbe28118f9f5d7a00b629b141ab27b45d35124b3ae&mpshare=1&scene=22&srcid=0705zFvXGOGQb52Sp8TaefxO#rd
oracle
oracle 提供了2種能夠預估將要建立的索引大小的辦法:app
① 利用包 Dbms_space.create_index_cost 直接獲得ide
② 利用11g新特性 Note raised when explain plan for create indexui
下邊分別舉例說明。this
[oracle@rhel6_lhr ~]$ sqlplus / as sysdbaspa
SQL*Plus: Release 11.2.0.3.0 Production on Fri Dec 26 15:58:06 2014orm
Copyright (c) 1982, 2011, Oracle. All rights reserved.server
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.
腳本:
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 是當實際建立索引時在表空間中實際佔用的字節數。
這是一個挺實用的小特性,在11g r2中使用explain plan for create index時Oracle會提示評估的索引大小(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.
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種辦法給出的索引評估大小與實際索引佔用空間差異不大,但這裏有個前提條件就是預估索引大小以前必須對錶進行分析過。
類型: 狀態: 上次主更新: 上次更新: |
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 |
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***
How TO Estimate The Size Of Tables and Indexes Before They Actually Gets Created And Populated In The Database , to Prepare Sufficient storage available .
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 :
Both Versions also Requires the following input :
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 :
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.
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 output of the procedure contains used_bytes and alloc_bytes :
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