在 Grails 中多少域 class 继承太多了?

How much domain class inheritance is too much in Grails?

通过阅读 Grails 中的 docs,我发现了以下语句:

However, excessive use of inheritance and table-per-subclass can result in poor query performance due to the use of outer join queries. In general our advice is if you're going to use inheritance, don't abuse it and don't make your inheritance hierarchy too deep.

我的问题是:多深才算太深?

继承链中的 5 个扩展会让 Grails 哭泣吗?、10 个?、20 个?...确定这一点的标准是什么?或者我们是否知道是否有明确的方法可以推断这种性能下降?

多深才算太深?是个比较主观的问题。但是,当您考虑使用 table-per-subclass 继承在数据库级别发生的情况时,可以做出有根据的猜测。假设您拥有这些域 类:

class Employee {    
    String firstName
    String lastName

    static constraints = {
    }

    static mapping = {
        tablePerHierarchy false
    }

}

class Supervisor extends Employee {
    String office

    static constraints = {
    }
}

您最终会得到两个 table:EMPLOYEESUPERVISOREMPLOYEE table 将包含 idfirst_namelast_name 列。但请注意 SUPERVISOR table 将仅包含列 idoffice.

这意味着要检索 Supervisor GORM 必须加入两个 table 以填充继承的属性。

SELECT EMPLOYEE.ID, FIRST_NAME, LAST_NAME, OFFICE
FROM   SUPERVISOR INNER JOIN EMPLOYEE 
       ON SUPERVISOR.ID = EMPLOYEE.ID

正是这些连接有可能导致性能下降。可以想象,10 或 20 级继承将是灾难性的。然而,有一些,特别是如果 table 很小,可能没问题。

此外,深继承层次结构表明领域模型架构可能存在问题(即考虑使用 Traits)。

您可以在我的文章 here 中阅读更多关于这两种继承形式的信息。