Data

JSON(B) for business objects: an index for every query, or foreign keys without forty tables

Redbase Blog Rinat Kozin
Data

A 100,309-row benchmark on PostgreSQL 18.6 compares the same business object stored as one JSONB document, in 41 normalized tables read by Dapper and EF Core, and in redb Pro — timing queries, memory and disk.

Key points

  • The benchmark stores 100,309 employees on PostgreSQL 18.6 as JSONB, 41 tables with Dapper and EF Core, and redb Pro.
  • Without a query-specific index the Q1 filter reads all 25,210 pages; an expression index cuts it to 13 ms SQL.
  • A GIN index serves containment and key existence only, so it does nothing for range filters or ORDER BY.
  • System.Text.Json rejects the (int Year, string Quarter) tuple key by default, so the JSONB twin stores "2024-Q1" as text.
  • One employee by id: 62 SQL statements with EF Core's Include/AsSplitQuery chain versus 10 with redb.
  • Per 100 objects, JSONB allocates about 0.9 MB in .NET, Dapper 1.8 MB, redb 5.5-5.8 MB, EF Core 9-11 MB.

Why it matters

If you keep aggregates in JSONB because migrations hurt, plan the index work per query up front: each new filter or sort order means another expression index, and those indexes cover a single array position or dictionary key at best. That is a schema decision in disguise.

The gap against EF Core here comes from the examples' own query shape — a split query that repeats the page statement as a subquery — not from the ORM itself, and no hand-tuned EF Core query was measured. Read the figures as the price of Include-heavy loading of deep graphs, and time your own access pattern before swapping a data layer.

Real-world use

  • A workforce or HR system that keeps one employee profile with nested addresses, arrays and dictionaries as a single JSONB document.
  • A results page that must load 100 whole objects at once and, on some pages, expand their project references.
  • Filtering and sorting on a dictionary entry such as a quarterly review key like 2024-Q1.
  • Choosing normalized tables read by Dapper or EF Core when foreign keys and referential integrity are mandatory.

This summary was generated by the agent from the original post on Redbase Blog. The full text and all rights belong to its author and publisher. Go to original

Report this story

Is something wrong with the summary, or do you own this content? Your message comes straight to my inbox.

Type of message

Related stories

Feedback

Found a bug or have an idea? Your message lands straight in my inbox.

Type of message