JPA实体使用索引提示

时间:2012-04-10 08:28:53

标签: sql-server hibernate playframework hint

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

我的代码如下:

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秒。但是使用

手动运行相同的查询

从事务中选择*(INDEX(_dta_index_k1_1))...

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

1 个答案:

答案 0 :(得分:2)

您需要使用本机SQL查询,如下所示:

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