【问题标题】:JPA Entity use index hintJPA 实体使用索引提示
【发布时间】:2012-04-22 13:10:03
【问题描述】:

是否可以在播放框架实体查询中指定数据库索引提示。

我的代码如下:

public static List<Transaction> findOnInactive(Date date) {
    return Transaction.find(
            "date = ? and account in ( select d.acctNb from account d "
                    + " where d.date = ? and (d.inactive = true or d.blocked = true)" 
                    + " group by d.acctNb )", date, date).fetch();
}

运行生成的查询需要 20 秒。但是使用

手动运行相同的查询

select * from transaction with (INDEX(_dta_index_k1_1)) ...

只需 1 秒。无论如何我可以在我的 JPA 查询中指定索引提示?

【问题讨论】:

    标签: sql-server hibernate playframework hint


    【解决方案1】:

    您需要使用原生 SQL 查询,如下所示:

    return JPA.em().createNativeQuery(
        "select * from transaction with (INDEX(_dta_index_k1_1)) ...",
        Transaction.class).getResultList();
    

    【讨论】:

    • 是的,但这意味着我需要手动将所有映射回我的事务对象。我的原始查询返回预先填充的事务列表,这非常方便。
    • 刚刚正确查看了您的代码。让我用指定的事务类试试这个。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-13
    • 2019-10-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多