I have table with a jsonb field and Postgres 12.1
create table market (
user int primary key,
base jsonb
);
the jsonb field has following structure
{
"a": [1, 2, 3],
"regions": [
{
"id": 1,
"name": "name",
"description": "description",
"shops": [
{
"id": 11,
"brands": [
{
"name": 22,
"id": 21
}
]
}
]
}
]
}
Our cliens choose a shop and then the brands. This chosing are stored in the market table. A user does his chose {shopId: 1, brands[1, 2, 3]}. I search how often the user chose this shop and this brands. In result i expected region_id, region_name, shop_id, count_of_using_shop_id, brand_id, count_of_using_brand_id
I have legacy the market table with several millions rows. I do not work with jsonb earlier and i am confused about that deep nested structure.
I think do this with using group by, but after several experiments i rejected this solution. Results are a very large number and group by operator performs costly sort.
Can you help me and point basic direction to solve my problem? Can it be more fastly directly with python with no sql? Of course i will be select all data to analyze with plain sql and then will be filtering shops and brands with python?