本文主要对文件系统存储与数据库存储作介绍,为了一些经常对存储空间评估人士,提供比较准确评估依据.....
下面所有描述都是在字符字为:
SQL> select userenv('language') from dual;
USERENV('LANGUAGE')
----------------------------------------------------
AMERICAN_AMERICA.ZHS16GBK
[@[email protected]]
一、文件系统存储
1、1K=1024B(BYTE)=1024*8b(bit);
2、一个字符=1个字节(1B);(备注:unix只有换行(0X0A),没回车,一个换行符占用一个字符;windows既有换行又有回车。换行符和回车符各占用一个字符)
3、一个中文=2个字节 --(如果是32位编码的字符集,占用四个字节)
二、数据库存储(oracle)
DUMP函数的输出格式类似:
类型 ,符号/指数位 [数字1,数字2,数字3,......,数字20]
各位的含义如下:
1.类型: Number型,Type=2 (类型代码可以从Oracle的文档上查到,见结尾)
2.长度:指存储的字节数
3.符号/指数位
在存储上,Oracle对正数和负数分别进行存储转换:
正数:加1存储(为了避免Null),在实际算法中就是要减1,必须>128
负数:被101减,如果总长度小于21个字节,最后加一个102(是为了排序的需要),必须<128
1、NUMBER:正数--从右第一个非零数起,二个字符为计算单位,二个字符=2个字节,如果最后单字符以二个字节计算,总数+1就是占用存储。
负数--从右第一个非零数起,二个字符为计算单位,二个字符=2个字节,如果最后单字符以二个字节计算,总数+2就是占用存储。
SQL> select id,dump(id) dp from test2 order by id;
ID DP
---------- --------------------------------------------------
1 Typ=2 Len=2: 193,2
12 Typ=2 Len=2: 193,13
123 Typ=2 Len=3: 194,2,24
1234 Typ=2 Len=3: 194,13,35
12345 Typ=2 Len=4: 195,2,24,46
123456 Typ=2 Len=4: 195,13,35,57
1234567 Typ=2 Len=5: 196,2,24,46,68
123456789 Typ=2 Len=6: 197,2,24,46,68,90
SQL> select id ,dump(id) dp from test4;
ID DP
---------- ----------------------------------------------
0 Typ=2 Len=1: 128
10 Typ=2 Len=2: 193,11
100 Typ=2 Len=2: 194,2
1000 Typ=2 Len=2: 194,11
10001 Typ=2 Len=4: 195,2,1,2
SQL> select id,dump(id) dp from test2 order by id desc
ID DP
---------- ---------------------------------------
-1 Typ=2 Len=3: 62,100,102
-12 Typ=2 Len=3: 62,89,102
-123 Typ=2 Len=4: 61,100,78,102
-1234 Typ=2 Len=4: 61,89,67,102
-12345 Typ=2 Len=5: 60,100,78,56,102
-123456 Typ=2 Len=5: 60,89,67,45,102
-1234567 Typ=2 Len=6: 59,100,78,56,34,102
-123456789 Typ=2 Len=7: 58,100,78,56,34,12,102
SQL> select id ,dump(id) dp from test4;
ID DP
---------- ---------------------------------------
0 Typ=2 Len=1: 128
-10 Typ=2 Len=3: 62,91,102
-100 Typ=2 Len=3: 61,100,102
-1000 Typ=2 Len=3: 61,91,102
-10001 Typ=2 Len=5: 60,100,101,100,102
2、VARCHAR2:变长,一个字符占用一字节
SQL> select name,dump(name) dp from revenco;
NAME DP
------------------------ ----------------------------------------------
1 Typ=1 Len=1: 49
12 Typ=1 Len=2: 49,50
123 Typ=1 Len=3: 49,50,51
1234 Typ=1 Len=4: 49,50,51,52
12345 Typ=1 Len=5: 49,50,51,52,53
123456789 Typ=1 Len=9: 49,50,51,52,53,54,55,56,57
3、CHAR:定长
SQL> select name,dump(name) dp from revenco2;
NAME DP
---------- ----------------------------------------------
1 Typ=96 Len=10: 49,32,32,32,32,32,32,32,32,32
1234 Typ=96 Len=10: 49,50,51,52,32,32,32,32,32,32
1234567890 Typ=96 Len=10: 49,50,51,52,53,54,55,56,57,48
4、DATE:定长=7个字节
SQL> select date_time,dump(date_time) dp from revenco3;
DATE_TIME DP
------------------- ----------------------------------------------
2009-11-06 11:57:18 Typ=12 Len=7: 120,109,11,6,12,58,19
2009-11-06 11:57:56 Typ=12 Len=7: 120,109,11,6,12,58,57
5、TIMESTAMP:变长(7或者11字节)
SQL> select to_char(time_stamp,'YYYY-MM-DD HH24:MI:SS.FF') TIME ,dump(time_stamp) dp from revenco4;
TIME DP
------------------------------------------------------------ --------------------------------------------------------
2009-11-06 12:16:16.000000 Typ=180 Len=7: 120,109,11,6,13,17,17
2009-11-06 12:16:16.123456 Typ=180 Len=11: 120,109,11,6,13,17,17,7,91,202,0
2009-11-06 12:17:12.120000 Typ=180 Len=11: 120,109,11,6,13,18,13,7,39,14,0
2009-11-06 12:17:12.000056 Typ=180 Len=11: 120,109,11,6,13,18,13,0,0,218,192
6、表占表空间
CREATE TABLE "ETL"."test5"
( "ID" NUMBER
) PCTFREE 10 PCTUSED 40 INITRANS 1 MAXTRANS 255 NOCOMPRESS LOGGING
STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT)
TABLESPACE "USERS"
SQL> SELECT COUNT(*) FROM test5;
COUNT(*)
----------
SQL> SELECT SEGMENT_NAME,BYTES FROM USER_SEGMENTS WHERE SEGMENT_NAME='TEST5';
SEGMENT_NAME BYTES
-------------------------- ----------
TEST5 65536
备注:为test5的记录数据为0,还有占用65536字节空间。因为oracle数据库存储最小单元为数据块,而表在创建时就进行存储分配空间。不管
表是否存在记录数。所以不能想当然计算行平均大小=BYTES/COUNT(*),但是oracle也提供计算行的平均大小。即:user_tables.avg_row_len(需
要对表进行统计收集才有信息的)。
7、每行平均字节数AVG_ROW_LEN
(1)通过DBA_TABLES的AVG_ROW_LEN可以查看TABLE每一行平均字节数。前提是对TABLE进行了统计信息收集。(关于统计信息收集请查看myblog《oracle统计信息收集详解》)
(2)oracle如何计算AVG_ROW_LEN,下面举例说明:
SQL> select * from test3;
ID NAME DATE_TIME
---------- ------------ -------------------
1234567 12345678 2009-11-13 11:21:21
1234567 12345678 2009-11-13 11:21:22
1234567 12345678 2009-11-13 11:21:23
1234567 12345678 2009-11-13 11:21:24
1234567 12345678 2009-11-13 11:21:24
SQL> l
1* select dump(id) dp,dump(name) dp,dump(date_time) dp from test3
SQL> /
DP DP DP
------------------------ ------------------------ ------------------------
Typ=2 Len=5: 196,2,24,46 Typ=1 Len=8: 49,50,51,52 Typ=12 Len=7: 120,109,11
Typ=2 Len=5: 196,2,24,46 Typ=1 Len=8: 49,50,51,52 Typ=12 Len=7: 120,109,11
Typ=2 Len=5: 196,2,24,46 Typ=1 Len=8: 49,50,51,52 Typ=12 Len=7: 120,109,11
Typ=2 Len=5: 196,2,24,46 Typ=1 Len=8: 49,50,51,52 Typ=12 Len=7: 120,109,11
Typ=2 Len=5: 196,2,24,46 Typ=1 Len=8: 49,50,51,52 Typ=12 Len=7: 120,109,11
SQL> select table_name,num_rows,blocks,empty_blocks,avg_space,chain_cnt,avg_row_len,last_analyzed from dba_tables where table_name='TEST3' and owner='ETL';
TABLE_NAME NUM_ROWS BLOCKS EMPTY_BLOCKS AVG_SPACE CHAIN_CNT AVG_ROW_LEN LAST_ANALYZED
-------------- ---------- ---------- ------------ ---------- ---------- ----------- -------------------
TEST3 5 5 0 0 0 23 2009-11-13 11:21:41
其中:AVG_ROW_LEN=((5+8+7)+3)*5/5=23
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/22630256/viewspace-1028585/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/22630256/viewspace-1028585/