定义更长长度的 VARCHAR2 列的影响
当值不超过 10 字节时,在 Oracle 中使用 VARCHAR2(1000)
而不是 VARCHAR2(10)
定义列会产生什么影响?
该列是否只占用存储值真正需要的空间,或者这会对表空间/索引的大小/性能产生负面影响?
What are the effects of defining a column with VARCHAR2(1000)
instead of VARCHAR2(10)
in Oracle, when the values are not longer than 10 Byte?
Does the column only take the space really necessary to store the values, or would that have any negative impacts on the size/performance of tablespaces/indexes?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
答案取决于您讨论的是数据库表中的列还是 PL/SQL 程序中的变量。
数据库列
使用的存储量与存储的数据大小成正比。
PL/SQL 变量
如果声明变量的大小为 1 到 4000 (11g+) / 1999(10g 或更早版本),则将分配最大长度的内存(即 VARCHAR2(100) 将需要至少 100 字节内存)。
如果变量声明的大小为 4001 (11g+) / 2000(10g 或更早)或更大,则将根据存储数据的大小分配内存。 (一个有趣的附带问题是,如果变量的值发生更改,内存大小如何调整 - 它是否会重新分配具有新大小的另一个缓冲区?)
10g 的参考:PL/SQL 数据类型
11g 参考:避免 PL/SQL 代码中的内存开销< /a>
The answer depends on whether you're talking about a column in a database table, or a variable in a PL/SQL program.
Database column
The amount of storage used is proportionate to the size of the data stored.
PL/SQL variable
If the variable is declared with a size 1 to 4000 (11g+) / 1999 (10g or earlier), memory will be allocated for the maximum length (i.e. VARCHAR2(100) will require at least 100 bytes of memory).
If the variable is declared with a size 4001 (11g+) / 2000 (10g or earlier) or greater, memory will be allocated according to the size of the data stored. (an interesting side question would be, if the variable's value is changed, how is the memory resized - does it reallocate another buffer with the new size?)
Reference for 10g: PL/SQL Datatypes
Reference for 11g: Avoiding Memory Overhead in PL/SQL Code
在数据库中,没有区别。
VARCHAR2
以可变长度存储,声明的长度只是一个限制。但是,如果某些客户端应用程序发现列定义为
VARCHAR2(1000)
,则会为每列保留1000
字节,而不是10
In the database, there is no difference.
VARCHAR2
are stored with variable length, the declared length is only a limitation.However, some client applications will reserve
1000
bytes per column instead of10
if they see that the column is defined asVARCHAR2(1000)
将列大小定义为您准备处理的最大长度。
对于临时表,我将文件加载到数据库中,我可以使用 VARCHAR2(4000)。然后,一旦所有数据都在数据库中,我就可以执行所需的任何验证(数据类型、数据长度、最小/最大值、可接受的字符...),并将有效值传递到具有适当定义的列中/限制。
我可以利用 DML 错误日志记录这样,不符合定义/约束的数据就会被发送到拒绝表,而无需任何棘手的逐行编码。
如果你有一个 VARCHAR2(1000) 那么,在某些时候你会得到比你预期更长的数据(例如,你可能会得到一个 10 个字符的字符串,但它有 14 个字节,因为有些字符是多字节字符集值。)
Define the column size as the maximum length you are prepared to deal with.
For a staging table, where I'm loading a file into the database, I may use VARCHAR2(4000). Then once all the data is in the database, I can do whatever validations I need to (data types, data lengths, min/max values, acceptable characters...) and have the valid values passed into a column with the appropriate definition/constraints.
I can leverage DML error logging so that data that doesn't fit the definition/constraints gets pumped off to a rejection table without any tricky row-by-row coding.
If you have a VARCHAR2(1000) then, at some point you WILL get data put in there that is longer than you expect (eg you may get a string of 10 characters but which has 14 bytes because some characters are multi-byte character set values.)