存储 JSON 字符串的最佳 SQL 数据类型是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9207404/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What's best SQL datatype for storing JSON string?
提问by DatPT
What's the best SQL datatype for storing JSON string?
存储 JSON 字符串的最佳 SQL 数据类型是什么?
static List<ProductModel> CreateProductList()
{
string json = @"[
{
ProductId: 1,
ProductCode: 'A',
Product: 'A'
},
{
ProductId: 2,
ProductCode: 'B',
Product: 'B'
}
]";
IList<JToken> tokenList = JToken.Parse(json).ToList();
List<ProductModel> productList = new List<ProductModel>();
foreach (JToken token in tokenList)
{
productList.Add(JsonConvert.DeserializeObject<ProductModel>(token.ToString()));
}
return productList;
}
Which SQL datatype should we use for storing such a string containing JSON?
我们应该使用哪种 SQL 数据类型来存储这样一个包含 JSON 的字符串?
NVARCHAR(255)
?TEXT
?VARBINARY(MAX)
?
NVARCHAR(255)
?TEXT
?VARBINARY(MAX)
?
回答by marc_s
Certainly NOT:
当然不是:
TEXT, NTEXT
: those types are deprecatedas of SQL Server 2005 and should not be used for new development. UseVARCHAR(MAX)
orNVARCHAR(MAX)
insteadIMAGE
,VARBINARY(MAX)
:IMAGE
is deprecated just likeTEXT/NTEXT
, and there's really no point in storing a text string into a binary column....
TEXT, NTEXT
:这些类型自 SQL Server 2005 起已弃用,不应用于新开发。使用VARCHAR(MAX)
或NVARCHAR(MAX)
代替IMAGE
,VARBINARY(MAX)
:IMAGE
就像 一样被弃用TEXT/NTEXT
,将文本字符串存储到二进制列中真的没有意义....
So that basically leaves VARCHAR(x)
or NVARCHAR(x)
: VARCHAR
stores non-Unicode strings (1 byte per character) and NVARCHAR
stores everything in a 2-byte-per-character Unicode mode. So do you need Unicode? Do you have Arabic, Hebrew, Chinese or other non-Western-European characters in your strings, potentially? Then go with NVARCHAR
所以基本上离开VARCHAR(x)
or NVARCHAR(x)
:VARCHAR
存储非 Unicode 字符串(每个字符 1 个字节)并以每个字符NVARCHAR
2 个字节的 Unicode 模式存储所有内容。那么你需要Unicode吗?您的字符串中是否可能包含阿拉伯语、希伯来语、中文或其他非西欧字符?然后去NVARCHAR
The (N)VARCHAR
columns come in two flavors: either you define a maximum length that results in 8000 bytes or less (VARCHAR
up to 8000 characters, NVARCHAR
up to 4000), or if that's not enough, use the (N)VARCHAR(MAX)
versions, which store up to 2 GByte of data.
这些(N)VARCHAR
列有两种风格:要么定义导致 8000 字节或更少(VARCHAR
最多 8000 个字符,NVARCHAR
最多 4000 个)的最大长度,或者如果这还不够,请使用(N)VARCHAR(MAX)
存储最多 2 GB 数据的版本。
Update:SQL Server 2016will have native JSON support - a new JSON
datatype (which is based on nvarchar
) will be introduced, as well as a FOR JSON
command to convert output from a query into JSON format
更新:SQL Server 2016将具有原生 JSON 支持 -将引入新的JSON
数据类型(基于nvarchar
),以及FOR JSON
将查询输出转换为 JSON 格式的命令
Update #2:in the final product, Microsoft did not include a separate JSON
datatype - instead, there are a number of JSON-functions (to package up database rows into JSON, or to parse JSON into relational data) which operate on columns of type NVARCHAR(n)
更新 #2:在最终产品中,Microsoft 没有包含单独的JSON
数据类型 - 相反,有许多 JSON 函数(将数据库行打包为 JSON,或将 JSON 解析为关系数据)对类型的列进行操作NVARCHAR(n)
回答by Kangkan
I shall go for nvarchar(max)
. That should fit the requirement.
我会去的nvarchar(max)
。那应该符合要求。
Update:With SQL Server 2016 and Azure SQL, there are a lot of additional native JSON capabilities. This might positively impact your design or approach. You may read this for more: https://docs.microsoft.com/en-us/sql/relational-databases/json/json-data-sql-server
更新:使用 SQL Server 2016 和 Azure SQL,有很多额外的原生 JSON 功能。这可能会对您的设计或方法产生积极影响。您可以阅读更多内容:https: //docs.microsoft.com/en-us/sql/relational-databases/json/json-data-sql-server
回答by Marat Gallyamov
I would recommend to use nvarchar(max)
if you plan to use JSON features on SQL 2016 or Azure SQL.
nvarchar(max)
如果您计划在 SQL 2016 或 Azure SQL 上使用 JSON 功能,我会推荐使用。
If you don't plan to use those features, you could use varbinary(max)
combined with COMPRESS
(and DECOMPRESS
) functions. More information: https://blogs.msdn.microsoft.com/sqlserverstorageengine/2015/11/23/storing-json-in-sql-server/
如果您不打算使用这些功能,您可以varbinary(max)
结合使用COMPRESS
(和DECOMPRESS
)函数。更多信息:https: //blogs.msdn.microsoft.com/sqlserverstorageengine/2015/11/23/storing-json-in-sql-server/
COMPRESS and DECOMPRESS functions use standard GZip compression. If your client can handle GZip compression (e.g browser that understands gzip content), you can directly return compressed content. Note that this is performance/storage trade-off. If you frequently query compressed data you mig have slower performance because text must be decompressed each time.
COMPRESS 和 DECOMPRESS 函数使用标准 GZip 压缩。如果您的客户端可以处理 GZip 压缩(例如理解 gzip 内容的浏览器),您可以直接返回压缩内容。请注意,这是性能/存储权衡。如果您经常查询压缩数据,则迁移性能会变慢,因为每次都必须解压缩文本。
回答by Prabhazeal
Recommended data type is NVARCHAR.
Please refer
https://docs.microsoft.com/en-us/sql/relational-databases/json/json-data-sql-server?view=sql-server-ver15#store-and-index-json-data-in-sql-server][1]
推荐的数据类型是NVARCHAR。
请参考
https://docs.microsoft.com/en-us/sql/relational-databases/json/json-data-sql-server?view=sql-server-ver15#store-and-index-json-data-在-sql-server][1]