jsonb_object_keys(scheme_and_id.json[0]) as id_type
不幸的是,在大大增加了查询的成本。
整个查询都附加在
fiddle中
i试图在类似的部分中以较早的方式提取ID_TYPE:
WITH scheme_and_id AS (
-- Convert the input JSON object into rows of key-value pairs
SELECT jsonb_array_elements('[
[{"isin": "XS1"}],
[{"valor": 456}]
]'::jsonb) as json
)
id_params AS (
-- Expand to individual parameters and extract the key name
SELECT
json as json,
jsonb_object_keys(json[0]) as id_scheme_name
FROM scheme_and_id,
LATERAL jsonb_array_elements(batch_json) as json
),
{"isin": "XS3", "primary": true}
虽然查询仅在每个ID JSON都是这样的数组时才有效:
[{"isin": "XS3", "primary": true}]
用于 @>运算符 我匹配“ type”:仅值的数组,现在忽略了“主”标志。 基本上,我希望获得一行的ID列,其中标识符数组匹配任何输入IDS选择 如in
WITH scheme_and_id AS (
-- Convert the input JSON object into rows of key-value pairs
SELECT jsonb_array_elements('[
[{"isin": "XS1"}],
[{"valor": 456}]
]'::jsonb) as json
), -- first build the JSON to be used to match the index
resolve_id as (select -- select the JSON objects
"id",
(blob -> 'identifiers') as "ids",
jsonb_object_keys(scheme_and_id.json[0]) as id_type
from blobstable,
scheme_and_id
where (blob -> 'identifiers') @> scheme_and_id.json)
select "id", id_type
from resolve_id
我的问题是:如何使此查询优化以与ID列表一起使用为输入
我不确定为什么要切碎外部
blobstable
。看来您只能在exists
上使用schema_and_id