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

时间:2017-06-21 20:32:36

标签: database-design timestamp separation-of-concerns

我想知道为数据库中的table创建单独的timestamp juste是不错的做法。

我有一个数据库,它有多个使用时间戳的表 - 所有6个相同的字段 - 并且想要知道,我可以只包含foreign key我的时间戳表而不是重复这些列。

这是我的简化示例:

用户表:

+----+-----------+-----------------------+
| 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     |
+----+-----------+-----------------------+

订单表

+----+-----------+--------------------+--------+
| 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      |
+----+-----------+--------------------+--------+

时间戳表

+----+-----------+-----------+------------+------------+
| 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          |
+----+-----------+-----------+------------+------------+

这些表仅作为示例。可以做那样的事情。如果你能提供反例和理由,为什么你不这样做,请提供。

希望这不是基于意见的。

1 个答案:

答案 0 :(得分:1)

它不会为您节省任何磁盘空间,因为'timestamps'表需要索引。此外,在实际情况下,审计表中的每一行都需要在“timestamps”表中单独排,因此“timestamps”记录不太可能被重用。

它会降低插入/更新/选择的性能,因为每个操作都需要触摸2个表,这会使磁盘IO的数量增加一倍(如果你想重用'timestamps'记录,会更糟糕)。

此外,它会使您的数据表更难管理,更难以保持一致,并且您的代码将变得更加复杂。