Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/63.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
MySQL+;PHP:将数据存储为JSON vs连接_Php_Mysql - Fatal编程技术网

MySQL+;PHP:将数据存储为JSON vs连接

MySQL+;PHP:将数据存储为JSON vs连接,php,mysql,Php,Mysql,我正在为我的项目规划一个新的数据库结构,现在我正在考虑将数据存储在json字段中,而不是创建一些连接表 我知道,我不应该将数据存储到json字段中,我希望在WHERE、ORDER BY或类似命令中使用json字段。但是,简单的1:n文本数据是什么呢 例如: Table: cities --------------- id int name string districts string (JSON-Decoded PHP-Array) vs 这只是一个简单的

我正在为我的项目规划一个新的数据库结构,现在我正在考虑将数据存储在json字段中,而不是创建一些连接表

我知道,我不应该将数据存储到json字段中,我希望在WHERE、ORDER BY或类似命令中使用json字段。但是,简单的1:n文本数据是什么呢

例如:

Table: cities
---------------
id          int
name        string
districts   string (JSON-Decoded PHP-Array)
vs

这只是一个简单的例子,但可能有更复杂的结构,如:

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数据不是很好的解决方案,当您想要查询此数据时

在其列上启用更多功能

出于您的目的,最好使用JSON,因为JSON是存储数据的本机方式,它是无模式的,可以保存复杂的结构。您还可以轻松地搜索其中的数据,这在MySQL数据库中以文本形式保存的JSON中是不可能的。在Mongo中,您的数据可以如下所示:

{
    _id: <ObjectId>,
    name: "City Name",
    data: {
        "districts": [
            {
                id: 1,
                name: "East District",
                population: 12221,
                ...
            },
            {
                id: 2,
                name: "West District",
                ...
            }
        ],
        "special_text1" : "This text exists only in 10% of the cities",
        "special_text2" : "This text exists only in 1% of the cities",
        ...
    },
    ...
}
{
_id:,
名称:“城市名称”,
数据:{
“地区”:[
{
id:1,
名称:"东区",
人口:12221,
...
},
{
id:2,
名称:"西区",
...
}
],
“特殊文本1”:“此文本仅存在于10%的城市”,
“特殊文本2”:“此文本仅存在于1%的城市中”,
...
},
...
}

No。如果目的是——像处理单独的事物一样处理实体,那么序列化(与存储json一样)——是您可能犯的最严重的架构错误之一。我真的不理解这个问题。您要将JSON保存到列
districts
,但您想用它做什么?使用MySQL根据该列中的数据进行搜索/过滤?正如我所说,我只想存储数据,而不想在这些json字段中进行查询。在我的例子中,MongoDB不是一个解决方案,因为其他数据需要在MySQL中。我有大约15个表,其中80%的字段是“经典”mysql colmns,而对于json字段,我只想存储列中的数据,在90%的情况下会存储空值。我只是想防止无用的列或为它们创建自己的表。如果您不需要查询此列,并且您确定将来不需要这样做,请将其存储为JSON或序列化数组,但我不建议这样做。为参考城市的数据创建自己的表肯定是更好的解决方案。
{
    _id: <ObjectId>,
    name: "City Name",
    data: {
        "districts": [
            {
                id: 1,
                name: "East District",
                population: 12221,
                ...
            },
            {
                id: 2,
                name: "West District",
                ...
            }
        ],
        "special_text1" : "This text exists only in 10% of the cities",
        "special_text2" : "This text exists only in 1% of the cities",
        ...
    },
    ...
}