gpt4 book ai didi

sql - 如何找到存储在postgres列中的json大小

转载 作者:行者123 更新时间:2023-11-29 11:24:36 24 4
gpt4 key购买 nike

我们正在使用 Postgres。在一个表中,我们有一列 JSON 类型。

如何找到为特定行存储的 JSON 的大小?以及如何在该列中找到具有最大 JSON 数据大小的行?

最佳答案

如果你想知道存储列需要多少字节,那么你可以使用

pg_column_size(any) - Number of bytes used to store a particular value (possibly compressed)

例子:

SELECT pg_column_size(int2) AS int2, pg_column_size(int4) AS int4,
pg_column_size(int8) AS int8, pg_column_size(text) AS text,
pg_column_size(bpchar) AS bpchar, pg_column_size(char) AS char,
pg_column_size(bool) AS bool, pg_column_size(to_json) AS to_json,
pg_column_size(to_jsonb) AS to_jsonb,
pg_column_size(json_build_object) AS json_build_object,
pg_column_size(jsonb_build_object) AS jsonb_build_object,
octet_length(text) AS o_text, octet_length(bpchar) AS o_bpchar,
octet_length(char) AS o_char, octet_length(to_json::text) AS o_to_json,
octet_length(to_jsonb::text) AS o_to_jsonb,
octet_length(json_build_object::text) AS o_json_build_object,
octet_length(jsonb_build_object::text) AS o_jsonb_build_object
FROM (SELECT 1::int2, 1::int4, 1::int8, 1::text, 1::char, '1'::"char",
1::boolean, to_json(1), to_jsonb(1), json_build_object(1,'test'),
jsonb_build_object(1,'test')
) AS sub

结果:

 int2 | int4 | int8 | text | bpchar | char | bool | to_json | to_jsonb | json_build_object | jsonb_build_object | o_text | o_bpchar | o_char | o_to_json | o_to_jsonb | o_json_build_object | o_jsonb_build_object
------+------+------+------+--------+------+------+---------+----------+-------------------+--------------------+--------+----------+--------+-----------+------------+---------------------+----------------------
2 | 4 | 8 | 5 | 5 | 1 | 1 | 5 | 20 | 18 | 21 | 1 | 1 | 1 | 1 | 1 | 14 | 13

获取具有最大 json 值的行只需按 pg_column_size(json_column) desc limit 1 排序。

关于sql - 如何找到存储在postgres列中的json大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47901739/

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