[English]
作者:
fuyuncat
来源:
www.HelloDBA.com
有些同学在估算数据的物理存储大小时会使用length函数,实际上这不正确,真正用于获取数据物理字节数的函数是sys_op_opnsize (10g)。length函数获取到的是数据的逻辑长度,而非物理长度。例如数字,逻辑长度是按照10进制得到的,而其物理存储是按照16进制。
SQL代码
- HELLODBA.COM>insert into demo.tt values (10);
- 1 row created.
- HELLODBA.COM>insert into demo.tt values (100);
- 1 row created.
- HELLODBA.COM>insert into demo.tt values (999);
- 1 row created.
- HELLODBA.COM>insert into demo.tt values (1000);
- 1 row created.
- HELLODBA.COM>insert into demo.tt values (9999);
- 1 row created.
- HELLODBA.COM>select sys_op_opnsize(x), length(x),x,dump(x) from demo.tt;
- SYS_OP_OPNSIZE(X) LENGTH(X) X DUMP(X)
- ----------------- ---------- ---------- ---------------------------
- 2 2 10 Typ=2 Len=2: 193,11
- 2 3 100 Typ=2 Len=2: 194,2
- 3 3 999 Typ=2 Len=3: 194,10,100
- 2 4 1000 Typ=2 Len=2: 194,11
- 3 4 9999 Typ=2 Len=3: 194,100,100
- 2 1 1 Typ=2 Len=2: 193,2
- 2 1 1 Typ=2 Len=2: 193,2
- 7 rows selected.
通过对比dump出来结果,很容易发现SYS_OP_OPNSIZE和LENGTH的区别。对于字符数据,则其物理存储长度与字符集相关:
SQL代码
- HELLODBA.COM>create table ts (a varchar2(10), b nvarchar2(10));
- Table created.
- HELLODBA.COM>insert into ts values ('A', 'A');
- 1 row created.
- HELLODBA.COM>insert into ts values ('AA', 'AA');
- 1 row created.
- HELLODBA.COM>insert into ts values ('AAA', 'AAA');
- 1 row created.
- HELLODBA.COM>select a,sys_op_opnsize(a) v, length(a),dump(a),b,sys_op_opnsize(b) v, length(b),dump(b
- ) from demo.ts;
- A V LENGTH(A) DUMP(A) B V LENGTH(B) DUMP(B)
- ---------- ---------- ---------- ----------------------------- ---------- ---------- ---------- -----------------------------
- A 1 1 Typ=1 Len=1: 65 A 2 1 Typ=1 Len=2: 0,65
- AA 2 2 Typ=1 Len=2: 65,65 AA 4 2 Typ=1 Len=4: 0,65,0,65
- AAA 3 3 Typ=1 Len=3: 65,65,65 AAA 6 3 Typ=1 Len=6: 0,65,0,65,0,65
--- Fuyuncat Mark ---