我使用Java和休眠模式。我尝试在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请求出了什么问题?
select co.num 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'.
我有105 000个结果。因此服务器缺少内存来渲染其他对象并创建CustomObject