SQL Server 不区分大小写的排序规则
在 SQL Server 中使用不区分大小写的排序规则有哪些优点/缺点(就查询性能而言)?
我有一个数据库当前正在使用不区分大小写的排序规则,但我不太喜欢它。我非常想将其更改为区分大小写。更改排序规则时应该注意什么?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果更改数据库上的排序规则,则还必须单独更改每个列上的排序规则 - 它们维护创建表时有效的排序规则设置。
结果:
If you change the collation on the database, you also have to change it on each column individually - they maintain the collation setting that was in force when their table was created.
Result:
(我将其添加为单独的答案,因为它与我的第一个答案有很大不同。)
好的,找到了一些实际的文档。这篇 MS 知识库文章指出,不同排序规则之间存在性能差异,但不是你想的那样。区别在于SQL 排序规则(向后兼容,但不支持unicode)和Windows 排序规则(支持unicode):
SQL 和 Windows 排序规则都有区分大小写和不区分大小写的版本,因此听起来这不是主要问题。
Dan 的优秀文章中的另一个好故事“来自战壕”,标题为“整理地狱":
他的结论是:
希望这有帮助。
(I added this as a separate answer because its substantially different than my first.)
Ok, found some actual documentation. This MS KB article says that there are performance differences between different collations, but not where you think. The difference is between SQL collations (backward compatible, but not unicode aware) and Windows collations (unicode aware):
Both SQL and Windows collations have case sensitive and case insensitive versions, so it sounds like that isn't the primary concern.
Another good story "from the trenches" in Dan's excellent article titled "Collation Hell":
He concludes:
Hope this helps.
我想说,在生产数据库中更改为区分大小写的排序规则的最大缺点是,许多(如果不是大多数)查询都会失败,因为它们当前被设计为忽略大小写。
我没有尝试更改现有数据库的排序规则,但我怀疑这也可能非常耗时。当这个过程发生时,您可能必须将您的用户完全锁定。除非您已经在开发人员上进行了彻底的测试,否则请勿尝试此操作。
I would say the biggest drawback to changing to a case sensitive collation in a production database would be that many, if not most, of your queries would fail because they are currently designed to ignore case.
I've not tried to change collation on an existing datbase, but I suspect it could be quite time consuming to do as well. You probably will have to lock your users out completely while the process happens too. Do not try this unless you have thoroughly tested on dev.
我找不到任何东西来确认正确构建的查询在区分大小写的数据库上与不区分大小写的数据库上是否运行得更快(尽管我怀疑差异可以忽略不计),但有几件事对我来说很清楚:
像这样的查询:
不会在 GiveName 上使用索引。你可能会想:
会更好,而且确实如此。但为了获得最佳性能,您必须执行以下操作:(
请参阅 这篇文章了解详情)
I can't find anything to confirm whether properly constructed queries work faster on a case-sensitive vs case-insensitive database (although I suspect the difference is negligible), but a few things are clear to me:
A query like:
won't use an index on GivenName. You would think something like:
would work better, and it does. But for maximum performance you'd have to do something like:
(see this article for the details)
如果更改数据库排序规则但未更改服务器排序规则(结果它们不匹配),请在使用临时表时小心。除非在 CREATE 语句中另有指定,否则它们将使用服务器的默认排序规则而不是数据库的默认排序规则,这可能会导致与数据库列进行 JOIN 或其他比较(假设它们也更改为数据库的排序规则,如 Damien_The_Un believeer 所暗示的那样)失败。
If you change the database collation but not the server collation (and they then don't match as a result), watch out when using temporary tables. Unless otherwise specified in their CREATE statement, they will use the server's default collation rather than that of the database which may cause JOINs or other comparisons against your DB's columns (assuming they're also changed to the DB's collation, as alluded to by Damien_The_Unbeliever) to fail.