如何在hibernate标准中使用mysql的“use index()”子句?

问题描述 投票:0回答:2

我在我的项目中使用标准 API,但是从 hibernate 创建的查询非常慢。

当我对查询运行解释时,我发现所需的索引没有被使用。所以我尝试使用

"use index()"
提供的
MYSQL
子句,查询只花了 5 秒。花了 70 秒。

但问题是我正在使用标准 API,并想知道是否有任何选项可以在标准 API 中使用

"use index()"
子句

java mysql hibernate criteria-api
2个回答
0
投票

虽然此解决方案已使用 HQL 进行了测试,但它也可能适用于 Criteria API。

您基本上定义了一个新的 Hibernate 函数,因此对于这个 HQL:

select table 
from MyTable table 
where useindex(table, my_index) is true and table.feature is not null

你会得到这个 SQL:

select table 
from MyTable table 
where useindex(table, my_index) is true and table.feature is not null

0
投票
createQuery(myCriteriaQuery)
  .addQueryHint("USE INDEX 'MyIndex' ON TABLE 'MyTable'");
© www.soinside.com 2019 - 2024. All rights reserved.