我在具体和类表继承之间权衡(见下面的例子)。类表当然有很多好处,特别是对于我的场景, super 表列保证在整个数据集中保持一致。但是,我几乎不需要一次查询每个子类,而是一次所有查询都在一个子类上(其中至少有 9 个子类)。
因此,我的行数看起来会非常大,查询一个行数较少的具体表是否会更快,即:
(如下例所示)
SELECT property_address
FROM policies_property
ORDER BY date_issued DESC;
或者外键关系是否足够快,以至于在类表继承中查找非常大的 super 表时的任何查询速度差异都可以忽略不计:
(如下例所示)
SELECT property_address
FROM policies_property INNER JOIN policies_super ON policies_property.id = policies_super.id
ORDER BY policies_super.date_issued DESC;
具体继承示例:每种类型的完全独立的表,每个表中都有重复的列>
--// Table: policies_motor
+------+---------------------+----------------+
| id | date_issued | vehicle_reg_no |
+------+---------------------+----------------+
| 1 | 2010-08-20 12:00:00 | 01-A-04004 |
| 2 | 2010-08-20 13:00:00 | 02-B-01010 |
| 3 | 2010-08-20 15:00:00 | 03-C-02020 |
+------+---------------------+----------------+
--// Table: policies_property
+------+---------------------+------------------+
| id | date_issued | property_address |
+------+---------------------+------------------+
| 1 | 2010-08-20 14:00:00 | Oxford Street |
+------+---------------------+------------------+
类表继承示例:一个父类(super class),许多子类。每个子类 id 引用一个父类(super class) id。
--// Table: policies_super
+------+---------------------+
| id | date_issued |
+------+---------------------+
| 1 | 2010-08-20 12:00:00 |
| 2 | 2010-08-20 13:00:00 |
| 3 | 2010-08-20 14:00:00 |
| 4 | 2010-08-20 15:00:00 |
+------+---------------------+
--// Table: policies_motor
+------+----------------+
| id | vehicle_reg_no |
+------+----------------+
| 1 | 01-A-04004 |
| 2 | 02-B-01010 |
| 4 | 03-C-02020 |
+------+----------------+
--// Table: policies_property
+------+------------------+
| id | property_address |
+------+------------------+
| 3 | Oxford Street |
+------+------------------+
请您参考如下方法:
I have next to no need to query every subclass at once, instead all queries will be on one subclass at a time (of which there are at least 9 subclasses).
对我来说,这听起来是一个让他们分开的令人信服的理由。不要认为“父类(super class)”方法是“更好的规范化”或“更相关的”等。它们只是理论上同样有效的两种设计选择;选择在实践中最有意义的那个。