1

I have a number of fields that are applicable to some rows but not others. Instead of making individual fields and making them null for particular rows, I am considering making a JSON field and storing the data in there.

Would there be a performance hit if I wanted to search this data held in JSON? Would there be any other pitfuls I should watch out for or should I just make fields for every piece of data and make them nullable?

5
  • I think you are going to have to be more precise about exactly what data, what is will be used for in your app
    – RiggsFolly
    Commented Dec 19, 2022 at 16:06
  • Storage doesn't have performance. Queries have performance. You can't get an answer for this choice unless you evaluate it against specific queries you want to optimize. Commented Dec 19, 2022 at 17:21
  • @BillKarwin in general, would searching for a field where x = y, would json take a performance hit?
    – panthro
    Commented Dec 19, 2022 at 17:55
  • It's hard to make a general statement. Some JSON searches can be optimized with an expression index, but not all can. This is why it's important to be more specific about the query you want to optimize. Commented Dec 19, 2022 at 18:03
  • @BillKarwin comparing ints? Where x is > 100 for example.
    – panthro
    Commented Dec 19, 2022 at 18:04

1 Answer 1

1

You can use expression indexes to extract values from a JSON document, and then the search will be as optimized as if the column were a normal column.

Here's an example:

mysql> create table mytable (id serial primary key, data json);

mysql> alter table mytable 
  add index ((cast(json_unquote(json_extract(data, '$.x')) as unsigned)));

If you use the exact same expression in your search, then the optimizer will use the index:

mysql> explain select * from mytable 
  where cast(json_unquote(json_extract(data, '$.x')) as unsigned) > 100\G
*************************** 1. row ***************************
           id: 1
  select_type: SIMPLE
        table: mytable
   partitions: NULL
         type: range
possible_keys: functional_index
          key: functional_index
      key_len: 9
          ref: NULL
         rows: 1
     filtered: 100.00
        Extra: Using where

The downside of this is that you must hard-code the specific field you want in the index definition.

Also using expression indexes in this way requires MySQL 8.0.

Not all types of JSON searches can be indexed this way.

I've seen a lot of JSON questions on Stack Overflow, and I have to comment that searching JSON is seldom a good idea. It's harder to write the queries, it's harder to optimize the queries (and some cannot be optimized), and harder for some developers to read or debug.

Besides performance, JSON takes a lot more space (200-300% in my tests) than normal columns to store equivalent data.

You might like to read my presentation How to Use JSON in MySQL Wrong, or my past answers on Stack Overflow regarding MySQL and JSON. Prepare yourself for some of the crazy ways many people try to use JSON. It's almost always simpler to code and easier to optimize if one uses normal rows and columns.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.