我正在为我的项目规划一个新的数据库结构,现在我正在考虑将数据存储在json字段中,而不是创建一些连接的表。MySQL + PHP:将数据存储为JSON vs连接
我知道,我不应该将数据存储到json字段中,我想在WHERE,ORDER BY或类似的命令中使用它。但是关于简单的1:n文本数据呢?
例如:
Table: cities
---------------
id int
name string
districts string (JSON-Decoded PHP-Array)
VS
Table: cities
---------------
id int
name string
Table: districts
---------------
id int
city_id int FK to Table cities
name string
这只是一个简单的例子,但可以有更复杂的结构,如:
Table: cities
---------------
id int
name string
data string (JSON-Decoded PHP-Array)
// Where data is a json-decoded PHP-Array like this:
$city_1["data"] = array(
"districts" => array(1 => "East District", 2 => "West District"),
"special_text1" => "This text exists only in 10% of the cities",
"special_text2" => "This text exists only in 1% of the cities"
);
$city_2["data"] = array(
"districts" => array(1 => "South District", 2 => "West District", 3 => "Northern"),
"special_text1" => "This text exists only in 10% of the cities",
);
所以我的问题是:使用json解码的字段是否存在问题,例如由于性能等原因?
号如果目的是 - 与实体的作用和在不同的东西,然后序列化(与存储为JSON) - 是最糟糕的架构错误,你可以在一个使。 – 2014-08-29 10:14:06
我真的不明白这个问题。你将把JSON保存到''区域'列,但你想用它做什么?使用MySQL根据该列中的数据进行搜索/过滤? – 2014-08-29 10:14:24