English 中文(简体)
邮政数据库:Sting或JSON栏
原标题:Database PostgreSql: column String or JSON?

i have a project write in Java 17. We use spring-boot and database PostgreSql. We have a table called "Setting" with this column: Id BIGINT, KEY String, VALUE String, uuid UUID

In this table we save generic configuration of project. For example:

KEY=workingDir, VALUE="C:workingDir" KEY=encoding, VALUE="UTF8" ecc...

But now we need to save a KEY that VALUE is a JSON. Example:

KEY=pattern, VALUE={range:10-50,valid:2,notvalid:0}

What are the best practises? Save JSON as a string? Other ideas? Thanks in advance

一项建议或想法

问题回答

如果称为“清算”的表格中的浏览量大(原因之一是有人储存数据库中的数值),你需要回答如何找到或分类数值,那么as of PostgreSQL v12, 您可能希望考虑使用<>/strong>:

  • The advantage of enforcing that each stored value is valid according to the JSON rules.
  • Assorted JSON-specific functions and operators available for data stored in these data types
  • Certain limitations for jsonb type s data values but certain improved DB efficiencies OR relaxed restrictions for json type s data and original data preservation but reduced DB efficiencies

这可能是针对初等人物的另列一栏,或可将原有数值重新编成或正常化为初等人物的数值(例如:<代码>{”价值:“原始价值”}),这取决于哪一种设定价值更为常见,或取决于你选择如何安排和管理桌子。

虽然不可能满足你的需要/要求,但另一种替代办法可能是,通过加强你与父母+子女关系结构的桌子,避免将JSON完全存放在与祖先切入钥匙或胎有关的个人后代条目/行中。

{>range”:[10, 50],“valid”:2,“notvalid”:0} Deposit as astring may be:

ID | KEY          | VALUE
1  | jsonSetting1 | {"range":[10, 50],"valid":2,"notvalid":0}
2  | strSetting1  | anySettingValue

相反,父母/子女结构可以是:

ID | PARENT | KEY           | VALUE
1  | null   | jsonSetting1  | null
2  | 1      | rangeMin      | 10
3  | 1      | rangeMax      | 50
4  | 1      | valid         | 2
5  | 1      | notValid      | 0
6  | null   | strSetting1   | anySettingValue

或更深层的祖先等级:

ID | PARENT | KEY           | VALUE
1  | null   | jsonSetting1  | null
2  | 1      | range         | null
3  | 2      | min           | 10
4  | 2      | max           | 50
5  | 1      | valid         | 2
6  | 1      | notValid      | 0
7  | null   | strSetting1   | anySettingValue

父母/子女关系也可以通过家庭调查建立。

这种办法的一些缺点,对于马森特人/马孙人来说,更为复杂,而且轮椅结构如何,但如果希望就个人价值提出问题,则可能有用。





相关问题
摘录数据

我如何将Excel板的数据输入我的Django应用? I m将PosgreSQL数据库作为数据库。

Postgres dump of only parts of tables for a dev snapshot

On production our database is a few hundred gigabytes in size. For development and testing, we need to create snapshots of this database that are functionally equivalent, but which are only 10 or 20 ...

How to join attributes in sql select statement?

I want to join few attributes in select statement as one for example select id, (name + + surname + + age) as info from users this doesn t work, how to do it? I m using postgreSQL.

What text encoding to use?

I need to setup my PostgreSQL DB s text encoding to handle non-American English characters that you d find showing up in languages such as German, Spanish, and French. What character encoding should ...

SQL LIKE condition to check for integer?

I am using a set of SQL LIKE conditions to go through the alphabet and list all items beginning with the appropriate letter, e.g. to get all books where the title starts with the letter "A": SELECT * ...