This is the second day of my participation in the More text Challenge. For more details, see more text Challenge
This article is participating in the “Java Theme month – Java development in action”, see: activity link for details
1. Project background
Recently doing a file management system, the function of the network location, similar to the baidu can achieve the function of files and folders uploads and downloads, and permissions management functions, because the relationship between the hierarchy of files and folders, not for data paging operation, otherwise the original hierarchy in query of data will be destroyed, can’t put the specific file into the corresponding folder, Therefore, when the amount of data is large, the efficiency of file permission assignment (setting the group information of files) is very low. It takes four or five seconds to process hundreds of pieces of data. After looking at the system, the leader couldn’t help but scold: “I go to your big watermelon”. After listening I one leng: what, you want to invite me to eat watermelon? After all, the system is made this way, I am quite embarrassed. I kept apologizing to the leader: SORRY sorry. The leader looked at my confused look in the eyes, came a sentence: do you know to add between sorry which two words more sad to urge? I frowned: to three, can’t afford? The leader looked at me with that contemptuous eyes and pointed at the door:
Okay, so to get to the point, performance tuning has definitely become an important indicator of the user experience for the system response time, and then we did a little bit of optimization of the code logic for the specific circumstances of the project.
2. Cause analysis
Aiming at the problem of loading time response, at first we did processing from the database and table structure level, but the effect is not big, then to make the code logic and business analysis, main reason is to file fu group permission information, because is circular file list, according to the file id to query the corresponding set of information, and then joining together after the object properties assigned to the file, Although it only takes 0.02-0.03 seconds to query the group information in a single file, the efficiency is still very low when the amount of data is large. Later, we tried to find out the group information and file ID of all files, and then compared it with the ID of the list file. If it matched, we put the group information into the collection, and finally assigned the value to the attributes in the file object. After the plan was thought out, the code logic was modified and implemented, and the final test results were actually improved by 20~30 times. The specific code optimization is as follows.
3. Code optimization
Before optimization:
Public void setFileGroupInfo(List<? > content){ for(Object object : content){ UserFile userFile = (UserFile) object; / / according to the file id to find sets of files information List < GroupFile > groupList = groupFileMapper. FindGroupFileByFileId (userFile. GetId ()); Long[] groupIds = new Long[groupList.size()]; for (int i = 0; i < groupList.size(); i++) { groupIds[i] = groupList.get(i).getGroupId(); } / / empowerment to file, if the file is open or contains a set of information, is set to have authorized the if (SysConstants. IS_PUBLIC_YES = = userFile. GetIsPublic () | | groupList. The size () > 0) { userFile.setIsAuthorize(1); } userFile.setGroupIds(groupIds); }}Copy the code
After the optimization:
public void setFileGroupInfo(List<? > content){// Since it is not efficient to loop to judge the content content and then query the group information according to the id of each content, we can fetch the id of the content and put it into a set. // Then use in to query all group information according to the fileIdList. Then loop to determine whether the object ID of content is equal to the fileId of the group list. If yes, put the corresponding group ID into groupIdsList of the object. List<Long> fileIdList = new ArrayList<>(); For (Object Object: content){UserFile UserFile = (UserFile) Object; fileIdList.add(userFile.getId()); } List<GroupFile> groupList = groupFileMapper.findGroupFileByFileIdList(fileIdList); For (Object Object: content){UserFile UserFile = (UserFile) Object; Long[] groupIds = new Long[]{}; List<Long> groupIdsList = new ArrayList<>(); GroupListSize = grouplist.size (); grouplist.size (); grouplist.size (); for (int i = 0; i < groupListSize; i++) { If (userfile.getid ().equals(grouplist.get (I).getFileId())){if(userFile.getid ().equals(grouplist.get (I).getFileId())){if(userFile.getid ().equals(grouplist.get (I).getFileId())) groupIdsList.add(groupList.get(i).getGroupId()); GroupIds = groupIdsList. ToArray (new Long[groupidslist.size ()]); userFile.setGroupIds(groupIds); / / empowerment to file, if the file is open or contains a set of information, is set to have authorized the if (SysConstants. IS_PUBLIC_YES = = userFile. GetIsPublic () | | userFile. GetGroupIds (.) length > 0){ userFile.setIsAuthorize(1); }}}Copy the code
Some users may ask, why not change the data structure, change the double-layer for loop to a map form, and then iterate over the entryset of the map to improve the efficiency, in fact, we have tested this idea, the performance of the change is not obvious. However, the for loop code here can be simplified by jdK8’s lambda expressions and flow features, so I won’t go over them here.
4. At the end
The week is so close to the past, tomorrow still have to adhere to the last day of work mode. About going to work, in fact, just like the old marriage, obviously not happy, but also have to stay together. It is said that 20 percent of the pain in life comes from going to work and 80 percent of the pain comes from not having money. So between going to work and not having money, we have to choose the former.