【问题标题】:JPQL request with NEW That takes too much time带有 NEW 的 JPQL 请求花费了太多时间
【发布时间】: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?

标签: sql jpa jpql


【解决方案1】:

原因是要收集的数据太多。当我运行以下请求时

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-03-22
    • 1970-01-01
    • 2018-03-19
    • 1970-01-01
    • 2017-02-15
    • 2020-07-23
    • 2013-07-11
    相关资源
    最近更新 更多