Native JSON support in MYSQL 5.7 : what are the pros and cons of JSON data type in MYSQL?

后端 未结 4 659
情深已故
情深已故 2020-11-28 19:54

In MySQL 5.7 a new data type for storing JSON data in MySQL tables has been added. It will obviously be a great change in MySQL. They listed some benefits

<
4条回答
  •  夕颜
    夕颜 (楼主)
    2020-11-28 20:40

    I got into this problem recently, and I sum up the following experiences:

    1, There isn't a way to solve all questions. 2, You should use the JSON properly.

    One case:

    I have a table named: CustomField, and it must two columns: name, fields. name is a localized string, it content should like:

    {
      "en":"this is English name",
      "zh":"this is Chinese name"
       ...(other languages)
    }
    

    And fields should be like this:

    [
      {
        "filed1":"value",
        "filed2":"value"
        ...
      },
      {
        "filed1":"value",
        "filed2":"value"
        ...
      }
      ...
    ]
    

    As you can see, both the name and the fields can be saved as JSON, and it works!

    However, if I use the name to search this table very frequently, what should I do? Use the JSON_CONTAINS,JSON_EXTRACT...? Obviously, it's not a good idea to save it as JSON anymore, we should save it to an independent table:CustomFieldName.

    From the above case, I think you should keep these ideas in mind:

    1. Why MYSQL support JSON?
    2. Why you want to use JSON? Did your business logic just need this? Or there is something else?
    3. Never be lazy

    Thanks

提交回复
热议问题