r/dataengineering • u/komal_rajput • May 08 '26
Discussion PostgreSQL query on 60M-row JSONB table is slow - should I add expression indexes or move to a structured table?
We have a silver_fec_efiling_itemizations table with 60M+ rows where each row stores the full FEC itemization record as JSONB in a record_data column. A typical query looks like this:
SELECT
record_data->>'contributor_first_name' AS first_name,
record_data->>'contributor_last_name' AS last_name,
record_data->>'contributor_state' AS state,
record_data->>'contributor_employer' AS employer,
(record_data->>'contribution_amount')::numeric AS amount,
LEFT(record_data->>'contribution_date',10)::date AS contribution_date
FROM silver_fec_efiling_itemizations
WHERE record_type = 'Schedule A'
AND record_data->>'entity_type' = 'IND'
AND record_data->>'contributor_state' = 'MD'
AND record_data->>'contributor_employer' ILIKE '%MICROSOFT%'
AND record_data->>'contribution_date' >= '2025-01-01'
AND record_data->>'contribution_date' < '2026-01-01'
record_type has a B-tree index but the rest of the filters are on JSONB extractions.
We do have a downstream structured table (fec_filing_lineitems) that promotes most of these fields into typed columns (entity_state, transaction_date, schedule_code, entity_type) -- except employer details.
Questions:
Is it worth adding expression indexes, or is 60M JSONB rows fundamentally the wrong place for these queries regardless of indexing?
Any general advice on indexing patterns for "mostly-JSONB" tables at this scale?
30
u/saltedappleandcorn May 08 '26
Is there any reason not to structure this out in to a proper table?
6
u/kamilc86 May 08 '26
Expression indexes on the JSONB paths you actually filter on will fix the immediate problem without restructuring. Create B tree indexes on the extracted contributor_state, entity_type, and contribution_date paths. For the ILIKE on employer, enable pg_trgm and create a GIN trigram index on that path because B tree cannot help with pattern matching.
The wide table concern is usually misplaced at this scale. PostgreSQL handles it fine with TOAST. If you query 5 or 6 fields but retrieve the rest, extract those into typed columns and keep the JSONB blob for everything else. You get indexed filters on the columns people actually search and the full record still lives in JSONB for display.
2
u/VipeholmsCola May 08 '26
You should also check out the r/database sub for niche questions
12
u/Justbehind May 08 '26
Either you model your data or go to NoSQL if you can't build a cohorent model that works and scales.
This screams anti-pattern from miles away.
While JSONB columns can be queried, it doesn't mean they should. At large data volumes, regular columns should be indexed and queried, JSONB should be for retrieval only.
1
u/wannabe-DE May 08 '26
Just saw this on LinkedIn. https://www.linkedin.com/posts/philip-mcclarence-86a49522_postgresql-jsonb-database
1
u/dinoaide May 08 '26
the problem is you need to parse it in the WHERE clause so you are doing full table read and parse 60M jsonb objects!
1
u/thecity2 May 08 '26
This design seems more appropriate for OLAP not OLTP.
1
u/parth-srin May 08 '26
You can index a specific jsonb field.
1
u/McHoff May 11 '26
Consider using a proper table rather than JSON? It looks like you have several different record_types... Depending on how many you have, one option is to make columns for all possible types and add a constraint to ensure the correct ones are non null depending on the value of record_type. Will be way faster and you'll have a safer schema too.
1
u/Pyromancer777 May 11 '26
This has been an incredible resource for my understanding of optimizations: https://use-the-index-luke.com/
I'm not even all the way through it, but there are golden tips starting from the first chapter
-2
u/theleveragedsellout May 08 '26
Perhaps time to look at noSQL
17
u/[deleted] May 08 '26
[removed] — view removed comment