是否有理由在 SQL Server 中对列使用 Base2 长度?

发布于 2024-09-28 03:36:20 字数 933 浏览 5 评论 0原文

可能的重复:
varchar 字段 - 2 的幂更有效吗?
Nvarchar 或 varchar 什么最好使用 2 的乘法或四舍五入的整数?

出于纯粹的习惯,我将 SQL Server 中使用的列的大小定义为 Base2 大小。例如,这是我正在处理的一个表:

  1. ID int
  2. FirstName nvarchar(64)
  3. LastName nvarchar(64)
  4. Col4 varchar(16)
  5. Col5 nvarchar(32)
  6. Col6 nvarchar(128)
  7. 等等...

我不知道这个在哪里习惯从何而来,我不确定它是否有意义。下面的表定义在某种程度上是否会比上面的表定义效率低?

  1. ID int
  2. FirstName nvarchar(50)
  3. LastName nvarchar(50)
  4. Col4 varchar(10)
  5. Col5 nvarchar(30)
  6. Col6 nvarchar(100)
  7. 等...

我想我的主要问题是:使用 Base2 列长度有什么合法理由吗?

Possible Duplicates:
varchar Fields - Is a Power of Two More Efficient?
Nvarchar or varchar what is better use multiply of 2 or rounded full numbers??

Out of sheer habit I define the sizes of columns that I use in SQL Server to be Base2 sizes. For example, here's a table that I'm working on:

  1. ID int
  2. FirstName nvarchar(64)
  3. LastName nvarchar(64)
  4. Col4 varchar(16)
  5. Col5 nvarchar(32)
  6. Col6 nvarchar(128)
  7. etc...

I have no idea where this habit came from, and I'm not sure it even makes sense. Would the following table definition be less efficient in some way than the one above?

  1. ID int
  2. FirstName nvarchar(50)
  3. LastName nvarchar(50)
  4. Col4 varchar(10)
  5. Col5 nvarchar(30)
  6. Col6 nvarchar(100)
  7. etc...

I guess my main question is: are there any legitimate reasons for using Base2 column lengths?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

乱世争霸 2024-10-05 03:36:20

使列大于所需的大小可能会对数据库设计造成严重损害。来自博尔:

表每行最多可以包含 8,060 字节。在 SQL Server 2008 中,对于包含 varchar、nvarchar、varbinary、sql_variant 或 CLR 用户定义类型列的表,此限制有所放宽。超过 8,060 字节行大小限制可能会影响性能,因为 SQL Server 仍保留每页限制为 8 KB。当 varchar、nvarchar、varbinary、sql_variant 或 CLR 用户定义类型列的组合超出此限制时,SQL Server 数据库引擎会将宽度最大的记录列移动到 ROW_OVERFLOW_DATA 分配单元中的另一个页,同时保持 24-原始页上的字节指针。当记录根据更新操作延长时,动态地将大记录移动到另一个页面。缩短记录的更新操作可能会导致记录移回 IN_ROW_DATA 分配单元中的原始页。此外,查询和执行其他选择操作(例如对包含行溢出数据的大型记录进行排序或联接)会减慢处理时间,因为这些记录是同步处理的,而不是异步处理的。

我发现如果你给他们额外的尺寸,他们迟早会使用它。此外,如果您将某些内容设置为 varchar (64) 并且实际上只需要最多 10 个字符,则更有可能有人将该字段用于其预期目的以外的用途,并且您会发现这些字段中的数据不正确(例如电话号码字段,其中包含有关办公室秘书的联系方式的注释,以选择一个不那么随机的示例)。

然而,至少这种设计比把所有东西都做成nvarchar(max)要好得多。

Making columns larger than they need to be can be actively harmful to your database design. From BOL:

A table can contain a maximum of 8,060 bytes per row. In SQL Server 2008, this restriction is relaxed for tables that contain varchar, nvarchar, varbinary, sql_variant, or CLR user-defined type columns....Surpassing the 8,060-byte row-size limit might affect performance because SQL Server still maintains a limit of 8 KB per page. When a combination of varchar, nvarchar, varbinary, sql_variant, or CLR user-defined type columns exceeds this limit, the SQL Server Database Engine moves the record column with the largest width to another page in the ROW_OVERFLOW_DATA allocation unit, while maintaining a 24-byte pointer on the original page. Moving large records to another page occurs dynamically as records are lengthened based on update operations. Update operations that shorten records may cause records to be moved back to the original page in the IN_ROW_DATA allocation unit. Also, querying and performing other select operations, such as sorts or joins on large records that contain row-overflow data slows processing time, because these records are processed synchronously instead of asynchronously.

I've found if you give them the extra size sooner or later they will use it. Further, if you set something as varchar (64) and you only really need 10 characters max, you are making it more likely someone will use the field for other than it's intended purpose and you will find that you get bad data in those fields (like a phone number field containing notes about the office secretary to contact to pick a not so random example).

However at least this design is far better than making everything nvarchar (max).

久光 2024-10-05 03:36:20

没有理由这样做,特别是对于 (n)varchar 数据,其中存储大小是数据的实际长度 + 2 个字节。

No reason to do this, especially with (n)varchar data, where the storage size is the actual length of the data + 2 bytes.

仙女山的月亮 2024-10-05 03:36:20

不,这只是程序员以 2 的幂来思考和行动的习惯 - SQL Server 绝对没有技术原因这样做 - 不会提高速度或性能或类似的东西。

No, it's just a programmer's habit to think and act in powers of 2 - there's definitely no technical reason from SQL Server to do this - no increase in speed or performance or anything like that.

帅冕 2024-10-05 03:36:20

疑。首先,列的确切长度主要是出于您自己的数据架构原因。其次,如果长度确实影响效率,则所有列的总长度可能是最重要的标准,即使如此,也会产生簿记开销,这意味着一个好的整数可能不是最佳答案。

因此,您可能会找到有关将行大小限制为特定数量的建议,以便整行适合页面或类似的内容。这是为了减少每条记录的磁盘 I/O 数量。但各个列的大小并不重要,重要的是总数。

Doubtful. First of all, the exact lengths of columns matters mostly for your own data schema reasons. Secondly, if the lengths do enter into efficiencies, the total length of all the columns is likely the most important criterion, and even then, there will be bookkeeping overhead that will mean that a nice round number is not likely to be the best answer.

So you might find advice about limiting your row size to a particular amount, so that the entire row fits into a page, or something along those lines. This is to reduce the number of disk I/O's per record. But the individual column sizes don't matter, it would be the total that does.

哭泣的笑容 2024-10-05 03:36:20

这不是一个具体的 SQL SErver 问题...我在 Oracle 和 MySQL 中做了同样的事情。除了我可能觉得使用 base2 尺寸更舒服之外,没有什么特别的原因。

It's not specifically a SQL SErver question... I do the same thing in Oracle and MySQL. There's no particular reason other than the fact that perhaps I feel more comfortable using base2 sizes.

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