简体   繁体   English

将JSON存储在msSQL数据库中?

[英]Storing JSON in an msSQL database?

I'm developing a form generator, and wondering if it would be bad mojo to store JSON in an SQL database? 我正在开发一个表单生成器,并想知道将JSON存储在SQL数据库中是不是很糟糕?

I want to keep my database & tables simple, so I was going to have 我想保持我的数据库和表格简单,所以我会有

`pKey, formTitle, formJSON`

on a table, and then store 在桌子上,然后存储

{["firstName":{"required":"true","type":"text"},"lastName":{"required":"true","type":"text"}}

in formJSON. 在formJSON中。

Any input is appreciated. 任何输入都表示赞赏。

I use JSON extensively in my CMS (which hosts about 110 sites) and I find the speed of access data to be very fast. 我在我的CMS(主存大约110个站点)中广泛使用JSON,我发现访问数据的速度非常快。 I was surprised that there wasn't more speed degradation. 我感到惊讶的是没有更多的速度降级。 Every object in the CMS (Page, Layout, List, Topic, etc) has an NVARCHAR(MAX) column called JSONConfiguration. CMS中的每个对象(页面,布局,列表,主题等)都有一个名为JSONConfiguration的NVARCHAR(MAX)列。 My ORM tool knows to look for that column and reconstitute it as an object if needed. 我的ORM工具知道要查找该列并在需要时将其重新构建为对象。 Or, depending on the situation, I will just pass it to the client for jQuery or Ext JS to process. 或者,根据具体情况,我将把它传递给客户端,以便jQuery或Ext JS进行处理。

As for readability / maintainability of my code, you might say it's improved because I now have classes that represent a lot of the JSON objects stored in the DB. 至于我的代码的可读性/可维护性,你可能会说它得到了改进,因为我现在有了代表DB中存储的许多JSON对象的类。

I used JSON.net for all serialization / deserialization. 我使用JSON.net进行所有序列化/反序列化。 https://www.newtonsoft.com/json https://www.newtonsoft.com/json

I also use a single query to return meta-JSON with the actual data. 我还使用单个查询来返回带有实际数据的meta-JSON。 As in the case of Ext JS, I have queries that return both the structure of the Ext JS object as well as the data the object will need. 与Ext JS的情况一样,我有查询返回Ext JS对象的结构以及对象将需要的数据。 This cuts out one post back / SQL round trip. 这减少了一个回发/ SQL往返。

I was also surprised at how fast the code was to parse a list of JSON objects and map them into a DataTable object that I then handed to a GridView. 我还惊讶于代码解析JSON对象列表的速度有多快,并将它们映射到DataTable对象,然后我将其传递给GridView。

The only downside I've seen to using JSON is indexing. 我看到使用JSON的唯一缺点是索引。 If you have a property of the JSON you need to search, then you have to store it as a separate column. 如果您有需要搜索的JSON属性,则必须将其存储为单独的列。

There are JSON DB's out there that might server your needs better: CouchDB, MongoDB, and Cassandra. 有JSON DB可以更好地满足您的需求:CouchDB,MongoDB和Cassandra。

A brilliant way to make an object database from sql server. 从sql server创建对象数据库的绝佳方法。 I do this for all config objects and everything else that doesn't need any specific querying. 我为所有配置对象以及其他不需要任何特定查询的内容执行此操作。 extending your object - easy, just create a new property in your class and init with default value. 扩展您的对象 - 简单,只需在您的类中创建一个新属性,并使用默认值创建init。 Don't need a property any more? 不再需要房产吗? Just delete it in the class. 只需在课堂上删除即可。 Easy roll out, easy upgrade. 易于推出,易于升级。 Not suitable for all objects, but if you extract any prop you need to index on - keep using it. 不适合所有对象,但如果你提取任何需要索引的道具 - 继续使用它。 Very modern way of using sql server. 非常现代的使用sql server的方式。

We have used a modified version of XML for exactly the purpose you decribe for seven or eight years and it works great. 我们使用XML的修改版本完全是您描述七八年的目的而且效果很好。 Our customers' form needs are so diverse that we could never keep up with a table/column approach. 我们客户的形式需求非常多样化,我们无法跟上表/列方法。 We are too far down the XML road to change very easily but I think JSON would work as well and maybe evan better. 我们在XML的道路上走得太远,很容易改变,但我认为JSON可以运行得更好,也许更好。

Reporting is no problem with a couple of good parsing functions and I would defy anyone to find a significant difference in performance between our reporting/analytics and a table/column solution to this need. 报告对于一些好的解析功能来说没有问题,我会蔑视任何人在我们的报告/分析和表/列解决方案之间找到性能上的显着差异。

It will be slower than having the form defined in code, but one extra query shouldn't cause you much harm. 它比在代码中定义的表单要慢,但是一个额外的查询不会对你造成太大的伤害。 (Just don't let 1 extra query become 10 extra queries!) (只是不要让1个额外的查询成为10个额外的查询!)

Edit: If you are selecting the row by formTitle instead of pKey (I would, because then your code will be more readable), put an index on formTitle 编辑:如果你通过formTitle而不是pKey选择行(我会,因为那时你的代码会更具可读性),在formTitle上放一个索引

You should be able to use SisoDb for this. 您应该能够使用SisoDb。 http://sisodb.com http://sisodb.com

I think it not an optimal idea to store object data in a string in SQL. 我认为将对象数据存储在SQL中的字符串中并不是一个最佳的想法。 You have to do transformation outside of SQL in order to parse it. 您必须在SQL之外进行转换才能解析它。 That presents a performance issue and you lose the leverage of using SQL native data parsing capability. 这会带来性能问题,您将失去使用SQL本机数据解析功能的优势。 A better way would be to store JSON as an XML datatype in SQL. 更好的方法是将JSON存储为SQL中的XML数据类型。 This way, you kill two birds with one stone: You don't have to create shit load of tables and still get all the native querying benefits of SQL. 通过这种方式,您可以一举两得:您不必创建表的shit加载,并且仍然可以获得SQL的所有本机查询优势。

XML in SQL Server 2005? SQL Server 2005中的XML? Better than JSON in Varchar? 在Varchar中比JSON好吗?

I wouldn't recommend it. 我不推荐它。

If you ever want to do any reporting or query based on these values in the future it's going to make your life a lot harder than having a few extra tables/columns. 如果您希望将来根据这些值进行任何报告或查询,那么将比使用一些额外的表/列更加困难。

Why are you avoiding making new tables? 你为什么要避免制作新桌子? I say if your application requires them go ahead and add them in... Also if someone has to go through your code/db later it's probably going to be harder for them to figure out what you had going on (depending on what kind of documentation you have). 我说如果你的应用程序要求他们继续并添加它们...如果有人必须通过你的代码/ db稍后它可能会更难以弄清楚你发生了什么(取决于什么类型的你有的文件)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM