Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/160 : 브리핑 목록 조회 쿼리 튜닝 #208

Merged
merged 1 commit into from
Jul 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading