简体   繁体   English

Grails中多少个域类继承太多?

[英]How much domain class inheritance is too much in Grails?

From reading the docs in Grails I found the following statement: 通过阅读Grails中的文档 ,我发现以下语句:

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. 通常,我们的建议是,如果您要使用继承,请不要滥用它,也不要使继承层次结构太深。

My question is: how deep is too deep? 我的问题是:太深太深了?

Will 5 extensions in an inheritance chain make Grails cry?, 10?, 20?... what is the criteria to determine this?, or do we know if there is a clear way to extrapolate such performance degradation? 继承链中的5个扩展是否会使Grails哭泣?,10?,20?...确定的标准是什么?或者我们是否知道有明确的方法可以推断这种性能下降?

How deep is too deep? 到底有多深? is a rather subjective question. 是一个相当主观的问题。 But, it's possible to make an educated guess when you consider what happens at the database level with table-per-subclass inheritance. 但是,当您考虑每个子类的表继承在数据库级别发生的情况时,有可能做出有根据的猜测。 Lets assume you have these domain classes: 假设您具有以下域类:

class Employee {    
    String firstName
    String lastName

    static constraints = {
    }

    static mapping = {
        tablePerHierarchy false
    }

}

class Supervisor extends Employee {
    String office

    static constraints = {
    }
}

You'd end up with two tables: EMPLOYEE and SUPERVISOR . 您最终将获得两个表: EMPLOYEESUPERVISOR The EMPLOYEE table would contain the columns id , first_name , and last_name . EMPLOYEE表将包含idfirst_namelast_name列。 But notice that the SUPERVISOR table would only contain the columns id and office . 但是请注意, SUPERVISOR表仅包含idoffice列。

This means that to retrieve a Supervisor GORM has to join both tables in order to populate the inherited properties. 这意味着要检索Supervisor GORM必须连接两个表才能填充继承的属性。

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

It's these joins which have the potential to result in reduced performance. 这些连接有可能导致性能降低。 As you can imagine, 10 or 20 levels of inheritance would be disastrous. 您可以想象,继承的10或20个级别将是灾难性的。 Yet a few, especially if the tables are small, would likely be OK. 但是,少数几个,特别是如果表很小的时候,可能就可以了。

Besides, a deep inheritance hierarchy is a sign that something is probably wrong with the domain model architecture (ie. consider using Traits). 此外,深度继承层次结构表明域模型体系结构可能存在问题(例如,考虑使用特征)。

You can read more about both forms of inheritance in my article here . 你可以阅读更多关于我的文章两种形式的继承在这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM