PostgreSQL NULL 与单独 Table
PostgreSQL NULL vs Separate Table
我有一个不断增长的 table 称为 transactions
,每月增长约 1000 万行。
此 table 有一个名为 extra
的 jsonb
列。
transactions
记录的 extra
列的 70% 为 NULL,其余的 json 值如下所示:
{
"lang": "en",
"pages": 3,
"message": "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Egestas purus viverra accumsan in nisl nisi. Arcu cursus vitae congue mauris rhoncus aenean vel elit scelerisque. In egestas erat imperdiet sed euismod nisi porta lorem mollis. Morbi tristique senectus et netus. Mattis pellentesque id nibh tortor id aliquet lectus proin. Sapien faucibus et molestie ac feugiat sed lectus vestibulum..."
}
注意:所有extra
json键对于所有行都是固定的,不会改变。
transactions
table概览:
id | price | type | extra
-------------------------------------------
1 | 2000.00 | SMS | null
2 | 2000.00 | SMS | null
3 | 4000.00 | SMS | null
4 | 5000.00 | SMS | {"lang": "en", "pages":8, "message":"Lore..."}
5 | 4000.00 | SMS | null
6 | 4000.00 | SMS | null
7 | 5000.00 | SMS | {"lang": "de", "pages":5, "message":"Some..."}
我为什么这么做?
我正在使用 jsonb
列而不是三个单独的列以避免许多 NULL 值。
使用 jsonb
我只有 1 列只有 30% 的 NULL,但是当我使用 3 个单独的列而不是 1 jsonb 列时,我的每列都有 30% 的 NULL。
问题:
将我的 extra
列拆分为 3 个单独的列是个好主意吗?
像这样:
id | price | type | lang | pages | message
--------------------------------------------
1 | 2000.00 | SMS | null | null | null
2 | 2000.00 | SMS | null | null | null
3 | 4000.00 | SMS | null | null | null
4 | 5000.00 | SMS | en | 8 | Lorem...
5 | 4000.00 | SMS | null | null | null
6 | 4000.00 | SMS | null | null | null
7 | 5000.00 | SMS | de | 5 | Some...
或者,我可以添加一个具有一对一关系的额外 table(例如 transaction_info
)。像这样:
交易
id | price | type
-------------------
1 | 2000.00 | SMS
2 | 2000.00 | SMS
3 | 4000.00 | SMS
4 | 5000.00 | SMS
5 | 4000.00 | SMS
6 | 4000.00 | SMS
7 | 5000.00 | SMS
transaction_info
id | transaction_id | lang | pages | message
--------------------------------------------
1 | 4 | en | 8 | Lorem...
2 | 7 | de | 5 | Some...
使用这种方法,我在两个 table 上都没有任何 NULL 值。
你更喜欢哪一个?
你应该阅读一些关于范式的内容 - 1. NF 说 - 每个值都是原子的。期望任何属性都有自己的列 - 这通常是个好主意(当属性数量少于 50 时)。 NULL 值只需要 1bite - 并且可能将数据存储在干净的关系 1NF 中比 JSON 格式更有效。
所以,因为你的新专栏只有三个,所以我对你的问题的回答是肯定的。这个主意不错。
第二个问题一两个 tables - 没有明确的答复 - 从关系模型的角度来看,两个变体都是正确的。如果现实中有明显的分离——有两个实体,那么我更喜欢两个 table。在其他地方(当列数较少时)我更喜欢 table.
我有一个不断增长的 table 称为 transactions
,每月增长约 1000 万行。
此 table 有一个名为 extra
的 jsonb
列。
transactions
记录的 extra
列的 70% 为 NULL,其余的 json 值如下所示:
{
"lang": "en",
"pages": 3,
"message": "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Egestas purus viverra accumsan in nisl nisi. Arcu cursus vitae congue mauris rhoncus aenean vel elit scelerisque. In egestas erat imperdiet sed euismod nisi porta lorem mollis. Morbi tristique senectus et netus. Mattis pellentesque id nibh tortor id aliquet lectus proin. Sapien faucibus et molestie ac feugiat sed lectus vestibulum..."
}
注意:所有extra
json键对于所有行都是固定的,不会改变。
transactions
table概览:
id | price | type | extra
-------------------------------------------
1 | 2000.00 | SMS | null
2 | 2000.00 | SMS | null
3 | 4000.00 | SMS | null
4 | 5000.00 | SMS | {"lang": "en", "pages":8, "message":"Lore..."}
5 | 4000.00 | SMS | null
6 | 4000.00 | SMS | null
7 | 5000.00 | SMS | {"lang": "de", "pages":5, "message":"Some..."}
我为什么这么做?
我正在使用 jsonb
列而不是三个单独的列以避免许多 NULL 值。
使用 jsonb
我只有 1 列只有 30% 的 NULL,但是当我使用 3 个单独的列而不是 1 jsonb 列时,我的每列都有 30% 的 NULL。
问题:
将我的 extra
列拆分为 3 个单独的列是个好主意吗?
像这样:
id | price | type | lang | pages | message
--------------------------------------------
1 | 2000.00 | SMS | null | null | null
2 | 2000.00 | SMS | null | null | null
3 | 4000.00 | SMS | null | null | null
4 | 5000.00 | SMS | en | 8 | Lorem...
5 | 4000.00 | SMS | null | null | null
6 | 4000.00 | SMS | null | null | null
7 | 5000.00 | SMS | de | 5 | Some...
或者,我可以添加一个具有一对一关系的额外 table(例如 transaction_info
)。像这样:
交易
id | price | type
-------------------
1 | 2000.00 | SMS
2 | 2000.00 | SMS
3 | 4000.00 | SMS
4 | 5000.00 | SMS
5 | 4000.00 | SMS
6 | 4000.00 | SMS
7 | 5000.00 | SMS
transaction_info
id | transaction_id | lang | pages | message
--------------------------------------------
1 | 4 | en | 8 | Lorem...
2 | 7 | de | 5 | Some...
使用这种方法,我在两个 table 上都没有任何 NULL 值。
你更喜欢哪一个?
你应该阅读一些关于范式的内容 - 1. NF 说 - 每个值都是原子的。期望任何属性都有自己的列 - 这通常是个好主意(当属性数量少于 50 时)。 NULL 值只需要 1bite - 并且可能将数据存储在干净的关系 1NF 中比 JSON 格式更有效。
所以,因为你的新专栏只有三个,所以我对你的问题的回答是肯定的。这个主意不错。
第二个问题一两个 tables - 没有明确的答复 - 从关系模型的角度来看,两个变体都是正确的。如果现实中有明显的分离——有两个实体,那么我更喜欢两个 table。在其他地方(当列数较少时)我更喜欢 table.