【问题标题】:Hibernate union-subclass (table per concrete class) mapping generator of "increment" very slow?“增量”的休眠联合子类(每个具体类的表)映射生成器非常慢?
【发布时间】:2011-11-26 22:06:19
【问题描述】:

我的 Hibernate hbm 文件在 mysql DB 中看起来像这样:

<hibernate-mapping>
<class name="com.company.common.bo.position.Parent" table="Parents"
    abstract="true">
    <id name="id">
        <generator class="increment" />
    </id>
    <property name="date" not-null="true" />
    <property name="milliseconds" not-null="true" />
    <property name="shares">
        <column name="shares" precision="19" scale="6" not-null="true" />
    </property>
    <many-to-one name="ticker" column="tickerID" not-null="true" index="_tickerID_date_milliseconds_idx" />
    <many-to-one name="auditTrail" column="auditTrailID"
        not-null="false" cascade="save-update" lazy="false" fetch="select" />

    <union-subclass name="com.company.common.bo.position.SubclassA"
        table="SubclassAs">
        <many-to-one name="account" column="accountID" not-null="true" foreign-key="SubclassA_accountID_fk" />
        <many-to-one name="portfolio">
            <column name="portfolioID" not-null="true"/>
        </many-to-one>
        <many-to-one name="individualTrade">
            <column name="individualTradeID" not-null="false"/>
        </many-to-one>  
        <many-to-one name="positionTransfer" column="positionTransferID"
                cascade="save-update" not-null="false"/>
    </union-subclass>

    <union-subclass
            name="com.company.common.bo.position.SubclassB" table="SubclassBs">
        <many-to-one name="individualTrade">
            <column name="individualTradeID" not-null="false" />
        </many-to-one>  
        <many-to-one name="account" column="accountID" not-null="true" foreign-key="SubclassBs_accountID_fk"/>
        <many-to-one name="internalExecution" column="executionID"
                cascade="save-update" not-null="false" />
    </union-subclass>       

    <union-subclass name="com.company.common.bo.position.SubclassC"
        table="SubclassCs">
    </union-subclass>
</class>

所以基本上我有一个抽象类 Parent 和 3 个扩展它的子类(SubclassA、B、C)。在数据库中有 3 个表(用于 3 个子类)。 id 生成器是“增量”的,因为联合子类映射不允许我使用本机。所以看起来随着增量,ID 在 3 个表中是唯一的。当我查看休眠 sql 时,它基本上从所有 3 个表中找到最大 ID,并将其用作下一个 ID。但它使用的查询似乎非常低效。这是我看到的:

select max(ids_.id) from ( select id from SubclassAs union select id from SubclassBs union select id from SubclassCs ) ids_ 

运行时间超过 12 秒。这些表中的每一个都有超过一百万条记录。它将每个 ID 联合在一起,然后从中选择最大值。

如果我这样做:

select max(ids_.id) from ( select max(id) as id from SubclassAs union select max(id) as id from SubclassBs union select max(id) as id from SubclassCs ) ids_

这要快得多,不到一毫秒,因为内部联合只从每个表中获取最大值,然后我只从这 3 条记录中选择最大值。

有没有办法让 hibernate 改为这样做,或者有没有更好的方法在这 3 个表中使用 ID 生成器?

谢谢

【问题讨论】:

    标签: java mysql hibernate generator increment


    【解决方案1】:

    如果 increment 不满足您,您可以使用其他一些生成器策略,并且由于 MySQL 不支持序列,下一个合适的选项是 hilo strategy

    【讨论】:

      猜你喜欢
      • 2012-09-25
      • 1970-01-01
      • 2011-12-31
      • 1970-01-01
      • 2012-11-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-18
      相关资源
      最近更新 更多