【发布时间】:2023-03-20 13:24:01
【问题描述】:
我使用 java 和 hibernate。我尝试在 JPQL 中实现该请求,但与在相同条件下用纯 SQL 发出的类似的类似请求相比,运行它需要花费太多时间(我什至不得不在延迟 5 分钟后停止程序)
select NEW package.CustomObject(co.num, item, dim, mat, pro) from Object1 co LEFT JOIN co.items item LEFT JOIN item.dim dim LEFT JOIN item.mat mat LEFT JOIN item.pro pro
where co.ins between '2018-12-26 01:00:00' and '2019-06-26 01:00:00'
or co.mod between '2018-12-26 01:00:00' and '2019-06-26 01:00:00'.
CustomObject 如下
public class CustomObject {
private String num;
private OtherCustomObject other;
public CustomObject(String num, ItemObject item, DimObject dim, MatObject mat, ProObject pro) {
this.num = num;
this.other = new OtherCustomObject(item, dim, mat, pro);
}
}
public class OtherCustomObject {
private String property1;
private String property2;
private String property3;
private DimObject dim;
private MatObject mat;
private ProObject pro;
public OtherCustomObject(ItemObject item, DimObject dim, MatObject mat, ProObject pro) {
this.property1 = item.getProperty1();
this.property2 = item.getProperty2();
this.property3 = item.getProperty3();
this.dim = dim;
this.mat = mat;
this.pro = pro;
}
}
这是用纯 SQL 发出的相似的等效请求
select co.num
from table1 co left join ItemTable item on item.ou = co.ou left join DimTable dim on dim.item_id = item.id left join MatTable mat on mat.item_id = item.id left join ProTable pro on pro.item_id = item.id
where co.ins between '2018-12-26 01:00:00' and '2019-06-26 01:00:00'
or co.mod between '2018-12-26 01:00:00' and '2019-06-26 01:00:00';
这个请求几乎是即时的。那么我的 JPQL 请求有什么问题呢?
【问题讨论】:
-
什么是数据库?你能分享你的实体代码吗?您是否使用了
setParameter或使用硬编码参数创建了 JPQL?执行 JPQL 查询时是否生成了另一个 SQL?