gpt4 book ai didi

postgresql - Postgres : How to optimize this query which uses multiple json_array_elements() calls

转载 作者:行者123 更新时间:2023-11-29 12:40:39 28 4
gpt4 key购买 nike

我有以下查询,它从 JSON 对象中提取多列数据(facebook_results Postgres 10 column of json 数据类型)。

有时此对象中的数组包含 10,000 多个项目。

这样做的目的是从对象中的每一列获取非规范化数据的平面图,并且在有数组的地方我也想获取所有列以及其中包含的对象(显然只是将数据复制下来对于外键)。

最里面的键都不包含数组,所以我不需要担心这个。我只关心应该“展开”的 matchesnodes 数组。

现在查询有效,但速度非常非常慢。我假设是因为这是因为执行的递归查询编写不当或具有不必要的复杂性减慢。

SELECT
id AS slice_id,
json_array_elements(facebook_results -> 'table' -> 'matches') -> 'table' -> 'size' AS match_size,
json_array_elements(facebook_results -> 'table' -> 'matches') -> 'table' -> 'score' AS match_score,
json_array_elements(facebook_results -> 'table' -> 'matches') -> 'table' -> 'width' AS match_width,
json_array_elements(facebook_results -> 'table' -> 'matches') -> 'table' -> 'format' AS match_format,
json_array_elements(facebook_results -> 'table' -> 'matches') -> 'table' -> 'domain' AS match_domain,
json_array_elements(json_array_elements(facebook_results -> 'table' -> 'matches') -> 'table' -> 'nodes') -> 'table' -> 'crawl_date' AS node_crawl_date,
json_array_elements(json_array_elements(facebook_results -> 'table' -> 'matches') -> 'table' -> 'nodes') -> 'table' -> 'url' AS node_url
FROM slices
WHERE id = 169

这是 facebook_results 列中包含的内容的示例:

{
"table":{
"matches": [
{
"table":{
"nodes":[
{
"table":{
"crawl_date":"2013-06-21",
"url":"http://example.com"
}
}
],
"size":7962624,
"score":47.059,
"width":3456,
"format":"MP4",
"domain":"example.com"
}
}
]
}
}

有人知道我该如何优化它吗?

最佳答案

您可以使用 LATERAL 重写您的查询:

SELECT
id AS slice_id,
s.t -> 'size' AS match_size,
s.t -> 'score' AS match_score,
s.t -> 'width' AS match_width,
s.t -> 'format' AS match_format,
s.t -> 'domain' AS match_domain,
s.t2-> 'crawl_date' AS node_crawl_date,
s.t2-> 'url' AS node_url
FROM slices
,LATERAL (
SELECT json_array_elements(facebook_results -> 'table' -> 'matches') -> 'table',
json_array_elements(json_array_elements(facebook_results -> 'table' -> 'matches')
-> 'table' -> 'nodes') -> 'table') s(t,t2)
WHERE id = 169;

DBFiddle Demo

或者更短:

SELECT
id AS slice_id,
s.t -> 'size' AS match_size,
s.t -> 'score' AS match_score,
s.t -> 'width' AS match_width,
s.t -> 'format' AS match_format,
s.t -> 'domain' AS match_domain,
s2.t2 -> 'crawl_date' AS node_crawl_date,
s2.t2 -> 'url' AS node_url
FROM slices
,LATERAL(SELECT
json_array_elements(facebook_results -> 'table' -> 'matches') -> 'table' ) s(t)
,LATERAL(SELECT json_array_elements(s.t -> 'nodes') -> 'table') s2(t2)
WHERE id = 169;

DBFiddle Demo2

关于postgresql - Postgres : How to optimize this query which uses multiple json_array_elements() calls,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52049544/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com