我定义了一个简单的表
create table resources (id serial primary key, fields jsonb);
它包含带有键(从一个大集合中提取)和 1 到 100 之间的值的数据,例如:
id | fields
--------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
1 | {"tex": 23, "blair": 46, "cubic": 50, "raider": 57, "retard": 53, "hoariest": 78, "suturing": 25, "apostolic": 22, "unloosing": 37, "flagellated": 85}
2 | {"egoist": 75, "poshest": 0, "annually": 19, "baptists": 29, "bicepses": 10, "eugenics": 9, "idolizes": 8, "spengler": 60, "scuppering": 13, "cliffhangers": 37}
3 | {"entails": 27, "hideout": 22, "horsing": 98, "abortions": 88, "microsoft": 37, "spectrums": 26, "dilettante": 52, "ringmaster": 84, "floweriness": 72, "vivekananda": 24}
4 | {"wraps": 6, "polled": 68, "coccyges": 63, "internes": 93, "unburden": 61, "aggregate": 76, "cavernous": 98, "stylizing": 65, "vamoosing": 35, "unoriginal": 40}
5 | {"villon": 95, "monthly": 68, "puccini": 30, "samsung": 81, "branched": 33, "congeals": 6, "shriller": 47, "terracing": 27, "patriarchal": 86, "compassionately": 94}
我想搜索其值(与特定键相关)大于某个基准值的条目。我可以通过以下方式完成此任务:
with exploded as (
select id, (jsonb_each_text(fields)).*
from resources)
select distinct id
from exploded
where key='polled' and value::integer>50;
...但是当然这不使用索引,而是诉诸于表扫描。我想知道是否有:
- 查询“polled”>50 的资源的更有效方法
- 一种构建支持此类查询的索引的方法