一段时间以来,我一直在使用MongoDB运行一个小型Java SpringFramework项目。尽管它无法在功能最强大的硬件上运行,但最近使用一个特定的Collection时,其运行速度却非常缓慢。
该数据库的特定集合具有大约30个条目,平均大小为1.5KB。大多数其他收藏较小且速度更快。
我将其范围缩小到了数据库,因为将集合清空可以完全加快它的速度。
日志使我困惑。当我请求网页时,这是我的日志(在比我的服务器稍好的硬件上运行,甚至更慢):
...
2018-07-23 13:34:08.176 DEBUG 12736 --- [nio-8080-exec-8] o.s.web.servlet.DispatcherServlet : DispatcherServlet with name 'dispatcherServlet' processing GET request for [/employees/active]
2018-07-23 13:34:08.177 DEBUG 12736 --- [nio-8080-exec-8] s.w.s.m.m.a.RequestMappingHandlerMapping : Looking up handler method for path /employees/active
2018-07-23 13:34:08.178 DEBUG 12736 --- [nio-8080-exec-8] s.w.s.m.m.a.RequestMappingHandlerMapping : Returning handler method [public java.lang.String be.ccdestrycker.callcentermanager.controller.EmployeeController.showActiveEmployees(org.springframework.ui.ModelMap)]
2018-07-23 13:34:08.178 DEBUG 12736 --- [nio-8080-exec-8] o.s.web.servlet.DispatcherServlet : Last-Modified value for [/employees/active] is: -1
2018-07-23 13:34:09.725 INFO 12736 --- [nio-8080-exec-8] b.c.c.CallCenterManagerApplication : TIMING - controller start
2018-07-23 13:34:09.747 INFO 12736 --- [nio-8080-exec-8] b.c.c.CallCenterManagerApplication : TIMING - controller end
2018-07-23 13:34:09.748 DEBUG 12736 --- [nio-8080-exec-8] o.s.w.s.v.ContentNegotiatingViewResolver : Requested media types are [text/html, application/xhtml+xml, image/webp, image/apng, application/xml;q=0.9, */*;q=0.8] based on Accept header types and producible media types [*/*])
2018-07-23 13:34:09.748 DEBUG 12736 --- [nio-8080-exec-8] o.s.w.s.v.ContentNegotiatingViewResolver : Returning [org.thymeleaf.spring5.view.ThymeleafView@5d0cd3ba] based on requested media type 'text/html'
...
日志更长,但是没有其他速度下降。
挂起的最长时间恰好在控制器代码(在我的第一个“ TIMER”日志上方的日志中)进行实际查询之前。还是控制器没有按顺序执行?这是我的请求映射:
@RequestMapping(value = {"/employees/active"})
public String showActiveEmployees(final ModelMap model) {
this.loggerService.info("TIMING - controller start");
model.addAttribute("allEmployees", this.employeeService.findAllActive());
this.loggerService.info("TIMING - controller end");
return "employees-list";
}
因此,我对速度下降的确切位置及其与MongoDB集合的关系感到困惑,即使此时查询甚至没有执行。如果有什么我可以改进的话。
如果需要,这里是Employee类。
public class Employee {
@Id
public String id;
public List<Skill> skills = new ArrayList<>();
public List<EmployeeLanguage> employeeLanguages = new ArrayList<>();
public List<EmployeeAbsence> employeeAbsences = new ArrayList<>();
public List<EmployeeEvaluation> employeeEvaluations = new ArrayList<>();
public List<EmployeeNote> employeeNotes = new ArrayList<>();
public String firstName;
public String lastName;
public String code;
public int peopleOnBehalf;
public String maritalStatus;
public String nationality;
public String address;
public int postalCode;
public String town;
public String phoneNumber;
public String email;
@DateTimeFormat(iso = DateTimeFormat.ISO.DATE)
public LocalDate dateOfBirth;
public String placeOfBirth;
public String landOfBirth;
public String idCardNumber;
public String nationalRegisterNumber;
public String bankAccountNumber;
public String comment;
public boolean active = true;
@DateTimeFormat(iso = DateTimeFormat.ISO.DATE)
public LocalDate startDate;
@DateTimeFormat(iso = DateTimeFormat.ISO.DATE)
public LocalDate endDate;
public String unactiveReason;
public LocalDateTime dateCreation;
public LocalDateTime dateLastEdit;
我的服务:
@Service
public class EmployeeService {
@Autowired
private EmployeeRepository employeeRepository;
...
public List<Employee> findAllActive() {
return this.employeeRepository.findByActive(true);
}
}
我的资料库:
public interface EmployeeRepository extends MongoRepository<Employee, String> {
List<Employee> findByActive(boolean active);
}