0

Which is better, to store config data into single jsonb field or dedicate a field for each property key?

For example, this json data:

{
  notifications: {
    check: {
      enabled: true,
      In_delimiters: true,
      Out_delimiters : false
    },
    battery_low: {
      enabled: true,
      battery_power: 0.70
    },
    gps_on: {
      enabled: true
    },
    gps_off: {
      enabled: true
    },
    speed_exceeded: {
      enabled: true,
      speed: 100
    }
  }
}

If I have thousands of users, each property key will be duplicated in each row, making the DB size grow bigger than if I dedicate a field for each property and store value only.

What's the advice in this case? Shall I go with jsonb and use compression? or dedicate a filed for each property?

1
  • json is good for unstructured data. Otherwise do not use it. Commented Jun 21, 2016 at 14:44

1 Answer 1

2

Don't think of database size first; storage is comparatively cheap these days.

Use json or jsonb if

  1. the structure varies from entry to entry, making it hard to come up with a relational data model
  2. there is not much processing of the values inside the database

Otherwise use a relational data model, that's what a relational database is best at.

If you don't process the data inside the database at all and saving storage is important to you, use json – if your row size exceeds 2KB, it will be compressed automatically. json is also the way to go if you have an object key order, white space or duplicate object keys that need to be preserved.

If you have to use the data in queries, use jsonb by all means. The operators are faster, and you can use indexes.

Sign up to request clarification or add additional context in comments.

2 Comments

What do you mean exactly when you write processing of the values inside the database?
Use JSON functions in SQL queries. Use indexes on JSON columns. Basically, do anything more than just storing and retrieving the JSON values.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.