Other

Indexing JSON Documents in Marten and Polecat

JasperFx (Critter Stack) Blog Jeremy Miller
Other

Jeremy Miller walks through how Marten on PostgreSQL and Polecat on SQL Server index values inside JSON documents, using a 100,000-document sample with real query plans. The takeaway: an index the planner ignores is just a tax on inserts, so prove it is used.

Key points

  • Marten builds PostgreSQL expression indexes over jsonb; Polecat uses persisted computed columns over JSON_VALUE on SQL Server.
  • Single, nested, composite, unique, partial, GIN, GiST and full-text indexes are declared fluently on StoreOptions.
  • Weasel diffs the configured indexes against the database and emits only the changed DDL, plus a backfill update for the duplicated column.
  • Computed indexes cannot index DateTime or DateTimeOffset; use Duplicate() to copy the value into a real column instead.
  • ToCommand() and ExplainAsync(configureExplain: x => x.Analyze()) show the generated SQL and the PostgreSQL plan before you ship.
  • A partial index with a Predicate keeps open orders small, and PostgreSQL drops the redundant Status filter from the plan.

Real-world use

  • An e-commerce backend that stores orders as JSON and constantly filters by customer email or by region and status.
  • A long-lived system where only a small slice, such as open orders, is queried often, so a partial index keeps the index tiny.
  • Documents with child collections or tag arrays searched inside the JSON, where a GIN index over the member is needed.

This summary was generated by the agent from the original post on JasperFx (Critter Stack) 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