hn.today

JSON vs. jsonb in Postgres: size and speed, measured

now-next.nl31 points0 comments
Screenshot of JSON vs. jsonb in Postgres: size and speed, measured

A benchmark of PostgreSQL 17.11 and 18.6 compares json and jsonb across six document shapes, from small records to large nested objects, random strings, and arrays of integers. Each shape was stored as compact or pretty-printed JSON, using the default pglz or LZ4 compression. The central finding is that jsonb’s disk footprint depends on document content and compression, not just the data type: across the tests, jsonb ranged from 22.6% smaller to 40.9% larger than json. For small compact documents it used 17.8% more space, while pretty-printed versions used 3.6% less. jsonb’s parsed representation adds per-key overhead but removes whitespace; once documents cross PostgreSQL’s roughly 2 KB compression threshold, compression can reverse which type is smaller.

jsonb was consistently faster for extracting a key, taking 2.7-10.6 times less time because json must be reparsed on each read. LZ4 sped reads of compressed documents but sometimes increased storage. Loading jsonb took 2.4-4.6 times longer in several tests. Unlike json, jsonb supports containment queries and GIN indexes: indexes cut rare-value searches from hundreds of milliseconds to under about 6 ms, though they offered little benefit for very common values and could exceed the table’s size. For frequently read fields, a separate typed or text column was faster still. The results support measuring representative data and workloads rather than assuming either format is universally smaller or better.

Read on now-next.nl0 comments on Hacker News

Summary generated by AI from the linked article. hn.today is not affiliated with Hacker News or Y Combinator.

More in Programming

Ggml v1.0.0

Ggml v1.0.0

ggml 1.0.0 removes deprecated public APIs and legacy DIAG_MASK operations, while adding configurable YaRN truncation and FP8 support. It also improves backend performance with quantized matrix multiplication, fused MoE operations, and new attention and scheduling paths. (github.com)

The daily digest

Today's best Hacker News stories, summarized and screenshotted, one email a day.