Skip to content

Commit

Permalink
Merge pull request #208 from Team-Shaka/feature/160
Browse files Browse the repository at this point in the history
Feature/160 : 브리핑 목록 조회 쿼리 튜닝
  • Loading branch information
swa07016 authored Jul 22, 2024
2 parents 92fede9 + f09a64d commit 1cbf9f1
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 26 deletions.
14 changes: 7 additions & 7 deletions Briefing-Api/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@ spring:
properties:
hibernate:
dialect: org.hibernate.dialect.MySQLDialect
# show_sql: true
# format_sql: true
show_sql: true
format_sql: true
use_sql_comments: true
hbm2ddl:
auto: update
Expand Down Expand Up @@ -147,8 +147,8 @@ spring:
properties:
hibernate:
dialect: org.hibernate.dialect.MySQLDialect
# show_sql: true
# format_sql: true
show_sql: true
format_sql: true
use_sql_comments: true
hbm2ddl:
auto: update
Expand Down Expand Up @@ -186,9 +186,9 @@ spring:
hibernate:
ddl-hbm2ddl:
auto: create
# show_sql: true
# format_sql: true
# use_sql_comments: true
show_sql: true
format_sql: true
use_sql_comments: true
default_batch_fetch_size: 1000
data:
redis:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@
import com.example.briefingcommon.entity.enums.BriefingType;
import com.example.briefingcommon.entity.enums.TimeOfDay;
import com.querydsl.core.Tuple;
import com.querydsl.core.types.Expression;
import com.querydsl.core.types.ExpressionUtils;
import com.querydsl.core.types.dsl.DateTemplate;
import com.querydsl.core.types.dsl.DateTimePath;
import com.querydsl.core.types.dsl.Expressions;
import com.querydsl.jpa.JPAExpressions;
import com.querydsl.jpa.impl.JPAQueryFactory;
import org.springframework.stereotype.Repository;

Expand Down Expand Up @@ -64,20 +66,19 @@ public List<Briefing> findTop10ByTypeOrderByCreatedAtDesc(BriefingType type) {
QBriefing briefing = QBriefing.briefing;
QScrap scrap = QScrap.scrap;

DateTimePath<LocalDateTime> dateTime = briefing.createdAt;
DateTemplate<LocalDate> date =
Expressions.dateTemplate(
LocalDate.class, "DATE_FORMAT({0}, {1})", dateTime, "%Y-%m-%d %H");
Expression<Long> scrapCount = ExpressionUtils.as(
JPAExpressions.select(scrap.id.count())
.from(scrap)
.where(scrap.briefing.eq(briefing)),
"scrapCount");

List<Tuple> results =
queryFactory
.select(briefing, scrap.count())
.select(briefing,
scrapCount)
.from(briefing)
.leftJoin(scrap)
.on(scrap.briefing.eq(briefing))
.where(briefing.type.eq(type))
.groupBy(briefing)
.orderBy(date.desc(), briefing.ranks.asc())
.orderBy(briefing.createdAt.desc())
.limit(20)
.fetch();

Expand All @@ -86,15 +87,19 @@ public List<Briefing> findTop10ByTypeOrderByCreatedAtDesc(BriefingType type) {
.map(
tuple -> {
Briefing b = tuple.get(briefing);
b.setScrapCount(Math.toIntExact(tuple.get(scrap.count())));
Long scrapCountValue = tuple.get(scrapCount);
b.setScrapCount(Math.toIntExact(scrapCountValue));
return b;
})
.collect(Collectors.toCollection(ArrayList::new));

Map<Integer, Briefing> briefingMap = new HashMap<>();
briefingList.forEach(candidate -> briefingMap.putIfAbsent(candidate.getRanks(), candidate));

return briefingMap.values().stream().toList();
return briefingMap.entrySet().stream()
.sorted(Map.Entry.comparingByKey())
.map(Map.Entry::getValue)
.collect(Collectors.toList());
}

@Override
Expand Down

This file was deleted.

8 changes: 6 additions & 2 deletions core/Briefing-Common/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,9 @@ spring:
baseline-version: 0

jpa:
hibernate:
ddl-auto: update
properties:
hibernate:
ddl-auto: update
show_sql: true
format_sql: true
use_sql_comments: true
8 changes: 4 additions & 4 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ spring:
properties:
hibernate:
dialect: org.hibernate.dialect.MySQLDialect
# show_sql: true
# format_sql: true
show_sql: true
format_sql: true
use_sql_comments: true
hbm2ddl:
auto: update
Expand Down Expand Up @@ -137,8 +137,8 @@ spring:
properties:
hibernate:
dialect: org.hibernate.dialect.MySQLDialect
# show_sql: true
# format_sql: true
show_sql: true
format_sql: true
use_sql_comments: true
hbm2ddl:
auto: update
Expand Down

0 comments on commit 1cbf9f1

Please sign in to comment.