This question already has an answer here:
这个问题在这里已有答案:
- Storing Data in MySQL as JSON 16 answers
- 将数据存储在MySQL中作为JSON 16的答案
Is it acceptable to store JSON data in a MySQL table row? I need to store arrays in a mysql database. The problem is, i don't know how many columns i will need for each user. So I thought to store JSON in one row named array
for exemple. Is this the best way?
将JSON数据存储在MySQL表行中是否可以接受?我需要将数组存储在mysql数据库中。问题是,我不知道每个用户需要多少列。所以我想将JSON存储在一行名为array for example的行中。这是最好的方法吗?
Edit:
编辑:
Also, I am using text
as table column type.
另外,我使用text作为表列类型。
4 个解决方案
#1
12
Yes, it's a very good idea to use mysql as a key-value store, in fact facebook does for some uses.
是的,使用mysql作为键值存储是一个非常好的主意,事实上facebook确实用于某些用途。
CREATE TABLE `json` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`data` blob NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
The above table-structure will get you far. It's very easy to shard or cluster.
上面的表结构会让你走得更远。分片或群集非常容易。
Edit: The point here is to use PHP, Ruby etc to handle the json/data. You do SELECT ...
, do your edits, then INSERT ... ON DUPLICATE KEY UPDATE ...
.
编辑:这里的要点是使用PHP,Ruby等来处理json / data。你做SELECT ...,做你的编辑,然后INSERT ... ON DUPLICATE KEY UPDATE ....
#2
2
Storing more than one piece of data in a relational database field is generally wrong. It is possible to think of cases where it would be acceptable, but not for storing an entire user.
在关系数据库字段中存储多个数据通常是错误的。可以考虑可以接受的情况,但不能考虑存储整个用户的情况。
If a using a relational database structure is not sufficient you may want to look at a NoSQL database inestead of MySQL.
如果使用关系数据库结构是不够的,您可能需要查看NoSQL数据库而不是MySQL。
#3
0
It's not a problem. mySQL can already handle most of the brackets and funky characters in their text field.
这不是一个问题。 mySQL已经可以处理其文本字段中的大多数括号和时髦字符。
#4
0
I think it is not. What about four months later you decide add a new attribute to your entity or remove some attributes from your entity? How will you parse your old json contents? If you dont know how many columns you will need in your table, you should think in a different way and maybe create a dynamic structure like using user_column table
我认为不是。四个月之后,您决定向实体添加新属性还是从实体中删除一些属性?你将如何解析旧的json内容?如果您不知道表中需要多少列,您应该以不同的方式思考并创建一个动态结构,例如使用user_column表
#1
12
Yes, it's a very good idea to use mysql as a key-value store, in fact facebook does for some uses.
是的,使用mysql作为键值存储是一个非常好的主意,事实上facebook确实用于某些用途。
CREATE TABLE `json` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`data` blob NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
The above table-structure will get you far. It's very easy to shard or cluster.
上面的表结构会让你走得更远。分片或群集非常容易。
Edit: The point here is to use PHP, Ruby etc to handle the json/data. You do SELECT ...
, do your edits, then INSERT ... ON DUPLICATE KEY UPDATE ...
.
编辑:这里的要点是使用PHP,Ruby等来处理json / data。你做SELECT ...,做你的编辑,然后INSERT ... ON DUPLICATE KEY UPDATE ....
#2
2
Storing more than one piece of data in a relational database field is generally wrong. It is possible to think of cases where it would be acceptable, but not for storing an entire user.
在关系数据库字段中存储多个数据通常是错误的。可以考虑可以接受的情况,但不能考虑存储整个用户的情况。
If a using a relational database structure is not sufficient you may want to look at a NoSQL database inestead of MySQL.
如果使用关系数据库结构是不够的,您可能需要查看NoSQL数据库而不是MySQL。
#3
0
It's not a problem. mySQL can already handle most of the brackets and funky characters in their text field.
这不是一个问题。 mySQL已经可以处理其文本字段中的大多数括号和时髦字符。
#4
0
I think it is not. What about four months later you decide add a new attribute to your entity or remove some attributes from your entity? How will you parse your old json contents? If you dont know how many columns you will need in your table, you should think in a different way and maybe create a dynamic structure like using user_column table
我认为不是。四个月之后,您决定向实体添加新属性还是从实体中删除一些属性?你将如何解析旧的json内容?如果您不知道表中需要多少列,您应该以不同的方式思考并创建一个动态结构,例如使用user_column表