数据库 – 为什么指定字符变化类型的长度

前端之家收集整理的这篇文章主要介绍了数据库 – 为什么指定字符变化类型的长度前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
参考 Character Types的Postgres文档,我不清楚指定字符变化(varchar)类型的长度.

假设:

>字符串的长度与应用程序无关.
>你不在乎有人把最大的大小放在数据库
>你有无限的硬盘空间

它提到:

The storage requirement for a short string (up to 126 bytes) is 1 byte
plus the actual string,which includes the space padding in the case
of character. Longer strings have 4 bytes of overhead instead of 1.
Long strings are compressed by the system automatically,so the
physical requirement on disk might be less. Very long values are also
stored in background tables so that they do not interfere with rapid
access to shorter column values. In any case,the longest possible
character string that can be stored is about 1 GB. (The maximum value
that will be allowed for n in the data type declaration is less than
that. It wouldn’t be useful to change this because with multibyte
character encodings the number of characters and bytes can be quite
different.

这涉及字符串的大小,而不是字段的大小(也就是说,它听起来总是压缩大的varchar字段中的大字符串,而不是一个大的varchar字段中的一个小字符串)

我问这个问题,因为它更容易(和懒惰)指定一个更大的大小,所以你不必担心字符串太大.例如,如果我为地名指定了varchar(50),我将获得具有更多字符的位置(例如Llanfairpwllgwyngyllgogerychwyrndrobwllllantysiliogogogoch),但是如果我指定varchar(100)或varchar(500),则我不太可能得到这个问题.

那么如果你最大的字符串是400字符长,你会得到varchar(500)和(任意的)varchar(5000000)或者text()之间的性能

如果有人有这个答案也感兴趣,并且知道其他数据库的答案,请添加.

我已经google了,但没有找到一个足够的技术性的解释.

解决方法

我的理解是,有约束对于数据完整性是有用的,因此我使用列大小来验证下层的数据项,并更好地描述数据模型.

关于这件事的一些链接

> VARCHAR(n) Considered Harmful
> CHAR(x) vs. VARCHAR(x) vs. VARCHAR vs. TEXT
> In Defense of varchar(x)

原文链接:https://www.f2er.com/mssql/76373.html

猜你在找的MsSQL相关文章