用 VARCHAR2(1000)
定义列有什么影响而不是 VARCHAR2(10)
在 Oracle 中,当值不超过 10 字节时?
该列是否只占用存储值真正需要的空间,还是会对表空间/索引的大小/性能产生负面影响?
最佳答案
答案取决于您是在谈论数据库表中的列还是 PL/SQL 程序中的变量。
数据库栏目
使用的存储量与存储的数据大小成正比。
PL/SQL 变量
如果变量声明的大小为 1 到 4000 (11g+)/1999(10g 或更早版本),则内存将分配为最大长度(即 VARCHAR2(100) 将需要至少 100 字节的内存)。
如果变量声明的大小为 4001 (11g+)/2000(10g 或更早版本)或更大,则将根据存储的数据大小分配内存。 (一个有趣的附带问题是,如果变量的值发生变化,内存是如何调整大小的——它是否会重新分配另一个具有新大小的缓冲区?)
10g 引用:PL/SQL Datatypes
Small VARCHAR2 variables are optimized for performance, and larger ones are optimized for efficient memory use. The cutoff point is 2000 bytes. For a VARCHAR2 that is 2000 bytes or longer, PL/SQL dynamically allocates only enough memory to hold the actual value. For a VARCHAR2 variable that is shorter than 2000 bytes, PL/SQL preallocates the full declared length of the variable. For example, if you assign the same 500-byte value to a VARCHAR2(2000 BYTE) variable and to a VARCHAR2(1999 BYTE) variable, the former takes up 500 bytes and the latter takes up 1999 bytes.
11g 引用:Avoiding Memory Overhead in PL/SQL Code
Specify a size of more than 4000 characters for the VARCHAR2 variable; PL/SQL waits until you assign the variable, then only allocates as much storage as needed
关于sql - 定义更大长度的 VARCHAR2 列的影响,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1882073/