When can I save JSON or XML data in an SQL Table

前端 未结 8 1739
星月不相逢
星月不相逢 2020-11-27 12:04

When using SQL or MySQL (or any relational DB for that matter) - I understand that saving the data in regular columns is better for indexing sake a

8条回答
  •  天命终不由人
    2020-11-27 12:14

    I'll wave my magic wand. Poof! Golden Rules on use of JSON:

    • If MySQL does not need to look inside the JSON, and the application simply needs a collection of stuff, then JSON is fine, possibly even better.

    • If you will be searching on data that is inside and you have MariaDB 10.0.1 or MySQL 5.7 (with a JSON datatype and functions), then JSON might be practical. MariaDB 5.3's "Dynamic" columns is a variant on this.

    • If you are doing "Entity-Attribute-Value" stuff, then JSON is not good, but it is the least of several evils. http://mysql.rjweb.org/doc.php/eav

    • For searching by an indexed column, not having the value buried inside JSON is a big plus.

    • For searching by a range on an indexed column, or a FULLTEXT search or SPATIAL, JSON is not possible.

    • For WHERE a=1 AND b=2 the "composite" index INDEX(a,b) is great; probably can't come close with JSON.

    • JSON works well with "sparse" data; INDEXing works, but not as well, with such. (I am referring to values that are 'missing' or NULL for many of the rows.)

    • JSON can give you "arrays" and "trees" without resorting to extra table(s). But dig into such arrays/trees only in the app, not in SQL.

    • JSON is worlds better than XML. (My opinion)

    • If you do not want to get into the JSON string except from the app, then I recommend compressing (in the client) it an storing into a BLOB. Think of it like a .jpg -- there's stuff in there, but SQL does not care.

    State your application; maybe we can be more specific.

提交回复
热议问题