定义更长长度的 VARCHAR2 列的影响

发布于 2024-08-14 10:06:34 字数 153 浏览 5 评论 0原文

当值不超过 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

久隐师 2024-08-21 10:06:34

答案取决于您讨论的是数据库表中的列还是 PL/SQL 程序中的变量。

数据库列

使用的存储量与存储的数据大小成正比。

PL/SQL 变量

如果声明变量的大小为 1 到 4000 (11g+) / 1999(10g 或更早版本),则将分配最大长度的内存(即 VARCHAR2(100) 将需要至少 100 字节内存)。

如果变量声明的大小为 4001 (11g+) / 2000(10g 或更早)或更大,则将根据存储数据的大小分配内存。 (一个有趣的附带问题是,如果变量的值发生更改,内存大小如何调整 - 它是否会重新分配具有新大小的另一个缓冲区?)

10g 的参考:PL/SQL 数据类型

较小的 VARCHAR2 变量针对性能进行了优化,较大的变量针对高效内存使用进行了优化。截止点是 2000 字节。对于 2000 字节或更长的 VARCHAR2,PL/SQL 动态分配仅足够的内存来保存实际值。对于短于 2000 字节的 VARCHAR2 变量,PL/SQL 会预分配该变量的完整声明长度。例如,如果将相同的 500 字节值分配给 VARCHAR2(2000 BYTE) 变量和 VARCHAR2(1999 BYTE) 变量,则前者占用 500 字节,后者占用 1999 字节。

11g 参考:避免 PL/SQL 代码中的内存开销< /a>

指定 VARCHAR2 变量的大小超过 4000 个字符; PL/SQL 等待您分配变量,然后仅分配所需的存储量

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

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.

Reference for 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

不爱素颜 2024-08-21 10:06:34

在数据库中,没有区别。 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 of 10 if they see that the column is defined as VARCHAR2(1000)

献世佛 2024-08-21 10:06:34

将列大小定义为您准备处理的最大长度。
对于临时表,我将文件加载到数据库中,我可以使用 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.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文