@PostMapping("")
public Long create(@RequestBody QuestionCreateDTO questionCreateDTO) {
long startTime = System.currentTimeMillis();
Long id = questionService.create(questionCreateDTO);
long endTime = System.currentTimeMillis();
log.info("executionTime={} millis", (endTime - startTime));
return id;// 질문 id 반환
}
questionCreateDTO.getTags().stream()
.forEach(tagId -> {
long beforeSelect = System.currentTimeMillis();
Tag tag = tagRepository.findById(tagId).orElseThrow(() -> new NoSuchElementException("태그 없음"));
long beforeSave = System.currentTimeMillis();
tagQuestionRepository.save(
TagQuestion.builder()
.question(question)
.tag(tag)
.build());
long afterSave = System.currentTimeMillis();
log.info("select={}", (beforeSave - beforeSelect));
log.info("save={}", (afterSave - beforeSave));
});
long beforeSelectIn = System.currentTimeMillis();
List<Tag> tags = tagRepository.findAllByIdIn(questionCreateDTO.getTags());
long beforeMapping = System.currentTimeMillis();
List<TagQuestion> tagQuestions = tags.stream()
.map(tag -> TagQuestion.builder()
.question(question)
.tag(tag)
.build())
.toList();
long beforeInsert = System.currentTimeMillis();
tagQuestionRepository.saveAll(tagQuestions);
long afterAll = System.currentTimeMillis();
log.info("select in 쿼리={}", (beforeMapping - beforeSelectIn));
log.info("mapping={}", (beforeInsert - beforeMapping));
log.info("insert 쿼리 3번={}", (afterAll - beforeInsert))
결과
결론 : 기존 코드 유지