sql-server - SQL Server 内部是如何处理 Varchar 的?

标签 sql-server database-design

我想知道 SQL Server 如何在内部处理 varchar 动态长度和存储。

如果我知道列中有固定长度的字符,使用 Varchar 是否有益?

我知道 CHARVARCHAR 之间的区别,但是在大数据库中使用 CHARVARCHAR 进行数据库设计的最佳方法是什么

最佳答案

CHAR 在行中分配其大小,VARCHAR 存储字符串的实际大小(2 字节),然后是实际字符串,因此 varchar 的开销是 +2 字节,而 CHAR 的开销是 - 它的大小减去实际字符串长度。如果在 varchar 中存储 NULL 值 - 它在 db 中的大小为零。

参见 Anatomy of the Record

record header
    4 bytes long
    two bytes of record metadata (record type)
    two bytes pointing forward in the record to the NULL bitmap
fixed length portion of the record, containing the columns storing data types that have fixed lengths (e.g. bigint, char(10), datetime)
NULL bitmap
    two bytes for count of columns in the record
    variable number of bytes to store one bit per column in the record, regardless of whether the column is nullable or not (this is

different and simpler than SQL Server 2000 which had one bit per nullable column only) this allows an optimization when reading columns that are NULL variable-length column offset array two bytes for the count of variable-length columns two bytes per variable length column, giving the offset to the end of the column value versioning tag this is in SQL Server 2005 only and is a 14-byte structure that contains a timestamp plus a pointer into the version store in tempdb

关于sql-server - SQL Server 内部是如何处理 Varchar 的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9339386/

相关文章:

关于随时间更新 items_for_sale 的数据库/模式设计问题

database - 名字和姓氏分开或放在数据库中哪个更好?

iphone - 简单的 iOS 应用程序的数据存储方法是什么?

database - 存储大型人口统计数据集的最佳方式

sql-server - SQL Server 聚集索引比实际表大。为什么以及如何修复它?

sql-server - SQL Datediff 以秒为单位,带小数位

sql-server - ColdFusion SQL Server 数据源的最小占用空间是多少?

mysql - 按 3 个表的 JOIN 的 AVG 标记进行 SQL 分组

sql-server - SQL Server 如何合并两个数据库?

c# - 设计多个表使用相同模型的 SQL 数据库