🎭 JSON vs JSONB: The Ultimate PostgreSQL Guide
Think they're the same? Think again! Here's what you're missing:
JSON: The Purist
JSONB: The Powerhouse
Performance Reality Check:
🔥 Hidden JSONB Operators:
@> Contains
<@ Contained by
? Key exists
?| Any keys exist
?& All keys exist
|| Concatenate
- Delete key/element
#- Delete at path
When to use JSON:
Logging raw API responses
Need to preserve exact format
Write-heavy, read-never workloads
When to use JSONB (99% of cases):
Any querying needed
Performance matters
Need indexing
Data aggregation
💎 Secret weapon:
Converted JSON to JSONB? What performance gains did you see? 🚀
#PostgreSQL #JSON #JSONB #Database #Performance
@postgres
Think they're the same? Think again! Here's what you're missing:
JSON: The Purist
-- Stores exact text representation
-- Preserves whitespace, duplicates, order
-- Faster input (no parsing)
-- No indexing support
CREATE TABLE logs (data JSON);
JSONB: The Powerhouse
-- Binary storage, parsed on input
-- Removes duplicates, sorts keys
-- 5-10x faster queries
-- Full indexing support!
CREATE TABLE events (data JSONB);
Performance Reality Check:
-- JSONB with GIN index
CREATE INDEX idx_data ON events USING GIN (data);
-- Lightning fast queries
SELECT * FROM events
WHERE data @> '{"user_id": 123}'; -- Uses index!
-- JSON? Full table scan every time 😢
🔥 Hidden JSONB Operators:
@> Contains
<@ Contained by
? Key exists
?| Any keys exist
?& All keys exist
|| Concatenate
- Delete key/element
#- Delete at path
When to use JSON:
Logging raw API responses
Need to preserve exact format
Write-heavy, read-never workloads
When to use JSONB (99% of cases):
Any querying needed
Performance matters
Need indexing
Data aggregation
💎 Secret weapon:
-- Partial JSONB index for massive performance
CREATE INDEX idx_active_users ON events (data->>'user_id')
WHERE data->>'status' = 'active';
Converted JSON to JSONB? What performance gains did you see? 🚀
#PostgreSQL #JSON #JSONB #Database #Performance
@postgres
👍1