如果您的数据始终包含相同的简单结构,我认为没有任何理由将它们存储为 JSON。您应该考虑将其简单地存储在包含列的表中host
and type
.
INSERT INTO table(my_host_column, my_type_column) VALUES
(my_json ->> 'host', my_json ->> 'type');
这使得许多事情变得更加简单(搜索、更新……)。对于你的情况,Postgres 提供了inet
IP 地址列的类型。这样的专栏可以为您进行合理性检查host
, 例如 (https://www.postgresql.org/docs/current/static/datatype-net-types.html https://www.postgresql.org/docs/current/static/datatype-net-types.html)
您可以随时重新创建 JSONjson_build_object('host', my_host_column, 'type', my_type_column)
(https://www.postgresql.org/docs/current/static/functions-json.html https://www.postgresql.org/docs/current/static/functions-json.html)
But if you still want to store the JSON as it is:
如果您不想用它做任何事情,请将其存储为text
类型(我绝对不推荐,因为你不知道未来会带来什么)。如果你想使用 Postgres 的 JSON 函数,你应该将其存储为json
or jsonb
type (https://www.postgresql.org/docs/current/static/datatype-json.html https://www.postgresql.org/docs/current/static/datatype-json.html).
jsonb
主要是节省空间(更多元数据)的开销,但操作速度通常要快得多。
进一步阅读:
PostgreSQL引入的JSONB说明 https://stackoverflow.com/questions/22654170/explanation-of-jsonb-introduced-by-postgresql/22669114
在 PostgreSQL 中使用 JSONB 数据类型实现更快的操作 https://www.compose.com/articles/faster-operations-with-the-jsonb-data-type-in-postgresql