【发布时间】:2018-03-30 14:11:32
【问题描述】:
我正在编写一个 java 代码,一切都很好,但我开始在我的 GUI 中尝试一些缓慢的问题。这是我的代码:
//some code..
//get articles from database
//in this case listOfArticles contains 1000 Objects
listOfArticles = service.getArticles();
//loop through listOfArticles to get another data from my database
for (Article article: listOfArticles) {
//each list may conatins 100 to 200 Objects
//in this case I'm going to the database 1000 times and this maybe the cause of the Slowness
listOfInfo = service.getInfoById(article.getId());
//...some code
}
在 GUI 中我等待 20 秒到 30 秒才能得到结果,你有什么建议可以优化响应时间或避免进入我循环中的数据库。
提前致谢
【问题讨论】:
-
是否有可能将大量文章分解成块并仅以按需方式获取有关它们的信息?
-
图像等不应在对象中。使用 JPA 时需要小心得到什么:较重的字段,如列表。