我正在我的学校使用SQL Server 2005为一个小型web应用程序开发数据库。 我在varchar vs nvarchar的问题上看到了几个学派的思想:

使用varchar,除非你要处理大量国际化的数据,否则就使用nvarchar。 只要用nvarchar就可以了。

我开始看到观点二的优点了。我知道nvarchar占用了两倍的空间,但这并不一定是一个大问题,因为它只存储几百个学生的数据。对我来说,不担心它,允许所有东西都使用nvarchar似乎是最简单的方法。还是我遗漏了什么?


当前回答

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.

其他回答

磁盘空间不是问题…但是记忆和性能会。 双倍的页面阅读量,双倍的索引大小,奇怪的LIKE和=恒定的行为等等

你需要存储中文等脚本吗?是或不是…

来自MS BOL的《Unicode的存储和性能影响》

编辑:

最近的SO问题强调了nvarchar性能有多差…

SQL Server在搜索nvarchar字符串时使用高CPU

由于您的应用程序很小,使用nvarchar与使用varchar相比,基本上没有明显的成本增加,而且如果您需要存储unicode数据,您也省去了潜在的麻烦。

I can speak from experience on this, beware of nvarchar. Unless you absolutely require it this data field type destroys performance on larger database. I inherited a database that was hurting in terms of performance and space. We were able to reduce a 30GB database in size by 70%! There were some other modifications made to help with performance but I'm sure the varchar's helped out significantly with that as well. If your database has the potential for growing tables to a million + records stay away from nvarchar at all costs.

Nvarchar将在内存、存储、工作集和索引方面有很大的开销,所以如果规格规定它真的永远都不需要,那就别费心了。

我不会有一个硬性的“总是nvarchar”规则,因为它在许多情况下完全是浪费——特别是来自ASCII/EBCDIC的ETL或标识符和代码列,它们通常是键和外键。

另一方面,有很多列的情况,在这些情况下,我肯定会在早期提出这个问题,如果我没有立即得到一个明确而快速的答案,我将使列为nvarchar。

如果您使用NVARCHAR只是因为系统存储过程需要它,最常见的情况是莫名其妙的sp_executesql,并且您的动态SQL非常长,那么从性能角度来看,您最好在VARCHAR中进行所有字符串操作(连接、替换等),然后将最终结果转换为NVARCHAR并将其输入到proc参数中。所以,不要总是使用NVARCHAR!