是否有理由在 SQL Server 中对列使用 Base2 长度?
可能的重复:
varchar 字段 - 2 的幂更有效吗?
Nvarchar 或 varchar 什么最好使用 2 的乘法或四舍五入的整数?
出于纯粹的习惯,我将 SQL Server 中使用的列的大小定义为 Base2 大小。例如,这是我正在处理的一个表:
- ID int
- FirstName nvarchar(64)
- LastName nvarchar(64)
- Col4 varchar(16)
- Col5 nvarchar(32)
- Col6 nvarchar(128)
- 等等...
我不知道这个在哪里习惯从何而来,我不确定它是否有意义。下面的表定义在某种程度上是否会比上面的表定义效率低?
- ID int
- FirstName nvarchar(50)
- LastName nvarchar(50)
- Col4 varchar(10)
- Col5 nvarchar(30)
- Col6 nvarchar(100)
- 等...
我想我的主要问题是:使用 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:
- ID int
- FirstName nvarchar(64)
- LastName nvarchar(64)
- Col4 varchar(16)
- Col5 nvarchar(32)
- Col6 nvarchar(128)
- 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?
- ID int
- FirstName nvarchar(50)
- LastName nvarchar(50)
- Col4 varchar(10)
- Col5 nvarchar(30)
- Col6 nvarchar(100)
- etc...
I guess my main question is: are there any legitimate reasons for using Base2 column lengths?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
使列大于所需的大小可能会对数据库设计造成严重损害。来自博尔:
我发现如果你给他们额外的尺寸,他们迟早会使用它。此外,如果您将某些内容设置为 varchar (64) 并且实际上只需要最多 10 个字符,则更有可能有人将该字段用于其预期目的以外的用途,并且您会发现这些字段中的数据不正确(例如电话号码字段,其中包含有关办公室秘书的联系方式的注释,以选择一个不那么随机的示例)。
然而,至少这种设计比把所有东西都做成nvarchar(max)要好得多。
Making columns larger than they need to be can be actively harmful to your database design. From BOL:
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).
没有理由这样做,特别是对于 (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.
不,这只是程序员以 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.
疑。首先,列的确切长度主要是出于您自己的数据架构原因。其次,如果长度确实影响效率,则所有列的总长度可能是最重要的标准,即使如此,也会产生簿记开销,这意味着一个好的整数可能不是最佳答案。
因此,您可能会找到有关将行大小限制为特定数量的建议,以便整行适合页面或类似的内容。这是为了减少每条记录的磁盘 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.
这不是一个具体的 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.