简体   繁体   English

将Json对象存储到数据库

[英]Storing Json object to database

I run really complex reporting from the database. 我从数据库运行非常复杂的报告。 So far I was returning a complex json object and javscript was rendering the report. 到目前为止,我正在返回一个复杂的json对象,javscript正在呈现报告。 But as its growing more creating this json object is taking really long time . 但随着它越来越多,创造这个json对象需要花费很长时间。

I am thinking to replace this with another approach . 我想用另一种方法取而代之。 User will create a request to generate report and a server side process can generate json and store it for later use and user will be emailed a link. 用户将创建生成报告的请求,服务器端进程可以生成json并将其存储以供以后使用,用户将通过电子邮件发送链接。

Is it safe approach to store json object in database ? 在数据库中存储json对象是否安全? Main reason to store it is that columns that user is going to run report on are never fixed . 存储它的主要原因是用户将要运行报告的列永远不会被修复。 Should I rather go for NoSql ? 我应该选择NoSql吗?

Thanks in advance . 提前致谢 。

storing JSON in the database should be safe. 将JSON存储在数据库中应该是安全的。 JSON is simple text so in that sense, I don't see any problem with this approach. JSON是简单的文本,所以在这个意义上,我认为这种方法没有任何问题。

In the specific case you mention, you should consider a key-value store such as Redis. 在您提到的特定情况下,您应该考虑使用Redis这样的键值存储。 You could store, say user_id -> json_object. 你可以存储,比如user_id - > json_object。 Key-value stores are excellent for caching, which is essentially what it sounds like you are doing. 键值存储非常适合缓存,这实际上就像你正在做的那样。

In the general case, storing JSON is possible, but it loses one vital thing: it is not obvious how to use the semantics of your data in a SQL query. 在一般情况下,存储JSON是可能的,但它失去了一个重要的事情:如何在SQL查询中使用数据的语义并不明显。 For instance, say we store records like: 例如,假设我们存储以下记录:

{'color': blue, 'height': '40px'}  
{'color': blue, 'height': '50px'}  
{'color': red, 'height': '40px'}  

as strings in a database. 作为数据库中的字符串。

If we want to identify all elements that are color "blue" then our only option is to search for the string "%blue%" in each string element. 如果我们想要识别所有颜色为“蓝色”的元素,那么我们唯一的选择是在每个字符串元素中搜索字符串“%blue%”。 This is both costly and error prone. 这既昂贵又容易出错。 You might consider either breaking up the JSON and storing it as a database or moving to some sort of key-value pair or other store. 您可以考虑分解JSON并将其存储为数据库或移动到某种键值对或其他存储。

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

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