是存储在MySQL中的数据为JSON是个好主意? [英] Is storing the data as json in mysql a good idea?
问题描述
我有一个场景,有这样的关系表一节有很多元素,让我想到了去为的has_many关系模型像一个部分有许多元素。
我从我们的客户一些建议,说要救一个列,而不是多个表中的数据为JSON。
我有点糊涂了哪些方法,我应该走了。
建议,欢迎。
这是模型正在提议
如:部分的has_many元素
表:部分
ID类型
1页
表:元素
ID类型内容section_id
1文字< P> lipsum lipsum< / P> 1
2图像/images/test.png 1
客户的建议是, STRONG>
ID类型的内容
第1页{:文本=> &其中p为H.; lipsum&所述; / P>中,图象=> /images/test.png}
在一个单一的数据库字段存储的数据作为JSON通常是一个坏主意。这意味着你将无法操纵/使用SQL查询数据 - 哪一种失败将数据存储在数据库中的点
我建议你部分
表和表之间存在一个一对多的关系为文本
和图片
。这可以通过 belongs_to的和的的has_many 协会的Rails
不管怎么说,创建新表将只有当你有多个文本和图像的一个部分是有帮助的。如果你有你的每一个部分的单一文本和单个图像(作为例子提出),你可能会被罚款有一个部分
表 ID
,键入
,文本
和图像
属性。
I got a scenario to have relational tables like a "section has many elements", so I thought of going for has_many relation model like a section has many elements.
I got some suggestions from our client saying to save the data as JSON inside a column instead of multiple tables.
I am bit confused which approach I should go with.
Suggestions are welcome.
This is the model am proposing
eg : section has_many elements
Table : sections
id type
1 page
Table : elements
id type content section_id
1 text <p>lipsum lipsum</p> 1
2 image /images/test.png 1
Client proposal is,
id type content
1 page {:text => "<p>lipsum</p>", image => "/images/test.png"}
Storing data as a JSON in a single database field is generally a bad idea. It means you will not be able to manipulate/query the data using SQL - which kind of defeats the point of storing the data in a database.
I'd recommend a one-to-many relationship between your section
table and tables for texts
and images
. That can be achieved through belongs_to and has_many associations in Rails.
Anyways, creating new tables will only be helpful if you have multiple texts and images for a single section. If you have a single text and a single image for each of your sections (as the example proposes), you'll probably be fine with a single sections
table with id
, type
, text
, and image
attributes.
这篇关于是存储在MySQL中的数据为JSON是个好主意?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!