Slow access to JSONB column I have the following query that accesses a JSONB column in the table datasets:

SELECT count(*), my_jsonb_column-&gt;0-&gt;'PARAM1'
FROM datasets
GROUP BY my_jsonb_column-&gt;0-&gt;'PARAM1' 


The explain analyze output is

HashAggregate  (cost=4778.92..5577.57 rows=53243 width=40) (actual time=1189.083..1189.135 rows=10 loops=1)
  Group Key: ((my_jsonb_column -&gt; 0) -&gt; 'PARAM1'::text)
  -&gt;  Seq Scan on datasets  (cost=0.00..4510.44 rows=53696 width=32) (actual time=0.052..1165.469 rows=53696 loops=1)
Planning time: 0.081 ms
Execution time: 1189.242 ms


A similar query that uses a plain column is roughly 50x faster, with a pretty much identical query plan also using a sequential scan.

The JSONB column contains an array with typically 1-3 elements, each one an object containing between 10 to 300 keys. 

I expected the JSONB version to be significantly slower than the plain column version, but I didn't expect it to be that slow. Am I doing something particularly inefficient here? 

Is there anything I can do to speed up this kind of access to a JSONB column? I'll need to access different keys, not just one specific one.

Does the size of the JSONB column play a role here? I would have assumed that this kind of access to a specific key should be mostly independent of the column size, but I'm not entirely sure.
The JSONB object is probably large enough that it is compressed and stored out of line, in a TOAST table.  Having to retrieve and decompress the TOASTed value for every row is going to slow things down by a lot.

I just wouldn't use JSON or JSONB for this purpose if I wanted speed.  If you need to access a small number of keys, pulling them out into the table proper could help.