在这里使用 varbinary 比使用 varchar 有什么优势? [英] What is the advantage of using varbinary over varchar here?

查看:32
本文介绍了在这里使用 varbinary 比使用 varchar 有什么优势?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

不久前我问了一个关于 SQL Server 中的层次结构/版本号排序的问题.(我如何排序一般使用 SQL Server 查询的版本号"列).

A while ago I asked a question about hierarchy/version number sorting in SQL Server. ( How Can I Sort A 'Version Number' Column Generically Using a SQL Server Query).

提交的答案中有this链接到 TSQL 编码挑战,其中包含许多相同的谜题.

在 SQL2000 解决方案中,作者展示了两种变体,一种使用并返回 varchar,另一种使用 varbinary.作者解释说他正在这样做,但没有解释为什么.

In the SQL2000 solution the author demonstrated a two variations, one using and returning a varchar and the other varbinary. The author explains THAT he is doing this without explaining WHY.

所以,我的问题是,方法差异的主要区别/优势(如果有的话)是什么?IE.为什么使用 varbinary 而不是 varchar?

So, my question is really, what main differences/advantages (if any) of the difference in approach? I.e. why use a varbinary instead of a varchar?

我省略了发布代码,因为它在上面的文章中得到了最优雅的总结.

I've omitted posting the code, as its most elegantly summed up in the above article.

推荐答案

我相信 varbinary 数据通常会消耗更少的字节 (5),而不是 varchar 的每一部分(我认为是 10 或 11)原始字符串,因此,对于非常多的组件或发生的比较,它应该更有效.

I believe the expectation is that the varbinary data will generally consume fewer bytes (5), than the varchar one (10 or 11, I think) per portion of the original string, and so, for very large numbers of components, or comparisons to occur, it should be more efficient.

但我建议,如果您想使用任何一种解决方案,请同时实现两者(它们很短),并尝试对您的真实数据(和查询模式)进行一些分析,看看是否有实用价值差异(我不希望如此).

But I'd recommend that if you were looking to use either solution, that you implement both (they're quite short), and try some profiling against your real data (and query patterns), to see if there are practical differences (I wouldn't expect so).

(Crafty Steal):正如 Martin 指出的那样,二进制比较会更有效,因为它不会涉及处理排序规则的所有代码.:-)

(Crafty Steal): And as Martin points out, the binary comparisons will be more efficient, since it won't involve all of the code that's there to deal with collations. :-)

这篇关于在这里使用 varbinary 比使用 varchar 有什么优势?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆