简体   繁体   English

JPQL构造函数执行需要很长时间

[英]JPQL Constructor execution takes long time

I am using JPQL constructor in my code. 我在代码中使用JPQL构造函数。 It involves large results, 5024 data retrieved and objects created when executing this query. 执行此查询时,它涉及大量结果,检索到5024个数据和创建对象。 It is taking around 2 minutes to complete its execution. 完成执行大约需要2分钟。 There are multiple tables involved to execute this query. 有涉及执行该查询的多个表。 I could not go for cache since the DB data will update daily. 由于数据库数据每天都会更新,因此我无法进行缓存。 Is there any way to optimize the execution speed? 有什么方法可以优化执行速度?

My SQL query formation is like this, 我的SQL查询格式是这样的,

String sql = "SELECT DISTINCT "
                  + "new com.abc.dc.entity.market.LaptopData(vd.segment.id, vd.segment.segmentGroup, "
                  + "vd.segment.segmentName, vd.segment.category, "
                  + "vd.product.make, vd.product.model, vd.product.modelYear) "
                  + "FROM LaptopData vd, Profile p "
                  + "WHERE vd.profile.profileCode = :profileCode "
                  + "AND vd.pk.profileId = p.id "
                  + "Order By vd.segment.segmentGroup, vd.segment.segmentName, vd.product.make, vd.product.model,"
                  + " vd.product.modelYear asc";
Query query = em.createQuery(sql);
query.setParameter("profileCode", profileCode);

@SuppressWarnings("unchecked")
List<LaptopData> results = query.getResultList();

em.clear();
return results;

Well, one option for you is to create separate table which would contain result of this query for all entities and update it daily (every night) and then run your query on this new table, like this: 好的,您可以选择的一个方法是创建一个单独的表,其中包含所有实体的查询结果,并每天(每晚)对其进行更新,然后在此新表上运行查询,如下所示:

"SELECT DISTINCT"
              + "new com.abc.dc.entity.market.LaptopData(m.id, m.segmentGroup, "
              + "m.segmentName, m.category, "
              + "m.make, m.model, m.modelYear) "
              + "FROM someNewTable m"
              + "WHERE m.profileCode = :profileCode ";

This way you don't have to do join and ordering every time you execute your query, but only once a day when you generate new table. 这样,您不必在每次执行查询时都进行联接和排序,而每天只需生成一次新表即可。 Ofcourse with this approach to see data updates you'll have to wait until new table is recreated. 当然,使用这种方法来查看数据更新,您必须等待直到重新创建新表。

Also, you can create indexes on fields you are using in where clause. 另外,您可以在where子句中使用的字段上创建索引。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM