我有一个包含“标识符”部分的JSON BLOB的数据数据库(数百万记录)(我创建了索引)。我有一个有效的查询来搜索该表中的一行,因此,如果有任何标识符类型(无论是“ isin”“ valor”或来自数组中的任何其他标识符,请匹配指定的ID,例如

问题描述 投票:0回答:1
将返回此行。在查询条件下,我还要硬码特定类型的ID,例如“ isin”“ valor” 现在,我尝试使用相同的查询来查看100个标识符的批次。 因此,我无法将类型进行编码,而是在查询中提取它:

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 ),

但它导致我的ID参数从数组结构中得到rme,它们成为
{"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

子查询
sql postgresql
1个回答
0
投票

DB<>小提琴
    
	
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.