Oracle Number类型的讨论
2006-08-18 18:19
477 查看
Specify scale for NUMBERs
Many developers do not specify a precision for NUMBER columns. This may be in order to minimize the work that would be needed to increase that precision should the application's requirements change, but it is often just a matter of habit. And although it is possible using the undocumented (*,scale) syntax to specify a scale without a precision, this is not normally done. If no precision and no scale are specified for an Oracle NUMBER column then it can contain arbitrary floating point numbers, and floating point numbers can be large.In most cases this has no performance impact because the columns in fact only contain integers and the number of bytes required to store an integer is no more than one plus half the number of significant digits (more). However, large floating point values may be stored in these columns if their values are sometimes computed using floating point arithmetic. In particular, when saving the results of floating point arithmetic to a NUMBER column, if there is no scale, or if the scale is further to the right than the least significant digit of precision in the result, then rounding will not be performed (more) and thus the full storage precision will be used.
Here is an example. We will create a table with two NUMBER columns. The first will have no scale. The other will have a scale of 4 digits. Note the difference in storage space required when we then insert the same value to these two columns. Although the values are the same, the column without any scale takes 21 bytes to store the number, whereas the column with a small scale takes just 2 bytes to store the same number.
SQL> create table numbers (n1 number, n2 number(10,4)); Table created. SQL> insert into numbers values (3*(1/3), 3*(1/3)); 1 row created. SQL> select * from numbers; N1 N2 ---------- ---------- 1 1 1 row selected. SQL> select vsize(n1), vsize(n2) from numbers; VSIZE(N1) VSIZE(N2) ---------- ---------- 21 2 1 row selected. |
SQL> create table numbers (n1 number, n2 number(*,38), n3 number(*,37)); Table created. SQL> insert into numbers values (log(2, 4), log(2, 4), log(2, 4)); 1 row created. SQL> select * from numbers; N1 N2 N3 ---------- ---------- ---------- 2 2 2 1 row selected. SQL> select vsize(n1), vsize(n2), vsize(n3) from numbers; VSIZE(N1) VSIZE(N2) VSIZE(N3) ---------- ---------- ---------- 21 21 2 1 row selected. |
Because most Oracle NUMBER columns just store integers and are never subject to floating point arithmetic, this waste of space is not very widespread despite that developers often fail to specify the scale of NUMBERs. However, where it does occur it reduces table data density which in turn increases table scan I/O, uses cache memory less effectively, and reduces index efficiency. Therefore, it is good to be in the habit of always specifying at least a scale for real NUMBERs, if not a precision as well. For integers, if a precision is specified then a scale of 0 is implied, otherwise the scale should be set to zero explicitly using the NUMBER(*,0) datatype specification or a synonymous ANSI datatype specification.
Our unscaled_numbers.sql script can be used to check an existing database for columns that are wasting space because of this issue and therefore might be impacting performance. By default it identifies NUMBER columns for which no scale has been specified and which have an average data length of more than 9 bytes (more than 15 digits).
相关文章推荐
- oracle 的number数据类型
- oracle中的number类型用法及举例
- 关于oracle number类型超过16位的显示问题
- ORACLE中的Number数据类型
- Oracle数据类型之number
- ORACLE NUMBER类型详解
- Oracle数据类型NUMBER
- 关于Oracle Number类型
- Oracle将number类型的时间值转换成时间格式的字符串
- oracle中扩充number类型
- Oracle数据类型之number
- oracle number类型,对于Java来说为什么 string 类型也是可以插入的,有时候又不行的解释
- 修改oracle某张表的ID类型varchar2为number,并保留原有数据(因为可能其他表与该表ID关联)
- oracle中number类型说明
- oracle的number类型默认长度是多少?
- Oracle中数据类型number(m,n)
- oracle中的number类型用法及举例
- Oracle NUMBER类型细讲
- Oracle数据类型之number