ronan
ronan

Reputation: 4672

Performance improvement suggestions for the Java code ( if condition )

the following code in the if condition is going to called for almost 10,000 times and in testing showing the performance issues .

if (fileUploaderIdListVo != null && fileUploaderIdListVo.size() > 0) {
    for (FileUploaderIdVo fuidVo : fileUploaderIdListVo) {
        if ( fuidVo.getExploded() != null && fuidVo.getExploded().equalsIgnoreCase("Y")) {
            List<Rights> rightsList = rightsDao.list(fuidVo.getContractId());
            int contractTitleId = Integer.parseInt(fuidVo.getContractTitleId());
            List<Integer> titleRightIds = new ArrayList<Integer>();
            for(Rights right: rightsList) {
                if(right!=null ) {
                    titleRightIds.add(right.getId());
                }
            }
            titleRightsManager.saveRightCombination("true", user.getUid(), fuidVo.getContractId(), titleRightIds, contractTitleId,new ArrayList<String>()); 

        }
    }
}

What I think of

getting the rightsList outside the the if condition 2. calling the saveRightCombination again outside the if condition

Please suggest suitable ways to improve the performance of the following code.

Upvotes: 0

Views: 87

Answers (1)

zmbq
zmbq

Reputation: 39069

Your bottleneck is accessing the database, not your Java code. You can do the following:

  1. See what takes longer - querying the database or updating the database. If one of them is significantly more expensive than the other, focus on that first.
  2. Check your 10,000 queries - they might take a while due to database index issues. If you can speed them up by adding an index - do it, it's the easiest way to speed things up.
  3. Try to reduce the number of queries. Instead of accessing the database 10,000 times, see if you can access it a few times (say, load 1/4 of the records at once). This can save quite a lot of time. I'm writing "a few" and not "one time" because this will let you split the code into multiple threads in the future, if it is ever necessary.
  4. If the problem is with updating the database, see, again, if you can unite database accesses. Maybe send 500 updates at once, resulting in 20 database updates instead of 10,000. If your database has some bulk copy mechanism, you might want to use that (although with 10,000 records, this might not be worth it).

Upvotes: 3

Related Questions