我有以下实体:
//metadata...
public class Article{
//properties...
private Set<Field> fields;
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "field", orphanRemoval = true)
public Set<Field> getFields()
{
return this.fields;
}
}
我的问题是我的服务获取所有文章需要很多时间,因为每个文章对象都有一个包含 200 个字段对象的列表,这是我的代码:
//this service toke a lot of time, beacause it load the Object Article and it list of Fields objects
listOfArticles = service.getArticles();
//loop through listOfArticles to construct a map of fields from the list
for (Article article: listOfArticles) {
//this service construct a map of fields for each Article
Map<String, String> mapFields = service.constructMap(article);
//...some code
}
我的想法是,在实体文章中,我想破坏与属性字段的关联(删除属性字段),并在应用程序启动时将所有 Fields 对象(来自数据库)加载到一个大地图中(地图可能包含 1M 个对象)然后在我的循环中,我将直接从 databse 的大地图网站中读取字段列表。这能为我解决问题,我可以减少响应时间吗?我的想法是提高性能的好解决方案吗?
提前谢谢。
加载整个表格从来都不是一个好主意,我会建议一些要点来提高你的表现。
- 为数据库结果创建分页
- 如果可能,仅加载文章并在列表中或您正在演示的位置显示属性
- 当您转到一篇文章时,从该特定文章中加载详细信息(字段)
- 将已加载的字段放入地图中,当您尝试访问文章时,请先验证内存中是否已经有,如果没有,则转到数据库。
尝试每次都使用延迟加载来提高系统性能。请记住,这样做可能会提高您的性能,但另一方面,您使用了更多内存,也许您可以考虑只使用列出的前 3 点。