我正在我的学校使用SQL Server 2005为一个小型web应用程序开发数据库。 我在varchar vs nvarchar的问题上看到了几个学派的思想:
使用varchar,除非你要处理大量国际化的数据,否则就使用nvarchar。 只要用nvarchar就可以了。
我开始看到观点二的优点了。我知道nvarchar占用了两倍的空间,但这并不一定是一个大问题,因为它只存储几百个学生的数据。对我来说,不担心它,允许所有东西都使用nvarchar似乎是最简单的方法。还是我遗漏了什么?
我正在我的学校使用SQL Server 2005为一个小型web应用程序开发数据库。 我在varchar vs nvarchar的问题上看到了几个学派的思想:
使用varchar,除非你要处理大量国际化的数据,否则就使用nvarchar。 只要用nvarchar就可以了。
我开始看到观点二的优点了。我知道nvarchar占用了两倍的空间,但这并不一定是一个大问题,因为它只存储几百个学生的数据。对我来说,不担心它,允许所有东西都使用nvarchar似乎是最简单的方法。还是我遗漏了什么?
当前回答
Nvarchar将在内存、存储、工作集和索引方面有很大的开销,所以如果规格规定它真的永远都不需要,那就别费心了。
我不会有一个硬性的“总是nvarchar”规则,因为它在许多情况下完全是浪费——特别是来自ASCII/EBCDIC的ETL或标识符和代码列,它们通常是键和外键。
另一方面,有很多列的情况,在这些情况下,我肯定会在早期提出这个问题,如果我没有立即得到一个明确而快速的答案,我将使列为nvarchar。
其他回答
Nvarchar将在内存、存储、工作集和索引方面有很大的开销,所以如果规格规定它真的永远都不需要,那就别费心了。
我不会有一个硬性的“总是nvarchar”规则,因为它在许多情况下完全是浪费——特别是来自ASCII/EBCDIC的ETL或标识符和代码列,它们通常是键和外键。
另一方面,有很多列的情况,在这些情况下,我肯定会在早期提出这个问题,如果我没有立即得到一个明确而快速的答案,我将使列为nvarchar。
在某些特殊情况下,您会有意限制数据类型,以确保它不包含某个特定集合中的字符。例如,我有一个场景,我需要在数据库中存储域名。域名的国际化在当时是不可靠的,所以最好限制在基础水平上的输入,并有助于避免任何潜在的问题。
总是使用nvarchar。
对于大多数应用程序,您可能永远都不需要双字节字符。但是,如果您需要支持双字节语言,而您的数据库模式中只有单字节支持,那么返回并在整个应用程序中进行修改是非常昂贵的。
将一个应用程序从varchar迁移到nvarchar的成本将远远超过在大多数应用程序中使用的一点点额外磁盘空间。
For your application, nvarchar is fine because the database size is small. Saying "always use nvarchar" is a vast oversimplification. If you're not required to store things like Kanji or other crazy characters, use VARCHAR, it'll use a lot less space. My predecessor at my current job designed something using NVARCHAR when it wasn't needed. We recently switched it to VARCHAR and saved 15 GB on just that table (it was highly written to). Furthermore, if you then have an index on that table and you want to include that column or make a composite index, you've just made your index file size larger.
做决定时要考虑周全;在SQL开发和数据定义中,似乎很少有“默认答案”(当然,除了不惜一切代价避免游标)。
Generally speaking; Start out with the most expensive datatype that has the least constraints. Put it in production. If performance starts to be an issue, find out what's actually being stored in those nvarchar columns. Is there any characters in there that wouldn't fit into varchar? If not, switch to varchar. Don't try to pre-optimize before you know where the pain is. My guess is that the choice between nvarchar/varchar is not what's going to slow down your application in the foreseable future. There will be other parts of the application where performance tuning will give you much more bang for the bucks.