I am working on a Spring-MVC project in which there is a part of statistics objects I want to retrieve from the DB, but as I have to retrieve it for entire day, the response time is very bad(18.7 seconds). How can I improve it? Controller :@RequestMapping(value = /getactivities)    public @ResponseBody Map<String, Object> getActivitiesForGroup(@RequestParam(groupid) long groupid) {        Map<String, Object> returnMap = new HashMap<>();        List<GroupMembers> members = this.groupMembersService.returnGroupMembers(groupid);        Map<Integer, Integer> activityCounter = new HashMap<>();        List<Integer> averageActivities = new ArrayList<>();        Date date = new Date();        Calendar cal = Calendar.getInstance();        cal.setTime(date);        int year = cal.get(Calendar.YEAR);        int month = cal.get(Calendar.MONTH);        int day = cal.get(Calendar.DAY_OF_MONTH);        for (int i = 0; i < 24; i++) {            averageActivities.add(this.noteActivityService.getAverageForHours(i, groupid));            int activityCount = 0;            for (GroupMembers member : members) {                NoteActivity activity = this.noteActivityService.getGroupNoteActivityForHour(year, month + 1, day, i, member.getMemberid());                activityCount += activity.getActivityCount();            }            activityCounter.put(i, activityCount);        }        returnMap.put(activities, activityCounter);        returnMap.put(averageActivities, averageActivities);        return returnMap;    }Service layer :@Overridepublic NoteActivity getGroupNoteActivityForHour(int yearNumber, int monthNumber, int dayNumber, int hourNumber, int memberId) {    NoteActivity noteActivity = this.noteActivityDAO.getGroupNoteActivityForHour(yearNumber,monthNumber,dayNumber,hourNumber,memberId);    if(noteActivity!=null){        return noteActivity;    }else {        return new NoteActivity();    }}DAO layer : @Override    public NoteActivity getGroupNoteActivityForHour(int yearNumber, int monthNumber, int dayNumber, int hourNumber, int memberId) {        Session session = this.sessionFactory.getCurrentSession();        Query query = session.createQuery(from NoteActivity as ga where ga.yearNumber=:yearNumber and ga.monthNumber=:monthNumber and ga.dayNumber=:dayNumber and ga.hourNumber=:hourNumber and ga.memberActivity.memberid=:memberid);        query.setParameter(yearNumber,yearNumber);        query.setParameter(monthNumber,monthNumber);        query.setParameter(dayNumber,dayNumber);        query.setParameter(hourNumber,hourNumber);        query.setParameter(memberid,memberId);        try{            return  (NoteActivity)query.uniqueResult();        }catch (Exception e) {            e.printStackTrace();        }        return null;    }Model :@Entity@Table(name = note_activity)public class NoteActivity implements Serializable{    private static final long serialVersionUID = 1L;    @Id    @Column(name = activity_id)    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = activity_gen)    @SequenceGenerator(name = activity_gen, sequenceName = activity_seq)    private int activityId;    @Column(name = activity_count,columnDefinition = integer default 0)    private int activityCount;    @Column(name = year_number,columnDefinition = integer default 0)    private int yearNumber;    @Column(name = month_number,columnDefinition = integer default 0)    private int monthNumber;    @Column(name = day_number,columnDefinition = integer default 0)    private int dayNumber;    @Column(name = hour_number,columnDefinition = integer default 0)    private int hourNumber;    @Column(name = date_of_activity,columnDefinition = character varying)    private String dateOfActivity;    @ManyToOne    @JoinColumn(name = memberid)    private GroupMembers memberActivity;}
Obtaining statistics from DB for activities on a given day
java;time limit exceeded;database;statistics;spring mvc