PostgreSQL vs MySQL: JSONB Performance and Indexing Compared
This deep-dive covers postgresql vs mysql jsonb performance with actionable analysis, practical implications, and expert insights.
Why Semi-Structured Data Matters in Modern Relational Databases
Most production applications store a mix of rigid relational entities (users, invoices, permissions) and variable payloads (webhook events, product attributes, AI metadata). Both PostgreSQL and MySQL have mature binary JSON types, yet their indexing models differ fundamentally. Understanding how GIN indexes compare against generated B-tree columns prevents full-table scans once your table crosses a few hundred thousand rows.
PostgreSQL 16: Binary JSONB with GIN Indexing
PostgreSQL's JSONB type parses documents into a binary tree on write. A single Generalized Inverted Index (GIN) can accelerate containment (@>), key existence (?), and JSONPath queries across arbitrary nested fields without altering your schema.
CREATE TABLE events (
id BIGSERIAL PRIMARY KEY,
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
payload JSONB NOT NULL
);
-- Compact GIN index optimized for @> containment queries
CREATE INDEX idx_events_payload_gin
ON events USING GIN (payload jsonb_path_ops);
-- Fast indexed lookup on nested fields and array elements
EXPLAIN ANALYZE
SELECT id, created_at
FROM events
WHERE payload @> '{"service": "billing", "tags": ["retry"], "meta": {"region": "eu-west"}}';
Using the jsonb_path_ops operator class hashes full key-value paths instead of indexing every key separately, shrinking index size by roughly 40 to 60 percent and speeding up containment lookups.
MySQL 8: JSON with Virtual Generated Columns and Multi-Valued Indexes
MySQL 8 also stores JSON in an optimized internal binary format, allowing O(1) lookup of top-level keys without scanning the whole string. However, MySQL does not offer a general-purpose inverted index across an entire JSON document. Instead, you expose the JSON paths you filter on as virtual generated columns and attach standard B-tree indexes:
CREATE TABLE events (
id BIGINT AUTO_INCREMENT PRIMARY KEY,
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
payload JSON NOT NULL,
service VARCHAR(64) GENERATED ALWAYS AS (JSON_UNQUOTE(JSON_EXTRACT(payload, '$.service'))) VIRTUAL,
region VARCHAR(32) GENERATED ALWAYS AS (JSON_UNQUOTE(JSON_EXTRACT(payload, '$.meta.region'))) VIRTUAL,
INDEX idx_service_region (service, region)
);
-- Multi-valued index for array membership queries (MySQL 8.0.17+)
CREATE INDEX idx_event_tags
ON events ((CAST(payload->'$.tags' AS CHAR(32) ARRAY)));
EXPLAIN ANALYZE
SELECT id, created_at
FROM events
WHERE service = 'billing'
AND region = 'eu-west'
AND 'retry' MEMBER OF (payload->'$.tags');
Virtual generated columns consume zero extra row storage on disk (only the B-tree index takes space) and deliver excellent B-tree range scan speed for known query patterns.
Head-to-Head Comparison
| Capability | PostgreSQL 16 (JSONB) | MySQL 8 (JSON) |
|---|---|---|
| Ad-Hoc Nested Queries | Indexed automatically via GIN | Requires pre-defined generated columns |
| Known Single-Key Lookups | Fast (expression B-tree or GIN) | Very fast (virtual column B-tree) |
| Array Containment | Native @> with GIN | Multi-valued index + MEMBER OF() |
| Partial Updates | Rewrites updated JSONB value | In-place partial update for same-size values |
| SQL/JSON Path Support | Rich (jsonb_path_query, @?) | Basic path expressions ($.a.b) |
| Index Write Overhead | Higher on GIN; low on B-tree | Low (standard B-tree updates) |
Practical Schema Design Rules for Both Engines
- Promote stable filter columns: Always keep
tenant_id,user_id,status, and timestamps as native typed columns so relational joins and partitioning stay fast. - Use expression B-tree indexes for sorting: In PostgreSQL, if you frequently sort or range-filter a numeric field inside JSONB, add a targeted B-tree index like
CREATE INDEX idx_price ON items (((payload->>'price')::numeric));. - Avoid storing multi-megabyte blobs in JSON rows: Large JSON payloads trigger TOAST compression/decompression in PostgreSQL or overflow pages in InnoDB; store large raw documents in object storage and index only metadata.
Summary
If your application queries unpredictable nested attributes, tags, or dynamic filter combinations, PostgreSQL JSONB with GIN (jsonb_path_ops) is markedly more capable and ergonomic. If your JSON paths are well-known ahead of time and you already run MySQL 8, virtual generated columns paired with multi-valued indexes give you B-tree speed without migrating engines.
Was this technical analysis helpful?
Your feedback informs our benchmark updates and ongoing article audits.
FAQ
Frequently Asked Questions
Structured for search engines and AI answer systems (AEO/GEO).
PostgreSQL json stores an exact text copy and reparses it on every query. jsonb stores a decomposed binary representation that removes duplicate keys, supports GIN indexing, and executes containment and path queries dramatically faster.
Yes. MySQL 8.0.17+ supports multi-valued indexes using CAST(doc->'$.tags' AS CHAR(32) ARRAY), which accelerates MEMBER OF(), JSON_CONTAINS(), and JSON_OVERLAPS() queries.
Yes, GIN indexes have higher write and maintenance overhead than standard B-tree indexes. Using jsonb_path_ops and keeping fastupdate enabled minimizes insert penalties for high-write workloads.
For most web and SaaS apps that need both relational integrity (joins, transactions, foreign keys) and flexible metadata fields, PostgreSQL JSONB eliminates the cost of operating a second database.
More answers in our FAQ hub.
Newsletter
Practical Linux & Applied AI Newsletter
Get new tested tutorials and benchmark comparisons delivered directly to your inbox. Zero spam.
Join Weekly guides readers getting our weekly digest.
- Practical Linux administration & terminal workflows
- Tested open-source AI setups & benchmarks
- Zero spam - unsubscribe anytime