JSONB 是 PostgreSQL 的二进制 JSON 类型 — 它以高效、可查询且可索引的格式存储 JSON 数据。它允许你在关系数据库中存储灵活的半结构化数据,将关系完整性与 NoSQL 样的灵活性相结合。这是 Postgres 最强大的功能之一。
JSONB 与 JSON 对比
JSON → stores the exact text (preserves whitespace/key order/duplicates), parsed each query
JSONB → stores a DECOMPOSED BINARY format → slightly slower to write, but MUCH faster
to query, and supports INDEXING and rich operators
→ Use JSONB in almost all cases (JSON only if you need exact text preservation).
存储和查询 JSONB
products (id SERIAL, attributes JSONB);
products (attributes)
();
attributes products;
attributes products;
attributes# products;
products attributes ;
products attributes @ ;
products attributes ? ;
