Skip to content

Commit

Permalink
note: json vs jsonb
Browse files Browse the repository at this point in the history
  • Loading branch information
harrylowkey committed May 13, 2024
1 parent 5424ff7 commit 74c844a
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 20 deletions.
33 changes: 33 additions & 0 deletions notes/databases/json_vs_jsonb_type.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# JSON vs JSONB in PostgreSQL

<!-- published_date: 18 May, 2024 -->
<!-- description: JSON vs JSONB in PostgreSQL -->
<!-- tags: database -->

Ref: https://www.dbvis.com/thetable/json-vs-jsonb-in-postgresql-a-complete-comparison/

JSON:
- Does not support indexing

JSONB:
- Supports indexing: GIN, GIST

JSON > JSONB when:
- You have to perform a lot of INSERT operations
- You do not expect to perform complex queries on your JSON data
- You need to preserve the original JSON data indentation and format

JSONB > JSON when:
- Storing user tracking data that needs to be updated over time
- Storing permission or configuration data
- Storing highly nested data whose structure may change over time

JSON vs. JSONB: Main differences

The main difference between json and jsonb lies in the way they store data behind the scene. This makes jsonb inherently more efficient than json. Specifically, the json data type stores an exact copy of the input text. So, each function and operation has to reparse the entire field at each execution.

At the same time, this can also represent an advantage over jsonb. This is because json preserves the indentation of the input data. So, if you need to be careful about JSON formatting, the json PostgreSQL data type can be useful.

On the other hand, jsonb stores data in a decomposed binary format. This makes the `INSERT` operation a bit slower compared to json due to conversion overhead. However, the jsonb binary format is significantly faster to process because it does not involve reparsing. Also, jsonb supports many more functions and operators than json.

Moreover, jsonb supports indexing. This can lead to significant performance advantages when parsing and querying JSON data.
40 changes: 20 additions & 20 deletions view_counts.txt
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
index:170
projects/climate:3
notes:14
projects/coders-tokyo-forum-backend:2
index:176
projects/climate:5
notes:16
projects/coders-tokyo-forum-backend:3
projects/code2image-package:3
projects/what-I-learn:2
notes/test-local-package:1
projects/renyoo:4
notes/setup-docker-on-ec2-server:2
projects/usdol:3
projects/renyoo:5
notes/setup-docker-on-ec2-server:3
projects/usdol:5
notes/sso:1
projects:5
projects:6
projects/iscale:3
projects/instagram-coding-easily:3
notes/locking:3
projects/tornomy:1
projects/materiality:2
projects/tornomy:2
projects/materiality:3
projects/coders-tokyo-forum-frontend:1
projects/mcashpay:2
projects/mcashpay:3
notes/5_cost_optimization:1
notes/reverse-proxy-vs-proxy-server:2
notes/transactions:3
Expand All @@ -26,15 +26,15 @@ notes/2_security:1
notes/microservice:1
notes/6_sustainability:1
notes/best-practices:1
notes/1_operational_excellence:1
notes/lambda:1
notes/3_reliability:1
notes/volumne_note:1
notes/query-plan:1
notes/useful-commands:1
notes/oci-image:1
notes/sargable-queries:1
notes/instance-purchasing-option:1
notes/1_operational_excellence:2
notes/lambda:2
notes/3_reliability:2
notes/volumne_note:2
notes/query-plan:2
notes/useful-commands:2
notes/oci-image:2
notes/sargable-queries:2
notes/instance-purchasing-option:2
notes/db-docker:1
notes/seucurity-context-note:1
notes/command:2

0 comments on commit 74c844a

Please sign in to comment.