将时间戳与数据库中的实际 table 分开

Separate the timestamps from the actual table in a Database

我想知道为数据库中的 timestamp 创建一个单独的 table juste 是否是一个好习惯。

我有一个包含多个 table 的数据库,它们使用时间戳 - 所有 6 个相同的字段 - 我想知道与其重复这些列,不如我只包含我的 foreign key时间戳 table.

这是我过于简化的例子:

用户table:

+----+-----------+-----------------------+
| ID | timestamp | username              |
+----+-----------+-----------------------+
| 1  | 1         | Nobody@nowhere.com    |
+----+-----------+-----------------------+
| 2  | 2         | nobody@nothere.ca     |
+----+-----------+-----------------------+
| 3  | 3         | nobody@hiding.org     |
+----+-----------+-----------------------+
| 4  | 4         | someone@somewhere.com |
+----+-----------+-----------------------+
| 5  | 4         | somebody@here.org     |
+----+-----------+-----------------------+

订单table

+----+-----------+--------------------+--------+
| ID | timestamp | ordersBlob         | userid |
+----+-----------+--------------------+--------+
| 1  | 6         | some text          | 1      |
+----+-----------+--------------------+--------+
| 2  | 7         | more text          | 1      |
+----+-----------+--------------------+--------+
| 3  | 8         | no text            | 2      |
+----+-----------+--------------------+--------+
| 4  | 9         | irony poining text | 3      |
+----+-----------+--------------------+--------+
| 5  | 10        | paradox text       | 4      |
+----+-----------+--------------------+--------+

时间戳table

+----+-----------+-----------+------------+------------+
| ID | createdOn | createdBy | modifiedOn | modifiedBy |
+----+-----------+-----------+------------+------------+
| 1  | 20170616  | 1         |            |            |
+----+-----------+-----------+------------+------------+
| 2  | 20170621  | 3         | 20170621   | 2          |
+----+-----------+-----------+------------+------------+
| 3  | 20160512  | 4         |            |            |
+----+-----------+-----------+------------+------------+
| 4  | 20160512  | 4         | 20160516   | 3          |
+----+-----------+-----------+------------+------------+
| 5  | 20160101  | 2         |            |            |
+----+-----------+-----------+------------+------------+
| 6  | 20160102  | 2         | 20160103   | 3          |
+----+-----------+-----------+------------+------------+
| 7  | 20160103  | 4         |            |            |
+----+-----------+-----------+------------+------------+
| 8  | 20160104  | 1         |            |            |
+----+-----------+-----------+------------+------------+
| 9  | 20160105  | 5         |            |            |
+----+-----------+-----------+------------+------------+
| 10 | 20160106  | 1         | 20160106   | 1          |
+----+-----------+-----------+------------+------------+

这些table仅作为示例。可以做那样的事情吗?如果你能提供反例和不这样做的原因,请提供。

希望这不是基于太多意见。

它不会为您节省任何磁盘 space,因为 'timestamps' table 需要索引。此外,在实际情况下,审核的 table 中的每一行都需要在 'timestamps' table 中单独的行,因此 'timestamps' 记录不太可能被重复使用。

它会降低 insert/update/selects 的性能,因为每个操作都需要触摸 2 tables,这会使磁盘 IO 量加倍(如果你想重用 'timestamps'条记录)。

此外,它会让你的数据table更难管理,更难保持一致,你的代码也会变得更复杂。