• 카테고리

    질문 & 답변
  • 세부 분야

    백엔드

  • 해결 여부

    미해결

countQuery 최적화 질문

23.09.19 12:35 작성 조회수 226

0

countQuery관련해서 궁금한 것이 있습니다.

@Override
    public Page<ReportChangeKRWDto> search(ReportSearchRequestDto dto, Pageable pageable) {
        List<ReportChangeKRWDto> content = queryFactory
                .select(new QReportChangeKRWDto(
                        reportChange.id.intValue(),    //번호는 일단 임의로
                        reportChange.referenceDate,
                        reportChange.changeAmount,
                        reportChange.beforeAsset,
                        reportChange.changeRatio,
                        reportChange.afterAsset)
                )
                .where(searchType(dto))
                .from(reportChange)
                .orderBy(sortCondition(dto))
                .offset(pageable.getOffset())
                .limit(10)   //TOP 10 고정
                .fetch();

        //countQuery 따로
        JPAQuery<Long> countQuery = queryFactory
                .select(reportChange.count())
                .from(reportChange)
                .where(searchType(dto))
                .orderBy(sortCondition(dto));
//                .offset(pageable.getOffset())
//                .limit(10);


        return PageableExecutionUtils.getPage(content, pageable, countQuery::fetchOne);
    }

여기서 countQuery를 통해 최적화 하고 싶다면 countQuery부분에는 offset, limit을 빼고 쿼리?부분들만 적용해야 하는건가요 ??

답변 1

답변을 작성해보세요.

0

David님의 프로필

David

2023.09.20

안녕하세요. 백엔드공부화이팅님, 공식 서포터즈 David입니다.

네, 맞습니다. ReportChange의 (where 조건 포함) 전체 레코드 개수를 구해야 전체 페이지를 구할 수 있기 때문입니다.

감사합니다.